Browse files

parsers WIP

  • Loading branch information...
1 parent d9e1650 commit 51923c6e2fbfce42fd18a8bbb29d761d8cf88fe8 @martinciu committed Apr 13, 2012
View
19 lib/issues/command/config.rb
@@ -0,0 +1,19 @@
+module Issues
+ module Command
+ module Config
+
+ def self.included(base)
+ base.extend ClassMethods
+ base.send(:include, Hooks)
+ base.send(:alias_method, :initialize_without_config, :initialize)
+ base.send(:alias_method, :initialize, :initialize_with_config)
+ base.after_initialize :parse_config
+ end
+
+ private
+
+ end
+ end
+end
+
+
View
22 lib/issues/command/options/config_parser.rb
@@ -0,0 +1,22 @@
+module Issues
+ module Command
+ module Options
+
+ class ConfigParser
+ def initialize(options, path)
+ self.options, self.path = options, path
+ end
+
+ def parse
+ options
+ end
+
+ private
+ attr_accessor :options, :path
+ end
+
+ end
+ end
+end
+
+
View
13 lib/issues/command/options/group.rb
@@ -4,8 +4,17 @@ module Options
class Group
extend Forwardable
+ include Enumerable
def_delegators :options, :each, :find, :<<
+ def merge(other)
+ other.each do |option|
+ options << option unless include?(option)
+ end
+
+ self
+ end
+
def initialize(options = [])
options ||= []
options.each do |option|
@@ -17,6 +26,10 @@ def fetch(name)
find { |option| option.name == name.to_s }
end
+ def <=>(ohter)
+ name == other.name.to_s
+ end
+
private
def options
@options ||= []
View
34 lib/issues/command/options/options_parser.rb
@@ -0,0 +1,34 @@
+module Issues
+ module Command
+ module Options
+
+ class OptionsParser
+ def initialize(options, args)
+ self.options, self.args = options, args
+ end
+
+ def parse
+ parser.parse!
+
+ options
+ end
+
+ private
+ attr_accessor :options, :args
+
+ def parser
+ OptionParser.new do |parser|
+ options.each do |option|
+ parser.on(*option.args) do |value|
+ option.value = value
+ end
+ end
+ end
+ end
+ end
+
+ end
+ end
+end
+
+
View
26 lib/issues/command/options/parser.rb
@@ -1,3 +1,6 @@
+require 'issues/command/options/options_parser'
+require 'issues/command/options/config_parser'
+
module Issues
module Command
module Options
@@ -8,25 +11,26 @@ def initialize(klass, args)
end
def parse
- parser.parse!(args)
-
- options
+ parsers.inject(Group.new) { |group, parser| group.merge(parser.parse) }
end
private
attr_accessor :klass, :args
+
def options
klass.options
end
- def parser
- OptionParser.new do |parser|
- options.each do |option|
- parser.on(*option.args) do |value|
- option.value = value
- end
- end
- end
+ def options_parser
+ OptionsParser.new(options, args)
+ end
+
+ def config_parser(path)
+ ConfigParser.new(options, path)
+ end
+
+ def parsers
+ [config_parser("~/.issues"), config_parser(".issues"), options_parser]
end
end

0 comments on commit 51923c6

Please sign in to comment.