Class: Samovar::Option
- Inherits:
-
Object
- Object
- Samovar::Option
- Defined in:
- lib/samovar/option.rb
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #coerce(result) ⇒ Object
- #coerce_type(result) ⇒ Object
-
#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block) ⇒ Option
constructor
A new instance of Option.
- #parse(input, parent = nil, default = nil) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block) ⇒ Option
Returns a new instance of Option.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/samovar/option.rb', line 25 def initialize(flags, description, key: nil, default: nil, value: nil, type: nil, required: false, &block) @flags = Flags.new(flags) @description = description if key @key = key else @key = @flags.first.key end @default = default # If the value is given, it overrides the user specified input. @value = value @value ||= true if @flags.boolean? @type = type @required = required @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
55 56 57 |
# File 'lib/samovar/option.rb', line 55 def block @block end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
49 50 51 |
# File 'lib/samovar/option.rb', line 49 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
47 48 49 |
# File 'lib/samovar/option.rb', line 47 def description @description end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
46 47 48 |
# File 'lib/samovar/option.rb', line 46 def flags @flags end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
48 49 50 |
# File 'lib/samovar/option.rb', line 48 def key @key end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
54 55 56 |
# File 'lib/samovar/option.rb', line 54 def required @required end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
53 54 55 |
# File 'lib/samovar/option.rb', line 53 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
51 52 53 |
# File 'lib/samovar/option.rb', line 51 def value @value end |
Instance Method Details
#coerce(result) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/samovar/option.rb', line 71 def coerce(result) if @type result = coerce_type(result) end if @block result = @block.call(result) end return result end |
#coerce_type(result) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/samovar/option.rb', line 57 def coerce_type(result) if @type == Integer Integer(result) elsif @type == Float Float(result) elsif @type == Symbol result.to_sym elsif @type.respond_to? :call @type.call(result) elsif @type.respond_to? :new @type.new(result) end end |
#parse(input, parent = nil, default = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/samovar/option.rb', line 83 def parse(input, parent = nil, default = nil) if result = @flags.parse(input) @value.nil? ? coerce(result) : @value elsif default ||= @default return default elsif @required raise MissingValueError.new(parent, self) end end |
#to_a ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/samovar/option.rb', line 97 def to_a if @default [@flags, @description, "(default: #{@default})"] elsif @required [@flags, @description, "(required)"] else [@flags, @description] end end |
#to_s ⇒ Object
93 94 95 |
# File 'lib/samovar/option.rb', line 93 def to_s @flags end |