Class: Dinamo::Model::Caster::Any

Inherits:
Object
  • Object
show all
Defined in:
lib/dinamo/model/caster.rb

Instance Method Summary collapse

Constructor Details

#initialize(type, &block) ⇒ Any



43
44
45
46
# File 'lib/dinamo/model/caster.rb', line 43

def initialize(type, &block)
  @type = type
  @block = block
end

Instance Method Details

#cast(value) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/dinamo/model/caster.rb', line 48

def cast(value)
  case @block.arity
  when 0 then @block.call
  when 1 then @block.call(value)
  when 2 then @block.call(@type, value)
  end
rescue
  fail Exceptions::CastError,
    "%p can not be casted into %p" % [value, @type]
end

#support(key) ⇒ Object



59
60
61
# File 'lib/dinamo/model/caster.rb', line 59

def support(key)
  keys << key.to_sym unless supported_key?(key)
end

#supported_key?(key) ⇒ Boolean



63
64
65
# File 'lib/dinamo/model/caster.rb', line 63

def supported_key?(key)
  keys.include?(key.to_sym)
end