Class: Samovar::Option
- Inherits:
-
Object
- Object
- Samovar::Option
- Defined in:
- lib/samovar/options.rb
Instance Attribute Summary collapse
-
#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.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #coerce(result) ⇒ Object
- #coerce_type(result) ⇒ Object
-
#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, &block) ⇒ Option
constructor
A new instance of Option.
- #parse(input, default = @default) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(flags, description, key: nil, default: nil, value: nil, type: nil, &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 |
# File 'lib/samovar/options.rb', line 25 def initialize(flags, description, key: nil, default: nil, value: nil, type: nil, &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 @block = block end |
Instance Attribute Details
#default ⇒ Object (readonly)
Returns the value of attribute default.
49 50 51 |
# File 'lib/samovar/options.rb', line 49 def default @default end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
46 47 48 |
# File 'lib/samovar/options.rb', line 46 def description @description end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
45 46 47 |
# File 'lib/samovar/options.rb', line 45 def flags @flags end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
51 52 53 |
# File 'lib/samovar/options.rb', line 51 def key @key end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
47 48 49 |
# File 'lib/samovar/options.rb', line 47 def type @type end |
Instance Method Details
#coerce(result) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/samovar/options.rb', line 65 def coerce(result) if @type result = coerce_type(result) end if @block result = @block.call(result) end return result end |
#coerce_type(result) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/samovar/options.rb', line 53 def coerce_type(result) if @type == Integer Integer(result) elsif @type == Float Float(result) elsif @type.respond_to? :call @type.call(result) elsif @type.respond_to? :new @type.new(result) end end |
#parse(input, default = @default) ⇒ Object
77 78 79 80 81 |
# File 'lib/samovar/options.rb', line 77 def parse(input, default = @default) if result = @flags.parse(input) @value.nil? ? coerce(result) : @value end || default end |
#to_a ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/samovar/options.rb', line 87 def to_a unless @default.nil? [@flags, @description, "Default: #{@default}"] else [@flags, @description] end end |
#to_s ⇒ Object
83 84 85 |
# File 'lib/samovar/options.rb', line 83 def to_s @flags end |