Module: Taupe::Accessorized::ClassMethods
- Defined in:
- lib/taupe/core.rb
Overview
Class methods
Instance Method Summary collapse
-
#single_accessor(*names) ⇒ Object
Define single accessor.
-
#stacked_accessor(*names) ⇒ Object
Define stacked accessor.
Instance Method Details
#single_accessor(*names) ⇒ Object
Note:
The getter is “_name” instead of “name”
Define single accessor. It can be set one time.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/taupe/core.rb', line 22 def single_accessor(*names) names.each do |name| define_method name do |data| instance_variable_set "@#{name}".to_sym, data end define_method "_#{name}" do instance_variable_get "@#{name}".to_sym end define_method "_#{name}=" do |data| instance_variable_set "@#{name}".to_sym, data end end end |
#stacked_accessor(*names) ⇒ Object
Note:
The getter is “_name_stack” instead of “name”
Define stacked accessor. It can be set several times.
40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/taupe/core.rb', line 40 def stacked_accessor(*names) names.each do |name| define_method name do |*data| stack = instance_variable_get("@_#{name}_stack".to_sym) || {} stack[data[0]] = data[1] instance_variable_set "@_#{name}_stack".to_sym, stack end define_method "_#{name}_stack" do instance_variable_get "@_#{name}_stack".to_sym end end end |