Class: Samovar::Many
- Inherits:
-
Object
- Object
- Samovar::Many
- Defined in:
- lib/samovar/many.rb
Instance Attribute Summary collapse
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#stop ⇒ Object
readonly
Returns the value of attribute stop.
Instance Method Summary collapse
-
#initialize(key, description = nil, stop: /^-/, default: nil, required: false) ⇒ Many
constructor
A new instance of Many.
- #parse(input, parent = nil, default = nil) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(key, description = nil, stop: /^-/, default: nil, required: false) ⇒ Many
Returns a new instance of Many.
8 9 10 11 12 13 14 |
# File 'lib/samovar/many.rb', line 8 def initialize(key, description = nil, stop: /^-/, default: nil, required: false) @key = key @description = description @stop = stop @default = default @required = required end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
19 20 21 |
# File 'lib/samovar/many.rb', line 19 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
17 18 19 |
# File 'lib/samovar/many.rb', line 17 def description @description end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
16 17 18 |
# File 'lib/samovar/many.rb', line 16 def key @key end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
20 21 22 |
# File 'lib/samovar/many.rb', line 20 def required @required end |
#stop ⇒ Object (readonly)
Returns the value of attribute stop.
18 19 20 |
# File 'lib/samovar/many.rb', line 18 def stop @stop end |
Instance Method Details
#parse(input, parent = nil, default = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/samovar/many.rb', line 38 def parse(input, parent = nil, default = nil) if @stop and stop_index = input.index{|item| @stop === item} input.shift(stop_index) elsif input.any? input.shift(input.size) elsif default ||= @default return default elsif @required raise MissingValueError.new(parent, self) end end |
#to_a ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/samovar/many.rb', line 26 def to_a usage = [to_s, @description] if @default usage << "(default: #{@default.inspect})" elsif @required usage << "(required)" end return usage end |
#to_s ⇒ Object
22 23 24 |
# File 'lib/samovar/many.rb', line 22 def to_s "<#{key}...>" end |