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.



60
61
62
63
# File 'lib/happymapper/supported_types.rb', line 60

def initialize(type,&block)
  @type = type
  @apply_block = block || no_operation
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



58
59
60
# File 'lib/happymapper/supported_types.rb', line 58

def type
  @type
end

Instance Method Details

#apply(value) ⇒ Object



73
74
75
# File 'lib/happymapper/supported_types.rb', line 73

def apply(value)
  @apply_block.call(value)
end

#apply?(value, convert_to_type) ⇒ Boolean

Returns:



69
70
71
# File 'lib/happymapper/supported_types.rb', line 69

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

#no_operationObject



65
66
67
# File 'lib/happymapper/supported_types.rb', line 65

def no_operation
  lambda {|value| value }
end