Class: Optitron::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/optitron/option.rb

Direct Known Subclasses

Arg, Cmd, Opt

Defined Under Namespace

Classes: Arg, Cmd, Opt

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

Instance Method Summary collapse

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



3
4
5
# File 'lib/optitron/option.rb', line 3

def default
  @default
end

#descObject

Returns the value of attribute desc.



3
4
5
# File 'lib/optitron/option.rb', line 3

def desc
  @desc
end

#has_defaultObject 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

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/optitron/option.rb', line 3

def name
  @name
end

#parameterizeObject Also known as: parameterize?

Returns the value of attribute parameterize.



3
4
5
# File 'lib/optitron/option.rb', line 3

def parameterize
  @parameterize
end

#requiredObject Also known as: required?

Returns the value of attribute required.



3
4
5
# File 'lib/optitron/option.rb', line 3

def required
  @required
end

#typeObject

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