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.
-
#has_default ⇒ Object
(also: #has_default?)
Returns the value of attribute has_default.
-
#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
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
3 4 5 |
# File 'lib/optitron/option.rb', line 3 def default @default end |
#desc ⇒ Object
Returns the value of attribute desc.
3 4 5 |
# File 'lib/optitron/option.rb', line 3 def desc @desc end |
#has_default ⇒ Object Also known as: has_default?
Returns the value of attribute has_default.
3 4 5 |
# File 'lib/optitron/option.rb', line 3 def has_default @has_default end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/optitron/option.rb', line 3 def name @name end |
#parameterize ⇒ Object Also known as: parameterize?
Returns the value of attribute parameterize.
3 4 5 |
# File 'lib/optitron/option.rb', line 3 def parameterize @parameterize end |
#required ⇒ Object Also known as: required?
Returns the value of attribute required.
3 4 5 |
# File 'lib/optitron/option.rb', line 3 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
#inclusion_test=(tester) ⇒ Object
18 19 20 21 |
# File 'lib/optitron/option.rb', line 18 def inclusion_test=(tester) interpolate_type(tester.first) @inclusion_test = tester end |
#interpolate_type(default) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/optitron/option.rb', line 23 def interpolate_type(default) @type = case default when nil @type when false, true :boolean when Numeric :numeric when Array :array when Hash :hash end end |
#validate(val) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/optitron/option.rb', line 38 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 :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 |