Module: MongoMapper::Plugins::Keys::InstanceMethods
- Defined in:
- lib/novelys_mongo_mapper/plugins/keys.rb
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #attributes ⇒ Object (also: #to_mongo)
- #attributes=(attrs) ⇒ Object
- #embedded_keys ⇒ Object
- #id ⇒ Object
- #id=(value) ⇒ Object
- #initialize(attrs = {}, from_db = false) ⇒ Object
- #key_names ⇒ Object
- #keys ⇒ Object
- #new? ⇒ Boolean
- #non_embedded_keys ⇒ Object
Instance Method Details
#[](name) ⇒ Object
209 210 211 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 209 def [](name) read_key(name) end |
#[]=(name, value) ⇒ Object
213 214 215 216 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 213 def []=(name, value) ensure_key_exists(name) write_key(name, value) end |
#attributes ⇒ Object Also known as: to_mongo
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 179 def attributes attrs = HashWithIndifferentAccess.new keys.each_pair do |name, key| value = key.set(self[key.name]) attrs[name] = value end .each do |association| if documents = instance_variable_get(association.ivar) attrs[association.name] = documents.map { |document| document.to_mongo } end end attrs end |
#attributes=(attrs) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 165 def attributes=(attrs) return if attrs.blank? attrs.each_pair do |name, value| writer_method = "#{name}=" if respond_to?(writer_method) self.send(writer_method, value) else self[name.to_s] = value end end end |
#embedded_keys ⇒ Object
234 235 236 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 234 def keys.values.select { |key| key. } end |
#id ⇒ Object
197 198 199 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 197 def id _id end |
#id=(value) ⇒ Object
201 202 203 204 205 206 207 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 201 def id=(value) if self.class.using_object_id? value = MongoMapper.normalize_object_id(value) end self[:_id] = value end |
#initialize(attrs = {}, from_db = false) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 148 def initialize(attrs={}, from_db=false) unless attrs.nil? provided_keys = attrs.keys.map { |k| k.to_s } unless provided_keys.include?('_id') || provided_keys.include?('id') write_key :_id, Mongo::ObjectID.new end end @new = from_db ? false : true self._type = self.class.name if respond_to?(:_type=) self.attributes = attrs end |
#key_names ⇒ Object
224 225 226 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 224 def key_names keys.keys end |
#keys ⇒ Object
219 220 221 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 219 def keys self.class.keys end |
#new? ⇒ Boolean
161 162 163 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 161 def new? @new end |
#non_embedded_keys ⇒ Object
229 230 231 |
# File 'lib/novelys_mongo_mapper/plugins/keys.rb', line 229 def keys.values.select { |key| !key. } end |