Module: AttrMagic

Defined in:
lib/attr_magic.rb,
lib/attr_magic/version.rb,
lib/attr_magic/instance_methods.rb

Overview

Ease lazy attribute implementation to the owner class.

Usage

class Klass
  AttrMagic.load(self)
end

Features

Implement a lazy attribute reader

class Person
  
  attr_writer :full_name

  def full_name
    igetset(__method__) { [first_name, last_name].compact.join(" ").strip }
  end
end

See InstanceMethods#igetset, InstanceMethods#igetwrite.

Validate an attribute

class Person
  
  def full_name
    igetset(__method__) do
      #require_attr :first_name                 # Will check for `nil` only.
      require_attr :first_name, :present?
      #require_attr :first_name, :not_blank?    # Also possible.
      [first_name, last_name].compact.join(" ").strip
    end
  end
end

See InstanceMethods#require_attr.

Defined Under Namespace

Modules: InstanceMethods

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.load(owner) ⇒ void

Load the feature into owner.

Parameters:

  • owner (Class)


46
47
48
49
50
# File 'lib/attr_magic.rb', line 46

def self.load(owner)
  return if owner < InstanceMethods
  owner.send(:include, InstanceMethods)
  owner.class_eval { private :igetset, :igetwrite, :require_attr }
end