Commit 4ee37c2

mo khan <mo@mokhan.ca>
2014-07-25 22:45:53
move storage in console and pass through as dependency.
1 parent 7041431
lib/tfa/add_command.rb
@@ -1,7 +1,7 @@
 module TFA
   class AddCommand
     def initialize(storage)
-      @storage = Storage.new(storage)
+      @storage = storage
     end
 
     def run(arguments)
lib/tfa/console.rb
@@ -1,7 +1,7 @@
 module TFA
   class Console
     def initialize(filename = "tfa")
-      @storage = PStore.new(File.join(Dir.home, ".#{filename}.pstore"))
+      @storage = Storage.new(filename)
     end
 
     def run(arguments)
lib/tfa/show_command.rb
@@ -1,7 +1,7 @@
 module TFA
   class ShowCommand
     def initialize(storage)
-      @storage = Storage.new(storage)
+      @storage = storage
     end
 
     def run(arguments)
lib/tfa/storage.rb
@@ -1,7 +1,7 @@
 module TFA
   class Storage
-    def initialize(storage)
-      @storage = storage
+    def initialize(filename)
+      @storage = PStore.new(File.join(Dir.home, ".#{filename}.pstore"))
     end
 
     def all_secrets
lib/tfa/totp_command.rb
@@ -1,7 +1,7 @@
 module TFA
   class TotpCommand
     def initialize(storage)
-      @storage = Storage.new(storage)
+      @storage = storage
     end
 
     def run(arguments)
lib/tfa/usage_command.rb
@@ -1,6 +1,7 @@
 module TFA
   class UsageCommand
     def initialize(storage)
+      @storage = storage
     end
 
     def run(arguments)
spec/lib/show_command_spec.rb
@@ -1,16 +1,13 @@
 module TFA
   describe ShowCommand do
     subject { ShowCommand.new(storage) }
-    let(:storage) { PStore.new(Tempfile.new('blah').path) }
+    let(:storage) { Storage.new(Tempfile.new('blah').path) }
 
     describe "#run" do
       context "when looking up the secret for a specific key" do
         it "retrieves the secret associated with the key given" do
           secret = SecureRandom.uuid
-          storage.transaction do
-            storage['production'] = secret
-          end
-
+          storage.save('production', secret)
           result = subject.run(['production'])
           expect(result).to eql(secret)
         end
@@ -18,12 +15,9 @@ module TFA
 
       context "when a specific name is not given" do
         it "returns all the secrets" do
-          storage.transaction do
-            storage['development'] = "1"
-            storage['staging'] = "2"
-            storage['production'] = "3"
-          end
-
+          storage.save('development', "1")
+          storage.save('staging', "2")
+          storage.save('production', "3")
           expect(subject.run([])).to eql([{"development" => "1"}, { "staging" => "2" }, { "production" => "3" }])
         end
       end
spec/lib/totp_command_spec.rb
@@ -2,12 +2,10 @@ module TFA
   describe TotpCommand do
     subject { TotpCommand.new(storage) }
     let(:secret) { ::ROTP::Base32.random_base32 }
-    let(:storage) { PStore.new(Tempfile.new('test').path) }
+    let(:storage) { Storage.new(Tempfile.new('test').path) }
 
     before :each do
-      storage.transaction do
-        storage['development'] = secret
-      end
+      storage.save('development', secret)
     end
 
     describe "#run" do