Module: Idolent::LazyClassMethods
- Defined in:
- lib/idolent.rb
Instance Method Summary collapse
- #attr_accessor(*variables) ⇒ Object
- #attr_reader(*variables) ⇒ Object
- #attr_writer(*variables) ⇒ Object
- #dump(o) ⇒ Object
- #lazy(variable, clazz, options = {}) ⇒ Object
- #lazy_array(variable, clazz, options = {}) ⇒ Object
- #lazy_hash(variable, clazz, options = {}) ⇒ Object
- #load(o) ⇒ Object
Instance Method Details
#attr_accessor(*variables) ⇒ Object
108 109 110 111 112 113 |
# File 'lib/idolent.rb', line 108 def attr_accessor(*variables) variables.each do |variable| define_method(variable) { read_variable(variable) } define_method("#{variable}=".to_sym) { |value| update_variable(variable, value) } end end |
#attr_reader(*variables) ⇒ Object
115 116 117 118 119 |
# File 'lib/idolent.rb', line 115 def attr_reader(*variables) variables.each do |variable| define_method(variable) { read_variable(variable) } end end |
#attr_writer(*variables) ⇒ Object
121 122 123 124 125 |
# File 'lib/idolent.rb', line 121 def attr_writer(*variables) variables.each do |variable| define_method("#{variable}=".to_sym) { |value| update_variable(variable, value) } end end |
#dump(o) ⇒ Object
150 151 152 |
# File 'lib/idolent.rb', line 150 def dump(o) o.variables end |
#lazy(variable, clazz, options = {}) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/idolent.rb', line 127 def lazy(variable, clazz, = {}) raise "class #{clazz} must have a load method" unless clazz.respond_to?(:load) raise "class #{clazz} must have a dump method" unless clazz.respond_to?(:dump) define_method(variable) { clazz.load(read_variable(variable)) } define_method("#{variable}=") { |value| update_variable(variable, clazz.dump(value)) } end |
#lazy_array(variable, clazz, options = {}) ⇒ Object
134 135 136 137 138 |
# File 'lib/idolent.rb', line 134 def lazy_array(variable, clazz, = {}) raise "class #{clazz} must have a load method" unless clazz.respond_to?(:load) raise "class #{clazz} must have a dump method" unless clazz.respond_to?(:dump) define_method(variable) { LazyArray.new(read_variable(variable), clazz) } end |
#lazy_hash(variable, clazz, options = {}) ⇒ Object
140 141 142 143 144 |
# File 'lib/idolent.rb', line 140 def lazy_hash(variable, clazz, = {}) raise "class #{clazz} must have a load method" unless clazz.respond_to?(:load) raise "class #{clazz} must have a dump method" unless clazz.respond_to?(:dump) define_method(variable) { LazyHash.new(read_variable(variable), clazz) } end |
#load(o) ⇒ Object
146 147 148 |
# File 'lib/idolent.rb', line 146 def load(o) self.new(o) end |