Module: MongoThing::Document::InstanceMethods

Defined in:
lib/mongo_thing/document.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mid, *args) ⇒ Object

OpenStruct like behavior



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mongo_thing/document.rb', line 91

def method_missing(mid, *args) # :nodoc:
  mname = mid.id2name
  chomped = mname.chomp('=')
  len = args.length
  if self.class.properties.include?(chomped.to_sym)
    if mname == chomped && len > 0 || len > 1
      raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
    end
    new_attribute(mname.chomp('='))
    @attributes.send(mid, *args)
  else
    raise NoMethodError, "undefined method `#{mname}' for #{self}", caller(1)
  end
end

Instance Method Details

#==(other) ⇒ Object

Performs equality checking on the document ids. For more robust equality checking please override this method.



142
143
144
145
# File 'lib/mongo_thing/document.rb', line 142

def ==(other)
  return false unless other.is_a?(Document)
  id == other.id
end

#attributes=(attrs = {}) ⇒ Object



133
134
135
136
137
138
# File 'lib/mongo_thing/document.rb', line 133

def attributes=(attrs={})
  attrs ||= {}
  attrs.each do |k,v|
    self.send("#{k}=", v)
  end
end

#cloneObject

Clone the current Document. This will return all attributes with the exception of the document’s id and versions.



158
159
160
# File 'lib/mongo_thing/document.rb', line 158

def clone
  self.class.instantiate(@attributes.except("_id").except("versions").dup, true)
end

#eql?(comparison_object) ⇒ Boolean

Delegates to ==

Returns:

  • (Boolean)


148
149
150
# File 'lib/mongo_thing/document.rb', line 148

def eql?(comparison_object)
  self == (comparison_object)
end

#idObject



190
191
192
# File 'lib/mongo_thing/document.rb', line 190

def id
  @attributes._id
end

#id=(new_id) ⇒ Object



194
195
196
# File 'lib/mongo_thing/document.rb', line 194

def id=(new_id)
  @attributes._id = new_id
end

#initialize(attrs = {}) ⇒ Object

Instantiate a new Document, setting the Document’s attributes if given. If no attributes are provided, they will be initialized with an empty Hash.

If a primary key is defined, the document’s id will be set to that key, otherwise it will be set to a fresh BSON::ObjectID string.

Options:

attrs: The attributes Hash to set up the document with.



127
128
129
130
131
# File 'lib/mongo_thing/document.rb', line 127

def initialize(attrs = {})
  @attributes = Attributes.new
  self.attributes = attrs
  self
end

#inspectObject

Returns the class name plus its attributes.



163
164
165
# File 'lib/mongo_thing/document.rb', line 163

def inspect
  self.class.name + ": " + to_hash.inspect
end

#new_attribute(name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/mongo_thing/document.rb', line 106

def new_attribute(name)
  name = name.to_sym
  unless self.respond_to?(name)
    class << self; self; end.class_eval do
      define_method(name) { @attributes.send(name) }
      define_method("#{name}=") { |x| @attributes.send("#{name}=", x) }
    end
  end
  name
end

#new_record?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/mongo_thing/document.rb', line 203

def new_record?
  id.nil?
end

#persisted?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/mongo_thing/document.rb', line 186

def persisted?
  true
end

#reloadObject

Reloads the Document attributes from the database.



168
169
170
171
172
173
174
# File 'lib/mongo_thing/document.rb', line 168

def reload
  if self.id
    self.attributes = self.class[self.id].to_hash
  else
    raise ArgumentError, "this document has not been saved"
  end
end

#saveObject



211
212
213
214
215
# File 'lib/mongo_thing/document.rb', line 211

def save
  @attributes._id = BSON::ObjectId.new if @attributes._id.nil?
  collection.save(self.to_hash)
  self
end

#to_aObject

Return an array with this Document only in it.



177
178
179
# File 'lib/mongo_thing/document.rb', line 177

def to_a
  [ self ]
end

#to_hashObject



152
153
154
# File 'lib/mongo_thing/document.rb', line 152

def to_hash
  @attributes.to_hash
end

#to_keyObject

Needed for Rails / form helper



199
200
201
# File 'lib/mongo_thing/document.rb', line 199

def to_key
  [id.to_s]
end

#to_paramObject

Returns the id of the Document, used in Rails compatibility.



182
183
184
# File 'lib/mongo_thing/document.rb', line 182

def to_param
  id.to_s
end