Skip to content

Commit

Permalink
- Finishing off serialization refactor.
Browse files Browse the repository at this point in the history
- Any type can now be localized. Closes #1558.
- Serialization now only needs a #mongoize instance method and
  .demongoize class method on any object to be able to be a custom type.
  Closes #1604.
  • Loading branch information
durran committed Apr 29, 2012
1 parent d1a770b commit 56151a2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/mongoid/extensions/float.rb
Expand Up @@ -21,7 +21,11 @@ module ClassMethods
#
# @since 3.0.0
def mongoize(object)
object ? (__numeric__(object) rescue 0.0) : object
unless object.blank?
__numeric__(object) rescue 0.0
else
nil
end
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion lib/mongoid/extensions/integer.rb
Expand Up @@ -31,7 +31,11 @@ module ClassMethods
#
# @since 3.0.0
def mongoize(object)
object ? (__numeric__(object).to_i rescue 0) : object
unless object.blank?
__numeric__(object).to_i rescue 0
else
nil
end
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions lib/mongoid/fields/foreign_key.rb
Expand Up @@ -3,6 +3,42 @@ module Mongoid
module Fields
class ForeignKey < Standard

# Adds the atomic changes for this type of resizable field.
#
# @example Add the atomic changes.
# field.add_atomic_changes(doc, "key", {}, [], [])
#
# @todo: Durran: Refactor, big time.
#
# @param [ Document ] document The document to add to.
# @param [ String ] name The name of the field.
# @param [ String ] key The atomic location of the field.
# @param [ Hash ] mods The current modifications.
# @param [ Array ] new The new elements to add.
# @param [ Array ] old The old elements getting removed.
#
# @since 2.4.0
def add_atomic_changes(document, name, key, mods, new_elements, old_elements)
old = (old_elements || [])
new = (new_elements || [])
if new.length > old.length
if new.first(old.length) == old
document.atomic_array_add_to_sets[key] = new.drop(old.length)
else
mods[key] = document.attributes[name]
end
elsif new.length < old.length
pulls = old - new
if new == old - pulls
document.atomic_array_pulls[key] = pulls
else
mods[key] = document.attributes[name]
end
elsif new != old
mods[key] = document.attributes[name]
end
end

# Is this field a foreign key?
#
# @example Is the field a foreign key?
Expand All @@ -15,6 +51,10 @@ def foreign_key?
true
end

def evolve(object)
object.is_a?(Document) ? object.id : mongoize(object)
end

def mongoize(object)
type.__mongoize_fk__(constraint, object, object_id_field?)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/mongoid/extensions/float_spec.rb
Expand Up @@ -91,8 +91,8 @@

context "when the string is empty" do

it "returns 0" do
Float.mongoize("").should eq(0)
it "returns nil" do
Float.mongoize("").should be_nil
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/mongoid/extensions/integer_spec.rb
Expand Up @@ -91,8 +91,8 @@

context "when the string is empty" do

it "returns 0" do
Integer.mongoize("").should eq(0)
it "returns nil" do
Integer.mongoize("").should be_nil
end
end

Expand Down

0 comments on commit 56151a2

Please sign in to comment.