Class: ActiveRecord::DynamicMatchers::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/dynamic_matchers.rb

Direct Known Subclasses

FindBy, FindByBang

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name) ⇒ Method

Returns a new instance of Method.



51
52
53
54
55
56
# File 'lib/active_record/dynamic_matchers.rb', line 51

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

.matchersObject (readonly)

Returns the value of attribute matchers.



29
30
31
# File 'lib/active_record/dynamic_matchers.rb', line 29

def matchers
  @matchers
end

Instance Attribute Details

#attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



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

def attribute_names
  @attribute_names
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.match(model, name) ⇒ Object



31
32
33
34
# File 'lib/active_record/dynamic_matchers.rb', line 31

def match(model, name)
  klass = matchers.find { |k| name =~ k.pattern }
  klass.new(model, name) if klass
end

.patternObject



36
37
38
# File 'lib/active_record/dynamic_matchers.rb', line 36

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

.prefixObject

Raises:

  • (NotImplementedError)


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

def prefix
  raise NotImplementedError
end

.suffixObject



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

def suffix
  ''
end

Instance Method Details

#defineObject



62
63
64
65
66
67
68
# File 'lib/active_record/dynamic_matchers.rb', line 62

def define
  model.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def self.#{name}(#{signature})
      #{body}
    end
  CODE
end

#valid?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/active_record/dynamic_matchers.rb', line 58

def valid?
  attribute_names.all? { |name| model.columns_hash[name] || model.reflect_on_aggregation(name.to_sym) }
end