class ActivePStore::DynamicMatchers::Method
Attributes
matchers[R]
attribute_names[R]
model[R]
name[R]
Public Class Methods
match(model, name)
click to toggle source
# File lib/active_pstore/dynamic_matchers.rb, line 36 def match(model, name) klass = matchers.find {|k| k.pattern === name } klass.new(model, name) if klass end
new(model, name)
click to toggle source
# File lib/active_pstore/dynamic_matchers.rb, line 56 def initialize(model, name) @model = model @name = name.to_s @attribute_names = @name.match(self.class.pattern)[1].split('_and_') end
pattern()
click to toggle source
# File lib/active_pstore/dynamic_matchers.rb, line 41 def pattern @pattern ||= /\A#{prefix}_([_a-zA-Z]\w*)#{suffix}\Z/ end
prefix()
click to toggle source
# File lib/active_pstore/dynamic_matchers.rb, line 45 def prefix raise NotImplementedError end
suffix()
click to toggle source
# File lib/active_pstore/dynamic_matchers.rb, line 49 def suffix '' end
Public Instance Methods
define()
click to toggle source
# File lib/active_pstore/dynamic_matchers.rb, line 62 def define model.class_eval <<-CODE, __FILE__, __LINE__ + 1 def self.#{name}(#{signature}) #{body} end CODE end
Private Instance Methods
attributes_hash()
click to toggle source
Given that the parameters starts with ‘_`, the finder needs to use the same parameter name.
# File lib/active_pstore/dynamic_matchers.rb, line 84 def attributes_hash "{" + attribute_names.map {|name| ":#{name} => _#{name}" }.join(',') + "}" end
body()
click to toggle source
# File lib/active_pstore/dynamic_matchers.rb, line 72 def body "#{finder}(#{attributes_hash})" end
finder()
click to toggle source
# File lib/active_pstore/dynamic_matchers.rb, line 88 def finder raise NotImplementedError end
signature()
click to toggle source
The parameters in the signature may have reserved Ruby words, in order to prevent errors, we start each param name with ‘_`.
# File lib/active_pstore/dynamic_matchers.rb, line 78 def signature attribute_names.map {|name| "_#{name}" }.join(', ') end