module Mongomatic::TypedFields::InstanceMethods
Public Instance Methods
cast_or_raise_typed_field(name, opts)
click to toggle source
# 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!()
click to toggle source
# 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