Sha256: 7413edbc4dfb1ac64249e47fd8af9c4c4ea48f790886e325b3ee37a8b2a95c23

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require "method_found/attribute_interceptor"

module MethodFound
=begin

@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

=end
  module AttributeMethods
    def self.included(base)
      base.instance_eval do
        def attribute_method_affix(prefix: '', suffix: '')
          include(AttributeInterceptor.new(prefix: prefix, suffix: suffix))
        end

        def attribute_method_suffix(suffix)
          include(AttributeInterceptor.new(suffix: suffix))
        end

        def attribute_method_prefix(prefix)
          include(AttributeInterceptor.new(prefix: prefix))
        end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
method_found-0.1.4 lib/method_found/attribute_methods.rb