Class: MongoModel::Reference

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Reference


5
6
7
# File 'lib/mongomodel/support/reference.rb', line 5

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id


3
4
5
# File 'lib/mongomodel/support/reference.rb', line 3

def id
  @id
end

Class Method Details

.cast(value) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mongomodel/support/reference.rb', line 41

def self.cast(value)
  case value
  when BSON::ObjectId
    new(value)
  else
    if BSON::ObjectId.legal?(value.to_s)
      new(BSON::ObjectId(value.to_s))
    else
      new(value.to_s)
    end
  end
end

.from_mongo(value) ⇒ Object


54
55
56
# File 'lib/mongomodel/support/reference.rb', line 54

def self.from_mongo(value)
  cast(value)
end

Instance Method Details

#as_jsonObject


19
20
21
# File 'lib/mongomodel/support/reference.rb', line 19

def as_json(*)
  to_s
end

#blank?Boolean


23
24
25
# File 'lib/mongomodel/support/reference.rb', line 23

def blank?
  id.blank?
end

#eql?(other) ⇒ Boolean Also known as: ==


27
28
29
30
31
32
33
34
# File 'lib/mongomodel/support/reference.rb', line 27

def eql?(other)
  case other
  when Reference
    id.to_s == other.id.to_s
  else
    id.to_s == other.to_s
  end
end

#hashObject


15
16
17
# File 'lib/mongomodel/support/reference.rb', line 15

def hash
  id.hash
end

#to_mongoObject


37
38
39
# File 'lib/mongomodel/support/reference.rb', line 37

def to_mongo
  id.blank? ? nil : id
end

#to_sObject Also known as: to_str


9
10
11
# File 'lib/mongomodel/support/reference.rb', line 9

def to_s
  id.to_s
end