Module: CastingAttributes::ClassMethods
- Defined in:
- lib/casting_attributes.rb
Instance Method Summary collapse
- #casting_attr_accessor(attr, type_name, options = {}) ⇒ Object
- #casting_attr_reader(attr, type_name, options = {}) ⇒ Object
- #casting_attr_writer(attr, type_name) ⇒ Object
Instance Method Details
#casting_attr_accessor(attr, type_name, options = {}) ⇒ Object
33 34 35 36 |
# File 'lib/casting_attributes.rb', line 33 def casting_attr_accessor(attr, type_name, = {}) casting_attr_reader(attr, type_name, ) casting_attr_writer(attr, type_name) end |
#casting_attr_reader(attr, type_name, options = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/casting_attributes.rb', line 7 def casting_attr_reader(attr, type_name, = {}) caster = _caster_by_type_name(type_name).new(self, attr) caster.readers.each do |reader| define_method reader do value = instance_variable_get(caster.storage_var) if value.nil? and .key?(:default) default_value = [:default] instance_variable_set(caster.storage_var, default_value) value = default_value end value end end true end |
#casting_attr_writer(attr, type_name) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/casting_attributes.rb', line 23 def casting_attr_writer(attr, type_name) caster = _caster_by_type_name(type_name).new(self, attr) caster.writers.each do |writer| define_method writer do |value| instance_variable_set(caster.storage_var, caster.typecast(value)) end end true end |