Class: Bogo::Cli::Parser::OptionValues
- Inherits:
-
Object
- Object
- Bogo::Cli::Parser::OptionValues
- Extended by:
- Forwardable
- Defined in:
- lib/bogo-cli/parser.rb
Constant Summary collapse
- NONFORWARDABLE =
[:is_a?, :respond_to?, :object_id, :inspect, :keys, :to_s, :[]=].freeze
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
- #assign(key, value) ⇒ Object
- #composite ⇒ Object
- #default?(key) ⇒ Boolean
-
#initialize ⇒ OptionValues
constructor
A new instance of OptionValues.
- #inspect ⇒ Object
- #is_a?(const) ⇒ Boolean
- #keys ⇒ Object
- #respond_to?(method) ⇒ Boolean
- #set_default(key, value) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ OptionValues
Returns a new instance of OptionValues.
23 24 25 26 |
# File 'lib/bogo-cli/parser.rb', line 23 def initialize @defaults = Smash.new @sets = Smash.new end |
Instance Method Details
#[]=(key, value) ⇒ Object
28 29 30 |
# File 'lib/bogo-cli/parser.rb', line 28 def []=(key, value) assign(key, value) end |
#assign(key, value) ⇒ Object
32 33 34 35 36 |
# File 'lib/bogo-cli/parser.rb', line 32 def assign(key, value) key = key.to_s.gsub("-", "_").to_sym @sets[key] = value self end |
#composite ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/bogo-cli/parser.rb', line 72 def composite Smash.new.tap do |c| keys.each do |k| c[k] = @sets.fetch(k, @defaults[k]) end end end |
#default?(key) ⇒ Boolean
48 49 50 51 52 53 54 |
# File 'lib/bogo-cli/parser.rb', line 48 def default?(key) unless keys.include?(key) raise KeyError, "Unknown option key '#{key}'" end !@sets.keys.include?(key) end |
#inspect ⇒ Object
60 61 62 |
# File 'lib/bogo-cli/parser.rb', line 60 def inspect "<OptionValues: #{@sets.inspect} | #{@defaults.inspect}>" end |
#is_a?(const) ⇒ Boolean
64 65 66 |
# File 'lib/bogo-cli/parser.rb', line 64 def is_a?(const) super || composite.is_a?(const) end |
#keys ⇒ Object
44 45 46 |
# File 'lib/bogo-cli/parser.rb', line 44 def keys @sets.keys | @defaults.keys end |
#respond_to?(method) ⇒ Boolean
68 69 70 |
# File 'lib/bogo-cli/parser.rb', line 68 def respond_to?(method) super || composite.respond_to?(method) end |
#set_default(key, value) ⇒ Object
38 39 40 41 42 |
# File 'lib/bogo-cli/parser.rb', line 38 def set_default(key, value) key = key.to_s.gsub("-", "_").to_sym @defaults[key] = value self end |
#to_s ⇒ Object
56 57 58 |
# File 'lib/bogo-cli/parser.rb', line 56 def to_s "<OptionValues: #{composite.inspect}>" end |