Module: ClassX::Attributes

Included in:
Role::Logger
Defined in:
lib/classx/attributes.rb

Overview

added attribute feature to module.

require 'classx'
module YourApp::Role::SomeModule
  extend Attributes

  has :some_attr

end

class YourApp
  included ClassX
  included YourApp::Role::SomeModule

end

YourApp.new(:some_attr => 10)

Constant Summary collapse

ATTRIBUTE_REGEX =
/\Aattribute_of:(\w+)\z/

Instance Method Summary collapse

Instance Method Details

#attribute_ofObject

return Hash of attribute’s name as a key and attribute’s meta class as a value. for example,

class YourClass
   include ClassX
   has :x
end

YourClass.attribute_of  #=> { "x" => <ClassX::Attribute: ... > }


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/classx/attributes.rb', line 34

def attribute_of
  unless instance_variable_defined?('@__attribute_of') && @__attribute_of
    @__attribute_of = {}
    private_instance_methods.select {|meth| meth.to_s =~ ATTRIBUTE_REGEX }.each do |meth|
      key = meth.to_s.sub(ATTRIBUTE_REGEX) { $1 }
      @__attribute_of[key] = __send__ "attribute_of:#{key}"
    end
  end

  @__attribute_of
end