123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::DynamicMatchers::Method

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: activerecord/lib/active_record/dynamic_matchers.rb

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(model, name) ⇒ Method

[ GitHub ]

  
# 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 (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 34

attr_reader :matchers

Class Method Details

.match(model, name)

[ GitHub ]

  
# 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

[ GitHub ]

  
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 41

def pattern
  @pattern ||= /\A#{prefix}_([_a-zA-Z]\w*)#{suffix}\Z/
end

.prefix

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 45

def prefix
  raise NotImplementedError
end

.suffix

[ GitHub ]

  
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 49

def suffix
  ''
end

Instance Attribute Details

#attribute_names (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 54

attr_reader :model, :name, :attribute_names

#model (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 54

attr_reader :model, :name, :attribute_names

#name (readonly)

[ GitHub ]

  
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 54

attr_reader :model, :name, :attribute_names

#valid?Boolean (readonly)

[ GitHub ]

  
# 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

Instance Method Details

#body

Raises:

  • (NotImplementedError)
[ GitHub ]

  
# File 'activerecord/lib/active_record/dynamic_matchers.rb', line 75

def body
  raise NotImplementedError
end

#define

[ GitHub ]

  
# 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