Class: HappyMapper::SupportedTypes::CastWhenType

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#typeObject (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

Returns:


71
72
73
# File 'lib/happymapper/supported_types.rb', line 71

def apply?(_value, convert_to_type)
  convert_to_type == type
end

#no_operationObject


67
68
69
# File 'lib/happymapper/supported_types.rb', line 67

def no_operation
  ->(value) { value }
end