Permalink
Browse files

Cleanup attribute methods a bit, use map! when accepting *args

Since we're dealing with a new array instance, it's safe to use map! and
we avoid an extra array object.

Also remove the symbolize_keys! from AttributeMethodMatcher, since it's
an internal class that always receives symbol keys from the prefix/suffix
methods implementations.
  • Loading branch information...
carlosantoniodasilva committed Jun 25, 2012
1 parent fef781c commit 7fad77fcc7a986bd11f386fcc877ed9b0bce47d1
Showing with 12 additions and 17 deletions.
  1. +12 −17 activemodel/lib/active_model/attribute_methods.rb
@@ -1,4 +1,3 @@
-require 'active_support/core_ext/hash/keys'
require 'active_support/core_ext/class/attribute'
require 'active_support/deprecation'
@@ -98,7 +97,7 @@ module ClassMethods
# person.clear_name
# person.name # => nil
def attribute_method_prefix(*prefixes)
- self.attribute_method_matchers += prefixes.map { |prefix| AttributeMethodMatcher.new :prefix => prefix }
+ self.attribute_method_matchers += prefixes.map! { |prefix| AttributeMethodMatcher.new prefix: prefix }
undefine_attribute_methods
end
@@ -133,7 +132,7 @@ def attribute_method_prefix(*prefixes)
# person.name # => "Bob"
# person.name_short? # => true
def attribute_method_suffix(*suffixes)
- self.attribute_method_matchers += suffixes.map { |suffix| AttributeMethodMatcher.new :suffix => suffix }
+ self.attribute_method_matchers += suffixes.map! { |suffix| AttributeMethodMatcher.new suffix: suffix }
undefine_attribute_methods
end
@@ -169,7 +168,7 @@ def attribute_method_suffix(*suffixes)
# person.reset_name_to_default!
# person.name # => 'Gemma'
def attribute_method_affix(*affixes)
- self.attribute_method_matchers += affixes.map { |affix| AttributeMethodMatcher.new :prefix => affix[:prefix], :suffix => affix[:suffix] }
+ self.attribute_method_matchers += affixes.map! { |affix| AttributeMethodMatcher.new prefix: affix[:prefix], suffix: affix[:suffix] }
undefine_attribute_methods
end
@@ -352,18 +351,18 @@ def attribute_method_matcher(method_name) #:nodoc:
# using the given `extra` args. This fallbacks `define_method`
# and `send` if the given names cannot be compiled.
def define_proxy_call(include_private, mod, name, send, *extra) #:nodoc:
- if name =~ NAME_COMPILABLE_REGEXP
- defn = "def #{name}(*args)"
+ defn = if name =~ NAME_COMPILABLE_REGEXP
+ "def #{name}(*args)"
else
- defn = "define_method(:'#{name}') do |*args|"
+ "define_method(:'#{name}') do |*args|"
end
- extra = (extra.map(&:inspect) << "*args").join(", ")
+ extra = (extra.map!(&:inspect) << "*args").join(", ")
- if send =~ CALL_COMPILABLE_REGEXP
- target = "#{"self." unless include_private}#{send}(#{extra})"
+ target = if send =~ CALL_COMPILABLE_REGEXP
+ "#{"self." unless include_private}#{send}(#{extra})"
else
- target = "send(:'#{send}', #{extra})"
+ "send(:'#{send}', #{extra})"
end
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
@@ -379,8 +378,6 @@ class AttributeMethodMatcher #:nodoc:
AttributeMethodMatch = Struct.new(:target, :attr_name, :method_name)
def initialize(options = {})
- options.symbolize_keys!
-
if options[:prefix] == '' || options[:suffix] == ''
ActiveSupport::Deprecation.warn(
"Specifying an empty prefix/suffix for an attribute method is no longer " \
@@ -390,7 +387,7 @@ def initialize(options = {})
)
end
- @prefix, @suffix = options[:prefix] || '', options[:suffix] || ''
+ @prefix, @suffix = options.fetch(:prefix, ''), options.fetch(:suffix, '')
@regex = /^(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})$/
@method_missing_target = "#{@prefix}attribute#{@suffix}"
@method_name = "#{prefix}%s#{suffix}"
@@ -399,8 +396,6 @@ def initialize(options = {})
def match(method_name)
if @regex =~ method_name
AttributeMethodMatch.new(method_missing_target, $1, method_name)
- else
- nil
end
end
@@ -468,7 +463,7 @@ def attribute_method?(attr_name) #:nodoc:
# The struct's attributes are prefix, base and suffix.
def match_attribute_method?(method_name)
match = self.class.send(:attribute_method_matcher, method_name)
- match && attribute_method?(match.attr_name) ? match : nil
+ match if match && attribute_method?(match.attr_name)
end
def missing_attribute(attr_name, stack)

0 comments on commit 7fad77f

Please sign in to comment.