Module: Mongomatic::TypedFields::InstanceMethods

Defined in:
lib/mongomatic/typed_fields.rb

Overview

ClassMethods

Instance Method Summary collapse

Instance Method Details

#cast_or_raise_typed_field(name, opts) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
53
54
# File 'lib/mongomatic/typed_fields.rb', line 45

def cast_or_raise_typed_field(name, opts)
  val      = value_for_key(name.to_s); return if val.nil?
  type     = opts[:type].to_sym
  try_cast = opts[:cast]
  
  converter = Mongomatic::TypeConverters.for_type(type).new(val)
  return true if converter.type_match?
  raise(InvalidType, "#{name} should be a :#{type}") unless try_cast
  set_value_for_key(name, converter.cast)
end

#check_typed_fields!Object



39
40
41
42
43
# File 'lib/mongomatic/typed_fields.rb', line 39

def check_typed_fields!
  self.class.typed_fields.each do |name, opts|
    cast_or_raise_typed_field(name, opts)
  end
end