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.



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

def default
  @default
end

#descObject

Returns the value of attribute desc.



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

def desc
  @desc
end

#groupObject

Returns the value of attribute group.



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

def group
  @group
end

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

Returns the value of attribute inclusion_test.



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

def inclusion_test
  @inclusion_test
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parameterizeObject Also known as: parameterize?

Returns the value of attribute parameterize.



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

def parameterize
  @parameterize
end

#requiredObject Also known as: required?

Returns the value of attribute required.



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

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

#any?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/optitron/option.rb', line 73

def any?
  @type.nil?
end

#array?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/optitron/option.rb', line 57

def array?
  @type == :array
end

#boolean?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/optitron/option.rb', line 49

def boolean?
  @type == :boolean
end

#greedy?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/optitron/option.rb', line 69

def greedy?
  @type == :greedy
end

#hash?Boolean

Returns:

  • (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

Returns:

  • (Boolean)


53
54
55
# File 'lib/optitron/option.rb', line 53

def numeric?
  @type == :numeric
end

#string?Boolean

Returns:

  • (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