lib/restly/associations.rb in restly-0.0.1.beta.1 vs lib/restly/associations.rb in restly-0.0.1.beta.2
- old
+ new
@@ -1,6 +1,9 @@
module Restly::Associations
+
+ ATTR_MATCHER = /(?<attr>\w+)(?<setter>=)?$/
+
extend ActiveSupport::Concern
extend ActiveSupport::Autoload
autoload :Base
autoload :BelongsTo
@@ -44,11 +47,11 @@
def associations
IndifferentSet.new self.class.reflect_on_all_resource_associations.keys.map(&:to_sym)
end
def respond_to_association?(m)
- !!(/(?<attr>\w+)(?<setter>=)?$/ =~ m.to_s) && associations.include?(attr.to_sym)
+ (matched = ATTR_MATCHER.match m) && associations.include?(matched[:attr].to_sym)
end
def respond_to?(m, include_private = false)
respond_to_association?(m) || super
end
@@ -83,12 +86,11 @@
end
end
end
def association_missing(m, *args)
- if !!(/(?<attr>\w+)(?<setter>=)?$/ =~ m.to_s) && respond_to_association?(m)
- attr = attr.to_sym
- case !!setter
+ if (matched = ATTR_MATCHER.match m) && associations.include?(attr = matched[:attr].to_sym)
+ case !!matched[:setter]
when true
set_association(attr, *args)
when false
get_association(attr)
end
\ No newline at end of file