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
Defined Under Namespace
Modules: InstanceMethods
Constant Summary collapse
- VERSION =
"0.1.2"
Class Method Summary collapse
-
.load(owner) ⇒ void
Load the feature into
owner
.
Class Method Details
.load(owner) ⇒ void
Load the feature into owner
.
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 |