diff options
| author | mo khan <mo@mokhan.ca> | 2014-07-25 16:39:11 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2014-07-25 16:39:11 -0600 |
| commit | bbff9efffa7b6f3a5938dc7e35df8e3c7c91aaa4 (patch) | |
| tree | 5092259a44ef44efd36ef6c69383d68f0d080bb0 /lib | |
| parent | eef9f2a9669a2c2b95fbf9cafd2d63f98c84dd0a (diff) | |
extract storage class.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/tfa/add_command.rb | 6 | ||||
| -rw-r--r-- | lib/tfa/show_command.rb | 48 |
2 files changed, 38 insertions, 16 deletions
diff --git a/lib/tfa/add_command.rb b/lib/tfa/add_command.rb index 63feb13..baa2e63 100644 --- a/lib/tfa/add_command.rb +++ b/lib/tfa/add_command.rb @@ -1,15 +1,13 @@ module TFA class AddCommand def initialize(storage) - @storage = storage + @storage = Storage.new(storage) end def run(arguments) name = arguments.first secret = arguments.last - @storage.transaction do - @storage[name] = secret - end + @storage.save(name, secret) "Added #{name}" end end diff --git a/lib/tfa/show_command.rb b/lib/tfa/show_command.rb index fe9c908..f7b4cc2 100644 --- a/lib/tfa/show_command.rb +++ b/lib/tfa/show_command.rb @@ -1,21 +1,45 @@ module TFA class ShowCommand def initialize(storage) - @storage = storage + @storage = Storage.new(storage) end def run(arguments) - if arguments.any? - name = arguments.last - @storage.transaction(true) do - @storage[name] - end - else - @storage.transaction(true) do - @storage.roots.map do |key| - @storage[key] - end - end + return @storage.secret_for(arguments.last) if arguments.any? + @storage.all_secrets + end + end +end + +module TFA + class Storage + def initialize(storage) + @storage = storage + end + + def all_secrets + open_readonly do |storage| + storage.roots.map { |key| { key => storage[key] } } + end + end + + def secret_for(key) + open_readonly do |storage| + storage[key] + end + end + + def save(key, value) + @storage.transaction do + @storage[key] = value + end + end + + private + + def open_readonly + @storage.transaction(true) do + yield @storage end end end |
