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
- #define ⇒ Object
-
#initialize(model, method_name) ⇒ Method
constructor
A new instance of Method.
- #valid? ⇒ Boolean
Constructor Details
permalink #initialize(model, method_name) ⇒ Method
Returns a new instance of Method.
52 53 54 55 56 57 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 52 def initialize(model, method_name) @model = model @name = method_name.to_s @attribute_names = @name.match(self.class.pattern)[1].split("_and_") @attribute_names.map! { |name| @model.attribute_aliases[name] || name } end |
Class Attribute Details
Instance Attribute Details
permalink #attribute_names ⇒ Object (readonly)
Returns the value of attribute attribute_names
50 51 52 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 50 def attribute_names @attribute_names end |
Class Method Details
permalink .match(model, name) ⇒ Object
[View source]
32 33 34 35 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 32 def match(model, name) klass = matchers.find { |k| k.pattern.match?(name) } klass.new(model, name) if klass end |
permalink .pattern ⇒ Object
[View source]
37 38 39 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 37 def pattern @pattern ||= /\A#{prefix}_([_a-zA-Z]\w*)#{suffix}\Z/ end |
permalink .prefix ⇒ Object
41 42 43 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 41 def prefix raise NotImplementedError end |
permalink .suffix ⇒ Object
[View source]
45 46 47 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 45 def suffix "" end |
Instance Method Details
permalink #define ⇒ Object
[View source]
63 64 65 66 67 68 69 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 63 def define model.class_eval " def self.\#{name}(\#{signature})\n \#{body}\n end\n CODE\nend\n", __FILE__, __LINE__ + 1 |
permalink #valid? ⇒ Boolean
59 60 61 |
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 59 def valid? attribute_names.all? { |name| model.columns_hash[name] || model.reflect_on_aggregation(name.to_sym) } end |