Class: MethodFound::AttributeInterceptor

Inherits:
Interceptor
  • Object
show all
Defined in:
lib/method_found/attribute_interceptor.rb

Overview

Class defining prefix, suffix and affix methods to a class for a given attribute name or set of attribute names.

Examples:

class Person < Struct.new(:attributes)
  include MethodFound::AttributeInterceptor.new
  include MethodFound::AttributeInterceptor.new(suffix: '=')
  include MethodFound::AttributeInterceptor.new(suffix: '_contrived?')
  include MethodFound::AttributeInterceptor.new(prefix: 'clear_')
  include MethodFound::AttributeInterceptor.new(prefix: 'reset_', suffix: '_to_default!')

  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

Instance Attribute Summary

Attributes inherited from Interceptor

#matcher

Instance Method Summary collapse

Methods inherited from Interceptor

#define_method_missing, #inspect

Constructor Details

#initialize(prefix: '', suffix: '') ⇒ AttributeInterceptor

Returns a new instance of AttributeInterceptor.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/method_found/attribute_interceptor.rb', line 45

def initialize(prefix: '', suffix: '')
  @prefix, @suffix = prefix, suffix
  regex_ = regex
  attribute_matcher = proc do |method_name|
    (matches = regex_.match(method_name)) && attributes.include?(matches[1]) && matches[1]
  end
  attribute_matcher.define_singleton_method :inspect do
    regex_.inspect
  end

  super attribute_matcher do |_, attr_name, *arguments, &block|
    send("#{prefix}attribute#{suffix}", attr_name, *arguments, &block)
  end
end

Instance Method Details

#define_attribute_methods(*attr_names) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/method_found/attribute_interceptor.rb', line 64

def define_attribute_methods(*attr_names)
  prefix, suffix = @prefix, @suffix
  attr_names.each do |attr_name|
    define_method "#{@prefix}#{attr_name}#{@suffix}".freeze do |*arguments, &block|
      send("#{prefix}attribute#{suffix}".freeze, attr_name, *arguments, &block)
    end
  end
end

#regexObject



60
61
62
# File 'lib/method_found/attribute_interceptor.rb', line 60

def regex
  /\A(?:#{Regexp.escape(@prefix)})(.*)(?:#{Regexp.escape(@suffix)})\z/.freeze
end