Class: ThorAddons::Helpers::OptionType
- Inherits:
-
Object
- Object
- ThorAddons::Helpers::OptionType
- 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
-
#option ⇒ Object
readonly
Returns the value of attribute option.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #convert_string ⇒ Object
-
#initialize(option, type) ⇒ OptionType
constructor
A new instance of OptionType.
- #valid? ⇒ Boolean
Constructor Details
#initialize(option, type) ⇒ OptionType
Returns a new instance of OptionType.
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
#option ⇒ Object (readonly)
Returns the value of attribute option.
6 7 8 |
# File 'lib/thor-addons/helpers/option_type.rb', line 6 def option @option end |
#type ⇒ Object (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_string ⇒ Object
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
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 |