Skip to content

Commit

Permalink
Switched to read_key, read_key_before_typecast and write_key as it fi…
Browse files Browse the repository at this point in the history
…ts better language-wise with mongo. read_attribute, read_attribute_before_typecast and write_attribute are still in rails plugin for those that prefer it for now.
  • Loading branch information
jnunemaker committed Jan 10, 2010
1 parent 3ca7dd5 commit 8a5eb9f
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 131 deletions.
10 changes: 4 additions & 6 deletions lib/mongo_mapper/document.rb
Expand Up @@ -337,7 +337,7 @@ def database
end

def new?
read_attribute('_id').blank? || using_custom_id?
!_id? || using_custom_id?
end

def save(options={})
Expand Down Expand Up @@ -381,9 +381,7 @@ def create(options={})
end

def assign_id
if read_attribute(:_id).blank?
write_attribute :_id, Mongo::ObjectID.new
end
self[:_id] = Mongo::ObjectID.new unless _id?
end

def update(options={})
Expand All @@ -398,8 +396,8 @@ def save_to_collection(options={})

def update_timestamps
now = Time.now.utc
write_attribute('created_at', now) if new? && read_attribute('created_at').blank?
write_attribute('updated_at', now)
self[:created_at] = now if new? && !created_at?
self[:updated_at] = now
end

def clear_custom_id_flag
Expand Down
54 changes: 27 additions & 27 deletions lib/mongo_mapper/embedded_document.rb
Expand Up @@ -105,19 +105,19 @@ def accessors_module
def create_accessors_for(key)
accessors_module.module_eval <<-end_eval
def #{key.name}
read_attribute(:#{key.name})
read_key(:#{key.name})
end
def #{key.name}_before_typecast
read_attribute_before_typecast(:#{key.name})
read_key_before_typecast(:#{key.name})
end
def #{key.name}=(value)
write_attribute(:#{key.name}, value)
write_key(:#{key.name}, value)
end
def #{key.name}?
read_attribute(:#{key.name}).present?
read_key(:#{key.name}).present?
end
end_eval
include accessors_module
Expand Down Expand Up @@ -188,11 +188,11 @@ def initialize(attrs={})
end

if self.class.embeddable?
if read_attribute(:_id).blank?
write_attribute :_id, Mongo::ObjectID.new
@new_document = true
else
if _id?
@new_document = false
else
write_key :_id, Mongo::ObjectID.new
@new_document = true
end
end
end
Expand Down Expand Up @@ -222,11 +222,11 @@ def attributes
attrs = HashWithIndifferentAccess.new

embedded_keys.each do |key|
attrs[key.name] = read_attribute(key.name).try(:attributes)
attrs[key.name] = self[key.name].try(:attributes)
end

non_embedded_keys.each do |key|
attrs[key.name] = read_attribute(key.name)
attrs[key.name] = self[key.name]
end

embedded_associations.each do |association|
Expand All @@ -241,8 +241,8 @@ def attributes
def to_mongo
attrs = HashWithIndifferentAccess.new

_keys.each_pair do |name, key|
value = key.set(read_attribute(key.name))
keys.each_pair do |name, key|
value = key.set(self[key.name])
attrs[name] = value unless value.nil?
end

Expand All @@ -262,12 +262,12 @@ def clone
end

def [](name)
read_attribute(name)
read_key(name)
end

def []=(name, value)
ensure_key_exists(name)
write_attribute(name, value)
write_key(name, value)
end

def ==(other)
Expand All @@ -294,7 +294,7 @@ def using_custom_id?

def inspect
attributes_as_nice_string = key_names.collect do |name|
"#{name}: #{read_attribute(name).inspect}"
"#{name}: #{self[name].inspect}"
end.join(", ")
"#<#{self.class} #{attributes_as_nice_string}>"
end
Expand All @@ -317,29 +317,29 @@ def update_attributes!(attrs={})
save!
end

def keys
self.metaclass.keys
end

private
def _keys
self.metaclass.keys
end

def key_names
_keys.keys
keys.keys
end

def non_embedded_keys
_keys.values.select { |key| !key.embeddable? }
keys.values.select { |key| !key.embeddable? }
end

def embedded_keys
_keys.values.select { |key| key.embeddable? }
keys.values.select { |key| key.embeddable? }
end

def ensure_key_exists(name)
self.metaclass.key(name) unless respond_to?("#{name}=")
end

def read_attribute(name)
if key = _keys[name]
def read_key(name)
if key = keys[name]
var_name = "@#{name}"
value = key.get(instance_variable_get(var_name))
instance_variable_set(var_name, value)
Expand All @@ -348,12 +348,12 @@ def read_attribute(name)
end
end

def read_attribute_before_typecast(name)
def read_key_before_typecast(name)
instance_variable_get("@#{name}_before_typecast")
end

def write_attribute(name, value)
key = _keys[name]
def write_key(name, value)
key = keys[name]
instance_variable_set "@#{name}_before_typecast", value
instance_variable_set "@#{name}", key.set(value)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/mongo_mapper/plugins/dirty.rb
Expand Up @@ -64,7 +64,7 @@ def reload(*args)

private
def clone_key_value(attribute_name)
value = send(:read_attribute, attribute_name)
value = self[attribute_name]
value.duplicable? ? value.clone : value
rescue TypeError, NoMethodError
value
Expand All @@ -90,7 +90,7 @@ def key_will_change!(attribute)
changed_keys[attribute] = clone_key_value(attribute)
end

def write_attribute(attribute, value)
def write_key(attribute, value)
attribute = attribute.to_s

if changed_keys.include?(attribute)
Expand All @@ -105,7 +105,7 @@ def write_attribute(attribute, value)
end

def value_changed?(key_name, old, value)
key = _keys[key_name]
key = keys[key_name]

if key.number? && value.blank?
value = nil
Expand Down
12 changes: 12 additions & 0 deletions lib/mongo_mapper/plugins/rails.rb
Expand Up @@ -5,6 +5,18 @@ module InstanceMethods
def new_record?
new?
end

def read_attribute(name)
self[name]
end

def read_attribute_before_typecast(name)
read_key_before_typecast(name)
end

def write_attribute(name, value)
self[name] = value
end
end

module ClassMethods
Expand Down

0 comments on commit 8a5eb9f

Please sign in to comment.