Class: HappyMapper::SupportedTypes::CastWhenType
- Inherits:
-
Object
- Object
- HappyMapper::SupportedTypes::CastWhenType
- Defined in:
- lib/happymapper/supported_types.rb
Overview
Many of the conversions are based on type. When the type specified matches then perform the action specified in the specified block. If no block is provided the value is simply returned.
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #apply(value) ⇒ Object
- #apply?(_value, convert_to_type) ⇒ Boolean
-
#initialize(type, &block) ⇒ CastWhenType
constructor
A new instance of CastWhenType.
- #no_operation ⇒ Object
Constructor Details
#initialize(type, &block) ⇒ CastWhenType
Returns a new instance of CastWhenType.
62 63 64 65 |
# File 'lib/happymapper/supported_types.rb', line 62 def initialize(type, &block) @type = type @apply_block = block || no_operation end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
60 61 62 |
# File 'lib/happymapper/supported_types.rb', line 60 def type @type end |
Instance Method Details
#apply(value) ⇒ Object
75 76 77 |
# File 'lib/happymapper/supported_types.rb', line 75 def apply(value) @apply_block.call(value) end |
#apply?(_value, convert_to_type) ⇒ Boolean
71 72 73 |
# File 'lib/happymapper/supported_types.rb', line 71 def apply?(_value, convert_to_type) convert_to_type == type end |
#no_operation ⇒ Object
67 68 69 |
# File 'lib/happymapper/supported_types.rb', line 67 def no_operation ->(value) { value } end |