Module: MaglevRecord::ReadWrite::ClassMethods
- Defined in:
- lib/maglev_record/read_write.rb
Instance Method Summary collapse
- #attr_accessor(*attr_names) ⇒ Object
- #attr_reader(*attr_names) ⇒ Object
- #attr_writer(*attr_names) ⇒ Object
-
#attributes ⇒ Object
resets the class to no methods returns a memento proc that can be called to restore the old state.
- #attributes_to_reset ⇒ Object
- #reset ⇒ Object
- #snapshot_attributes ⇒ Object
Instance Method Details
#attr_accessor(*attr_names) ⇒ Object
42 43 44 45 |
# File 'lib/maglev_record/read_write.rb', line 42 def attr_accessor(*attr_names) attr_reader *attr_names attr_writer *attr_names end |
#attr_reader(*attr_names) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/maglev_record/read_write.rb', line 18 def attr_reader(*attr_names) attr_names.each do |attr_name| attributes << attr_name.to_s self.module_eval <<-ATTRREADER, __FILE__, __LINE__ + 1 def #{attr_name} attributes[:#{attr_name}] end ATTRREADER end end |
#attr_writer(*attr_names) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/maglev_record/read_write.rb', line 29 def attr_writer(*attr_names) attr_names.each do |attr_name| attributes << attr_name.to_s self.module_eval <<-ATTRWRITER, __FILE__, __LINE__ + 1 def #{attr_name}=(new_value) updated attributes[:#{attr_name}] = new_value end ATTRWRITER end end |
#attributes ⇒ Object
resets the class to no methods returns a memento proc that can be called to restore the old state
51 52 53 54 55 56 57 |
# File 'lib/maglev_record/read_write.rb', line 51 def attributes @attributes ||= [] raise TypeError, "attributes contain bad elements #{@attributes}" unless @attributes.all?{ |attribute| attribute.is_a? String } @attributes.sort! @attributes.uniq! @attributes end |
#attributes_to_reset ⇒ Object
76 77 78 |
# File 'lib/maglev_record/read_write.rb', line 76 def attributes_to_reset snapshot_attributes end |
#reset ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/maglev_record/read_write.rb', line 59 def reset _attributes = attributes_to_reset.map{ |attribute| attributes.delete attribute } reset_proc = super if defined?(super) return Proc.new { reset_proc.call unless reset_proc.nil? _attributes.each{ |attribute| attributes << attribute} attributes self } end |
#snapshot_attributes ⇒ Object
72 73 74 |
# File 'lib/maglev_record/read_write.rb', line 72 def snapshot_attributes attributes.reject{|attribute| attribute.include? 'valid' } end |