module MongoModel::Attributes::Typecasting

Public Instance Methods

[]=(key, value) click to toggle source
Calls superclass method
# File lib/mongomodel/attributes/typecasting.rb, line 4
def []=(key, value)
  values_before_typecast[key] = value

  result = super(key, typecast(key, value))
  result.parent_document = instance if result.respond_to?(:parent_document=)
  result
end
before_type_cast(key) click to toggle source
# File lib/mongomodel/attributes/typecasting.rb, line 29
def before_type_cast(key)
  values_before_typecast[key]
end
has?(key) click to toggle source

Check if key has a value that typecasts to true.

attributes = Attributes::Store.new(:comments_count => Property.new(:comments_count, Integer))

attributes = 0 attributes.has?(:comments_count)

> false

attributes = 1 attributes.has?(:comments_count)

> true

# File lib/mongomodel/attributes/typecasting.rb, line 24
def has?(key)
  value = self[key]
  boolean_typecast(key, value)
end

Private Instance Methods

boolean_typecast(key, value) click to toggle source
# File lib/mongomodel/attributes/typecasting.rb, line 46
def boolean_typecast(key, value)
  if property = properties[key]
    value ? property.boolean(value) : false
  else
    !!value
  end
end
store(key, value) click to toggle source
Calls superclass method
# File lib/mongomodel/attributes/typecasting.rb, line 34
def store(key, value)
  values_before_typecast[key] = value
  super(key, value)
end
typecast(key, value) click to toggle source
# File lib/mongomodel/attributes/typecasting.rb, line 39
def typecast(key, value)
  unless value.nil?
    property = properties[key]
    property ? property.cast(value) : value
  end
end
values_before_typecast() click to toggle source
# File lib/mongomodel/attributes/typecasting.rb, line 54
def values_before_typecast
  @values_before_typecast ||= {}
end