Module: ClassX::Bracketable

Defined in:
lib/classx/bracketable.rb

Overview

you can access attribute using Hash like interface.

class YourClass
   include ClassX
   include ClassX::Bracketable
   has :x
end

your = YourClass.new(:x => 20)
your[:x] #=> 20
your[:x] = 30
your[:x] #=> 30

you can also use for class attribute

class YourClass
   extend ClssX::CAttrs
   extend ClassX::Bracketable

   class_has :x
end

YourClass[:x] = 20
YourClass[:x] #=> 20

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/classx/bracketable.rb', line 27

def [] name
  attr_meth = nil
  if respond_to? :class_attribute_of, true
    attr_meth = :class_attribute_of
  elsif respond_to? :attribute_of, true
    attr_meth = :attribute_of
  else
    return nil
  end

  if __send__(attr_meth).keys.include? name.to_s
    __send__ name
  else
    nil
  end
end

#[]=(name, val) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/classx/bracketable.rb', line 44

def []= name, val
  attr_meth = nil
  if respond_to? :class_attribute_of, true
    attr_meth = :class_attribute_of
  elsif respond_to? :attribute_of, true
    attr_meth = :attribute_of
  else
    return nil
  end

  if __send__(attr_meth).keys.include?(name.to_s) && respond_to?("#{name.to_s}=")
    __send__ "#{name.to_s}=", val
  else
    raise NoMethodError, ":#{name} is private, or missing"
  end
end