diff options
author | Emilio Tagua <miloops@gmail.com> | 2010-09-22 12:07:48 -0300 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2010-09-22 18:08:03 -0300 |
commit | dc2f26ca38c2dbef2674da3839bee56388f39405 (patch) | |
tree | 0a417a9904170a2d9f991a8cd4a5642f8bf74661 | |
parent | 0876f39088e7f42f91948ad93659608e316ab9a4 (diff) | |
download | rails-dc2f26ca38c2dbef2674da3839bee56388f39405.tar.gz rails-dc2f26ca38c2dbef2674da3839bee56388f39405.tar.bz2 rails-dc2f26ca38c2dbef2674da3839bee56388f39405.zip |
Perf: refactor methods using inject when not need in activemodel.
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
-rw-r--r-- | activemodel/lib/active_model/dirty.rb | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/serialization.rb | 11 | ||||
-rw-r--r-- | activemodel/lib/active_model/serializers/xml.rb | 7 |
3 files changed, 6 insertions, 14 deletions
diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 1361d327b3..1dfd0b6132 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -115,7 +115,7 @@ module ActiveModel # person.name = 'bob' # person.changes # => { 'name' => ['bill', 'bob'] } def changes - changed.inject(HashWithIndifferentAccess.new){ |h, attr| h[attr] = attribute_change(attr); h } + HashWithIndifferentAccess[changed.map { |attr| [attr, attribute_change(attr)] }] end # Map of attributes that were changed when the model was saved. diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index 8f90ef64ba..37739b98a1 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -79,15 +79,8 @@ module ActiveModel attribute_names -= except end - method_names = Array.wrap(options[:methods]).inject([]) do |methods, name| - methods << name if respond_to?(name.to_s) - methods - end - - (attribute_names + method_names).inject({}) { |hash, name| - hash[name] = send(name) - hash - } + method_names = Array.wrap(options[:methods]).map { |n| n if respond_to?(n.to_s) }.compact + Hash[(attribute_names + method_names).map { |n| [n, send(n)] }] end end end diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index e89385e7e5..26a134568c 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -76,10 +76,9 @@ module ActiveModel end def serializable_methods - Array.wrap(options[:methods]).inject([]) do |methods, name| - methods << self.class::MethodAttribute.new(name.to_s, @serializable) if @serializable.respond_to?(name.to_s) - methods - end + Array.wrap(options[:methods]).map do |name| + self.class::MethodAttribute.new(name.to_s, @serializable) if @serializable.respond_to?(name.to_s) + end.compact end def serialize |