Permalink
Browse files

Perf: refactor methods using inject when not need in activemodel.

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information...
1 parent 0876f39 commit dc2f26ca38c2dbef2674da3839bee56388f39405 @miloops miloops committed with spastorino Sep 22, 2010
@@ -115,7 +115,7 @@ def changed
# 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.
@@ -79,15 +79,8 @@ def serializable_hash(options = nil)
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
@@ -76,10 +76,9 @@ def serializable_attributes
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

0 comments on commit dc2f26c

Please sign in to comment.