Class: Samovar::One
- Inherits:
-
Object
- Object
- Samovar::One
- Defined in:
- lib/samovar/one.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.
-
#pattern ⇒ Object
readonly
Returns the value of attribute pattern.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
Instance Method Summary collapse
-
#initialize(key, description, pattern: //, default: nil, required: false) ⇒ One
constructor
A new instance of One.
- #parse(input, parent = nil, default = nil) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(key, description, pattern: //, default: nil, required: false) ⇒ One
Returns a new instance of One.
8 9 10 11 12 13 14 |
# File 'lib/samovar/one.rb', line 8 def initialize(key, description, pattern: //, default: nil, required: false) @key = key @description = description @pattern = pattern @default = default @required = required end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
19 20 21 |
# File 'lib/samovar/one.rb', line 19 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
17 18 19 |
# File 'lib/samovar/one.rb', line 17 def description @description end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
16 17 18 |
# File 'lib/samovar/one.rb', line 16 def key @key end |
#pattern ⇒ Object (readonly)
Returns the value of attribute pattern.
18 19 20 |
# File 'lib/samovar/one.rb', line 18 def pattern @pattern end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
20 21 22 |
# File 'lib/samovar/one.rb', line 20 def required @required end |
Instance Method Details
#parse(input, parent = nil, default = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/samovar/one.rb', line 38 def parse(input, parent = nil, default = nil) if input.first =~ @pattern input.shift 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/one.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/one.rb', line 22 def to_s "<#{@key}>" end |