Richo Healey avatar Richo Healey committed 4afbe93

Move option handling (as in config) to it's own namespae

Comments (0)

Files changed (4)

lib/twat/actions.rb

       end
     end
 
-    def setdefault
-      unless config.accounts.include?(opts[:account])
-        raise NoSuchAccount
-      end
+    def setopt
+      k, v = opts[:optval].split("=")
+      raise RequiresOptVal unless v
+      options = Options.new
+      options.send(:"#{k}=", v)
 
-      config[:default] = opts[:account]
-      config.save!
-      puts "Successfully set #{opts[:account]} as default"
+      puts "Successfully set #{k} as #{v}"
     end
 
     def updateconfig

lib/twat/argparse.rb

           options[:user] = (user || :default)
           options[:action] = :user_feed
         end #}}}
-        opts.on("--set-default ACCOUNT", 'Set ACCOUNT as default') do |acct| #{{{ --set-default ACCOUNT
-          options[:action] = :setdefault
-          options[:account] = acct.to_sym
+        opts.on("--set OPTION=VALUE", 'Set OPTION to VALUE') do |optval| #{{{ --set OPTION=VALUE
+          options[:action] = :setoption
+          options[:optval] = optval
         end #}}}
         opts.on("--update-config", "Update config to latest version") do #{{{ --update-config
           options[:action] = :updateconfig

lib/twat/exceptions.rb

   class NoDefaultAccount < Exception; end
   class NoSuchCommand < Exception; end
   class NoConfigFile < Exception; end
+  class RequiresOptVal < Exception; end
   class Usage < Exception; end
   class InvalidCredentials < Exception; end
   class ConfigVersionIncorrect < Exception; end
+  class InvalidSetOpt < Exception; end
 end

lib/twat/options.rb

+module Twat
+  class Options
+
+    # A set of wrappers around the global config object to set given attributes
+    # Catching failures is convenient because of the method_missing? hook
+    def method_missing
+      raise InvalidSetOpt
+    end
+
+    def config
+      @config ||= Config.new
+    end
+
+    # This is deliberately not abstracted (it could be easily accessed from
+    # withing the method_missing method, but that will just lead to nastiness
+    # later when I implement colors, for example.
+    def default=(value)
+      unless config.accounts.include?(opts[:account])
+        raise NoSuchAccount
+      end
+
+      config[:default] = value
+      config.save!
+    end
+
+  end
+end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.