module MethodFound::AttributeMethods

Re-implementation of ActiveModel::AttributeMethods from Rails using {MethodFound::Interceptor} modules instead of class variables.

@example

class Person < Struct.new(:attributes)
  include MethodFound::AttributeMethods

  attribute_method_suffix ''
  attribute_method_suffix '='
  attribute_method_suffix '_contrived?'
  attribute_method_prefix 'clear_'
  attribute_method_affix  prefix: 'reset_', suffix: '_to_default!'

  define_attribute_methods :name

  def initialize(attributes = {})
    super(attributes)
  end

  private

  def attribute_contrived?(attr)
    true
  end

  def clear_attribute(attr)
    send("#{attr}=", nil)
  end

  def reset_attribute_to_default!(attr)
    send("#{attr}=", 'Default Name')
  end

  def attribute(attr)
    attributes[attr]
  end

  def attribute=(attr, value)
    attributes[attr] = value
  end
end

Public Class Methods

included(base) click to toggle source
# File lib/method_found/attribute_methods.rb, line 50
def self.included(base)
  base.include(AttributeInterceptor.new)
  base.instance_eval do
    def attribute_method_prefix(*prefixes)
      prefixes.each { |prefix| include AttributeInterceptor.new(prefix: prefix) }
    end

    def attribute_method_suffix(*suffixes)
      suffixes.each { |suffix| include AttributeInterceptor.new(suffix: suffix) }
    end

    def attribute_method_affix(*affixes)
      affixes.each { |affix| include AttributeInterceptor.new(prefix: affix[:prefix], suffix: affix[:suffix]) }
    end

    def define_attribute_methods(*attr_names)
      ancestors.each do |ancestor|
        ancestor.define_attribute_methods(*attr_names) if ancestor.is_a?(AttributeInterceptor)
      end
    end

    def alias_attribute(new_name, old_name)
      ancestors.each do |ancestor|
        ancestor.alias_attribute(new_name, old_name) if ancestor.is_a?(AttributeInterceptor)
      end
    end
  end
end

Public Instance Methods

alias_attribute(new_name, old_name) click to toggle source
# File lib/method_found/attribute_methods.rb, line 71
def alias_attribute(new_name, old_name)
  ancestors.each do |ancestor|
    ancestor.alias_attribute(new_name, old_name) if ancestor.is_a?(AttributeInterceptor)
  end
end
attribute_method_affix(*affixes) click to toggle source
# File lib/method_found/attribute_methods.rb, line 61
def attribute_method_affix(*affixes)
  affixes.each { |affix| include AttributeInterceptor.new(prefix: affix[:prefix], suffix: affix[:suffix]) }
end
attribute_method_prefix(*prefixes) click to toggle source
# File lib/method_found/attribute_methods.rb, line 53
def attribute_method_prefix(*prefixes)
  prefixes.each { |prefix| include AttributeInterceptor.new(prefix: prefix) }
end
attribute_method_suffix(*suffixes) click to toggle source
# File lib/method_found/attribute_methods.rb, line 57
def attribute_method_suffix(*suffixes)
  suffixes.each { |suffix| include AttributeInterceptor.new(suffix: suffix) }
end
define_attribute_methods(*attr_names) click to toggle source
# File lib/method_found/attribute_methods.rb, line 65
def define_attribute_methods(*attr_names)
  ancestors.each do |ancestor|
    ancestor.define_attribute_methods(*attr_names) if ancestor.is_a?(AttributeInterceptor)
  end
end