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(*arguments, **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.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/samovar/options.rb', line 18 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.
42 43 44 |
# File 'lib/samovar/options.rb', line 42 def defaults @defaults end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
41 42 43 |
# File 'lib/samovar/options.rb', line 41 def key @key end |
#ordered ⇒ Object (readonly)
Returns the value of attribute ordered.
39 40 41 |
# File 'lib/samovar/options.rb', line 39 def ordered @ordered end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
38 39 40 |
# File 'lib/samovar/options.rb', line 38 def title @title end |
Class Method Details
.parse(*arguments, **options, &block) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/samovar/options.rb', line 10 def self.parse(*arguments, **, &block) = self.new(*arguments, **) .instance_eval(&block) if block_given? return .freeze end |
Instance Method Details
#<<(option) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/samovar/options.rb', line 74 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
56 57 58 |
# File 'lib/samovar/options.rb', line 56 def each(&block) @ordered.each(&block) end |
#empty? ⇒ Boolean
60 61 62 |
# File 'lib/samovar/options.rb', line 60 def empty? @ordered.empty? end |
#freeze ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/samovar/options.rb', line 44 def freeze return self if frozen? @ordered.freeze @keyed.freeze @defaults.freeze @ordered.each(&:freeze) super end |
#initialize_dup(source) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/samovar/options.rb', line 30 def initialize_dup(source) super @ordered = @ordered.dup @keyed = @keyed.dup @defaults = @defaults.dup end |
#merge!(options) ⇒ Object
68 69 70 71 72 |
# File 'lib/samovar/options.rb', line 68 def merge!() .each do |option| self << option end end |
#option(*arguments, **options, &block) ⇒ Object
64 65 66 |
# File 'lib/samovar/options.rb', line 64 def option(*arguments, **, &block) self << Option.new(*arguments, **, &block) end |
#parse(input, parent = nil, default = nil) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/samovar/options.rb', line 89 def parse(input, parent = nil, default = nil) values = (default || @defaults).dup while option = @keyed[input.first] prefix = input.first result = option.parse(input) if result != nil values[option.key] = result end end return values end |
#to_s ⇒ Object
103 104 105 |
# File 'lib/samovar/options.rb', line 103 def to_s @ordered.collect(&:to_s).join(' ') end |
#usage(rows) ⇒ Object
107 108 109 110 111 |
# File 'lib/samovar/options.rb', line 107 def usage(rows) @ordered.each do |option| rows << option end end |