Class: MongoModel::Types::Custom

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomodel/support/types/custom.rb

Instance Method Summary collapse

Methods inherited from Object

#boolean, #to_query

Constructor Details

#initialize(type) ⇒ Custom

Returns a new instance of Custom.



6
7
8
# File 'lib/mongomodel/support/types/custom.rb', line 6

def initialize(type)
  @type = type
end

Instance Method Details

#cast(value) ⇒ Object



10
11
12
13
14
15
16
17
18
# 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) ⇒ Object



28
29
30
31
32
33
34
35
# 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) ⇒ Object



20
21
22
23
24
25
26
# 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