module BigRecord::AttributeMethods::ClassMethods

Declare and check for suffixed attribute methods.

Public Instance Methods

attribute_method_suffix(*suffixes) click to toggle source

Declare a method available for all attributes with the given suffix. Uses method_missing and respond_to? to rewrite the method

#{attr}#{suffix}(*args, &block)

to

attribute#{suffix}(#{attr}, *args, &block)

An attribute#{suffix} instance method must exist and accept at least the attr argument.

For example:

class Person < BigRecord::Base
  attribute_method_suffix '_changed?'

  private
    def attribute_changed?(attr)
      ...
    end
end

person = Person.find(1)
person.name_changed?    # => false
person.name = 'Hubert'
person.name_changed?    # => true
# File lib/big_record/attribute_methods.rb, line 35
def attribute_method_suffix(*suffixes)
  attribute_method_suffixes.concat suffixes
  rebuild_attribute_method_regexp
end
match_attribute_method?(method_name) click to toggle source

Returns MatchData if method_name is an attribute method.

# File lib/big_record/attribute_methods.rb, line 41
def match_attribute_method?(method_name)
  rebuild_attribute_method_regexp unless defined?(@@attribute_method_regexp) && @@attribute_method_regexp
  @@attribute_method_regexp.match(method_name)
end

Private Instance Methods

attribute_method_suffixes() click to toggle source

Default to =, ?, _before_type_cast

# File lib/big_record/attribute_methods.rb, line 54
def attribute_method_suffixes
  @@attribute_method_suffixes ||= []
end
rebuild_attribute_method_regexp() click to toggle source

Suffixes a, ?, c become regexp /(a|?|c)$/

# File lib/big_record/attribute_methods.rb, line 48
def rebuild_attribute_method_regexp
  suffixes = attribute_method_suffixes.map { |s| Regexp.escape(s) }
  @@attribute_method_regexp = /(#{suffixes.join('|')})$/.freeze
end