Class: Optitron::Option
- Inherits:
-
Object
- Object
- Optitron::Option
- Defined in:
- lib/optitron/option.rb
Defined Under Namespace
Constant Summary collapse
- TRUE_BOOLEAN_VALUES =
[true, 't', 'T', 'true', 'TRUE']
- FALSE_BOOLEAN_VALUES =
[false, 'f', 'F', 'false', 'FALSE']
- BOOLEAN_VALUES =
TRUE_BOOLEAN_VALUES + FALSE_BOOLEAN_VALUES
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#desc ⇒ Object
Returns the value of attribute desc.
-
#group ⇒ Object
Returns the value of attribute group.
-
#has_default ⇒ Object
(also: #has_default?)
Returns the value of attribute has_default.
-
#inclusion_test ⇒ Object
Returns the value of attribute inclusion_test.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parameterize ⇒ Object
(also: #parameterize?)
Returns the value of attribute parameterize.
-
#required ⇒ Object
(also: #required?)
Returns the value of attribute required.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #any? ⇒ Boolean
- #array? ⇒ Boolean
- #boolean? ⇒ Boolean
- #greedy? ⇒ Boolean
- #hash? ⇒ Boolean
- #interpolate_type(default) ⇒ Object
- #numeric? ⇒ Boolean
- #string? ⇒ Boolean
- #validate(val) ⇒ Object
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
4 5 6 |
# File 'lib/optitron/option.rb', line 4 def default @default end |
#desc ⇒ Object
Returns the value of attribute desc.
4 5 6 |
# File 'lib/optitron/option.rb', line 4 def desc @desc end |
#group ⇒ Object
Returns the value of attribute group.
4 5 6 |
# File 'lib/optitron/option.rb', line 4 def group @group end |
#has_default ⇒ Object Also known as: has_default?
Returns the value of attribute has_default.
4 5 6 |
# File 'lib/optitron/option.rb', line 4 def has_default @has_default end |
#inclusion_test ⇒ Object
Returns the value of attribute inclusion_test.
3 4 5 |
# File 'lib/optitron/option.rb', line 3 def inclusion_test @inclusion_test end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/optitron/option.rb', line 4 def name @name end |
#parameterize ⇒ Object Also known as: parameterize?
Returns the value of attribute parameterize.
4 5 6 |
# File 'lib/optitron/option.rb', line 4 def parameterize @parameterize end |
#required ⇒ Object Also known as: required?
Returns the value of attribute required.
4 5 6 |
# File 'lib/optitron/option.rb', line 4 def required @required end |
#type ⇒ Object
Returns the value of attribute type.
3 4 5 |
# File 'lib/optitron/option.rb', line 3 def type @type end |
Instance Method Details
#any? ⇒ Boolean
73 74 75 |
# File 'lib/optitron/option.rb', line 73 def any? @type.nil? end |
#array? ⇒ Boolean
57 58 59 |
# File 'lib/optitron/option.rb', line 57 def array? @type == :array end |
#boolean? ⇒ Boolean
49 50 51 |
# File 'lib/optitron/option.rb', line 49 def boolean? @type == :boolean end |
#greedy? ⇒ Boolean
69 70 71 |
# File 'lib/optitron/option.rb', line 69 def greedy? @type == :greedy end |
#hash? ⇒ Boolean
65 66 67 |
# File 'lib/optitron/option.rb', line 65 def hash? @type == :hash end |
#interpolate_type(default) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/optitron/option.rb', line 32 def interpolate_type(default) @type = case default when nil @type when false, true :boolean when String :string when Numeric :numeric when Array :array when Hash :hash end end |
#numeric? ⇒ Boolean
53 54 55 |
# File 'lib/optitron/option.rb', line 53 def numeric? @type == :numeric end |
#string? ⇒ Boolean
61 62 63 |
# File 'lib/optitron/option.rb', line 61 def string? @type == :string end |
#validate(val) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/optitron/option.rb', line 77 def validate(val) validated_type = case @type when :boolean if TRUE_BOOLEAN_VALUES.include?(val) true elsif FALSE_BOOLEAN_VALUES.include?(val) false else raise end when :numeric Integer(val) when :array Array(val) when :float Float(val) when :hash val.is_a?(Hash) ? val : raise when :greedy, nil, :string val else raise end @inclusion_test.include?(validated_type) or raise if @inclusion_test validated_type end |