Class: Samovar::Options
- Inherits:
-
Object
- Object
- Samovar::Options
- Defined in:
- lib/samovar/options.rb
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#ordered ⇒ Object
readonly
Returns the value of attribute ordered.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(option) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #freeze ⇒ Object
-
#initialize(title = "Options", key: :options) ⇒ Options
constructor
A new instance of Options.
- #initialize_dup(source) ⇒ Object
- #merge!(options) ⇒ Object
- #option(*args, **options, &block) ⇒ Object
- #parse(input, parent = nil, default = nil) ⇒ Object
- #to_s ⇒ Object
- #usage(rows) ⇒ Object
Constructor Details
#initialize(title = "Options", key: :options) ⇒ Options
Returns a new instance of Options.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/samovar/options.rb', line 33 def initialize(title = "Options", key: :options) @title = title @ordered = [] # We use this flag to option cache to improve parsing performance: @keyed = {} @key = key @defaults = {} end |
Instance Attribute Details
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
57 58 59 |
# File 'lib/samovar/options.rb', line 57 def defaults @defaults end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
56 57 58 |
# File 'lib/samovar/options.rb', line 56 def key @key end |
#ordered ⇒ Object (readonly)
Returns the value of attribute ordered.
54 55 56 |
# File 'lib/samovar/options.rb', line 54 def ordered @ordered end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
53 54 55 |
# File 'lib/samovar/options.rb', line 53 def title @title end |
Class Method Details
.parse(*args, **options, &block) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/samovar/options.rb', line 25 def self.parse(*args, **, &block) = self.new(*args, **) .instance_eval(&block) if block_given? return .freeze end |
Instance Method Details
#<<(option) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/samovar/options.rb', line 89 def << option @ordered << option option.flags.each do |flag| @keyed[flag.prefix] = option flag.alternatives.each do |alternative| @keyed[alternative] = option end end if default = option.default @defaults[option.key] = option.default end end |
#each(&block) ⇒ Object
71 72 73 |
# File 'lib/samovar/options.rb', line 71 def each(&block) @ordered.each(&block) end |
#empty? ⇒ Boolean
75 76 77 |
# File 'lib/samovar/options.rb', line 75 def empty? @ordered.empty? end |
#freeze ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/samovar/options.rb', line 59 def freeze return self if frozen? @ordered.freeze @keyed.freeze @defaults.freeze @ordered.each(&:freeze) super end |
#initialize_dup(source) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/samovar/options.rb', line 45 def initialize_dup(source) super @ordered = @ordered.dup @keyed = @keyed.dup @defaults = @defaults.dup end |
#merge!(options) ⇒ Object
83 84 85 86 87 |
# File 'lib/samovar/options.rb', line 83 def merge!() .each do |option| self << option end end |
#option(*args, **options, &block) ⇒ Object
79 80 81 |
# File 'lib/samovar/options.rb', line 79 def option(*args, **, &block) self << Option.new(*args, **, &block) end |
#parse(input, parent = nil, default = nil) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/samovar/options.rb', line 104 def parse(input, parent = nil, default = nil) values = (default || @defaults).dup while option = @keyed[input.first] if result = option.parse(input) values[option.key] = result end end return values end |
#to_s ⇒ Object
116 117 118 |
# File 'lib/samovar/options.rb', line 116 def to_s @ordered.collect(&:to_s).join(' ') end |
#usage(rows) ⇒ Object
120 121 122 123 124 |
# File 'lib/samovar/options.rb', line 120 def usage(rows) @ordered.each do |option| rows << option end end |