Skip to content

Commit

Permalink
Switched from hash with indifferent access to plain hash.
Browse files Browse the repository at this point in the history
Indifferent hashes seem to be pretty slow. This helps things out a bit. Using string keys for keys because they can be injected dynamically by users and thus an attack could be formed if symbols were used. Using symbols for associations.
  • Loading branch information
jnunemaker committed Mar 16, 2011
1 parent f19d772 commit 1571721
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def one(association_id, options={}, &extension)
end

def associations
@associations ||= HashWithIndifferentAccess.new
@associations ||= {}
end

def associations=(hash)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/associations/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Base
AssociationOptions = [:as, :class, :class_name, :dependent, :extend, :foreign_key, :in, :polymorphic, :autosave]

def initialize(name, options={}, &extension)
@name, @options, @query_options, @original_options = name, {}, {}, options
@name, @options, @query_options, @original_options = name.to_sym, {}, {}, options
options.symbolize_keys!
options[:extend] = modularized_extensions(extension, options[:extend])
separate_options_and_conditions
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/dirty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def attribute_should_change?(key, old, value)
end

def value_changed?(key_name, old, value)
value = nil if keys[key_name].number? && value.blank?
value = nil if keys[key_name.to_s].number? && value.blank?
old != value
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mongo_mapper/plugins/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def inherited(descendant)
end

def keys
@keys ||= HashWithIndifferentAccess.new
@keys ||= {}
end

def key(*args)
Expand Down Expand Up @@ -294,7 +294,7 @@ def set_parent_document(key, value)
end

def read_key(key_name)
if key = keys[key_name]
if key = keys[key_name.to_s]
value = key.get(instance_variable_get(:"@#{key_name}"))
set_parent_document(key, value)
instance_variable_set(:"@#{key_name}", value)
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo_mapper/plugins/modifiers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def decrement(*args)
def set(*args)
criteria, updates = criteria_and_keys_from_args(args)
updates.each do |key, value|
updates[key] = keys[key].set(value) if key?(key)
updates[key] = keys[key.to_s].set(value) if key?(key)
end
collection.update(criteria, {'$set' => updates}, :multi => true)
end
Expand Down
4 changes: 2 additions & 2 deletions test/functional/test_userstamps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ class UserstampsTest < Test::Unit::TestCase
end

should "add belongs_to creator" do
@document.associations.keys.should include('creator')
@document.associations.keys.should include(:creator)
end

should "add belongs_to updater" do
@document.associations.keys.should include('updater')
@document.associations.keys.should include(:updater)
end
end
end
8 changes: 4 additions & 4 deletions test/unit/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class Post
end

should "default associations to inherited class" do
Message.associations.keys.should include("room")
Enter.associations.keys.should include("room")
Exit.associations.keys.should include("room")
Chat.associations.keys.should include("room")
Message.associations.keys.should include(:room)
Enter.associations.keys.should include(:room)
Exit.associations.keys.should include(:room)
Chat.associations.keys.should include(:room)
end
end

Expand Down

0 comments on commit 1571721

Please sign in to comment.