Class: ActiveRecord::DynamicMatchers::Method
- Defined in:
- activerecord/lib/active_record/dynamic_matchers.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.matchers ⇒ Object
readonly
Returns the value of attribute matchers.
Instance Attribute Summary collapse
-
#attribute_names ⇒ Object
readonly
Returns the value of attribute attribute_names.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #body ⇒ Object
- #define ⇒ Object
-
#initialize(model, name) ⇒ Method
constructor
A new instance of Method.
- #valid? ⇒ Boolean
Constructor Details
#initialize(model, name) ⇒ Method
Returns a new instance of Method.
56 57 58 59 60 61 |
# File 'activerecord/lib/active_record/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_') @attribute_names.map! { |n| @model.attribute_aliases[n] || n } end |
Class Attribute Details
.matchers ⇒ Object (readonly)
Returns the value of attribute matchers
34 35 36 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 34 def matchers @matchers end |
Instance Attribute Details
#attribute_names ⇒ Object (readonly)
Returns the value of attribute attribute_names
54 55 56 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 54 def attribute_names @attribute_names end |
#model ⇒ Object (readonly)
Returns the value of attribute model
54 55 56 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 54 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name
54 55 56 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 54 def name @name end |
Class Method Details
.match(model, name) ⇒ Object
36 37 38 39 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 36 def match(model, name) klass = matchers.find { |k| name =~ k.pattern } klass.new(model, name) if klass end |
.pattern ⇒ Object
41 42 43 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 41 def pattern @pattern ||= /\A#{prefix}_([_a-zA-Z]\w*)#{suffix}\Z/ end |
.prefix ⇒ Object
45 46 47 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 45 def prefix raise NotImplementedError end |
.suffix ⇒ Object
49 50 51 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 49 def suffix '' end |
Instance Method Details
#body ⇒ Object
75 76 77 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 75 def body raise NotImplementedError end |
#define ⇒ Object
67 68 69 70 71 72 73 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 67 def define model.class_eval <<-CODE, __FILE__, __LINE__ + 1 def self.#{name}(#{signature}) #{body} end CODE end |
#valid? ⇒ Boolean
63 64 65 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 63 def valid? attribute_names.all? { |name| model.columns_hash[name] || model.reflect_on_aggregation(name.to_sym) } end |