Class: ThorAddons::Helpers::OptionType

Inherits:
Object
  • Object
show all
Defined in:
lib/thor-addons/helpers/option_type.rb

Constant Summary collapse

TYPE_CLASS_MAP =
{
  array: [Array],
  boolean: [TrueClass, FalseClass],
  hash: [Hash],
  numeric: [Integer, Float],
  string: [String]
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option, type) ⇒ OptionType

Returns a new instance of OptionType.

Raises:

  • (TypeError)


8
9
10
11
12
13
14
# File 'lib/thor-addons/helpers/option_type.rb', line 8

def initialize(option, type)
  @option = option
  @type = type

  raise TypeError, "Invalid type: '#{type}'" unless
    TYPE_CLASS_MAP.keys.include?(type)
end

Instance Attribute Details

#optionObject (readonly)

Returns the value of attribute option.



6
7
8
# File 'lib/thor-addons/helpers/option_type.rb', line 6

def option
  @option
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/thor-addons/helpers/option_type.rb', line 6

def type
  @type
end

Instance Method Details

#convert_stringObject



28
29
30
31
32
33
34
35
36
# File 'lib/thor-addons/helpers/option_type.rb', line 28

def convert_string
  case type
  when :boolean then option.to_b
  when :array then option.to_a
  when :hash then option.to_h
  when :numeric then option.to_n
  else option
  end
end

#valid?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/thor-addons/helpers/option_type.rb', line 24

def valid?
  TYPE_CLASS_MAP[type].any? { |klass| option.is_a?(klass) }
end