class MongoModel::Types::Custom

Public Class Methods

new(type) click to toggle source
# File lib/mongomodel/support/types/custom.rb, line 6
def initialize(type)
  @type = type
end

Public Instance Methods

cast(value) click to toggle source
# File lib/mongomodel/support/types/custom.rb, line 10
def cast(value)
  if value.is_a?(@type)
    value
  elsif @type.respond_to?(:cast)
    @type.cast(value)
  else
    @type.new(value)
  end
end
from_mongo(value) click to toggle source
# File lib/mongomodel/support/types/custom.rb, line 28
def from_mongo(value)
  if @type.respond_to?(:from_mongo)
    value = value.with_indifferent_access if value.respond_to?(:with_indifferent_access)
    @type.from_mongo(value)
  else
    value
  end
end
to_mongo(value) click to toggle source
# File lib/mongomodel/support/types/custom.rb, line 20
def to_mongo(value)
  if value.respond_to?(:to_mongo)
    value.to_mongo
  else
    value
  end
end