Module: MaglevRecord::ReadWrite::ClassMethods

Defined in:
lib/maglev_record/read_write.rb

Instance Method Summary collapse

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

#attributesObject

resets the class to no methods returns a memento proc that can be called to restore the old state

Raises:

  • (TypeError)


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_resetObject



76
77
78
# File 'lib/maglev_record/read_write.rb', line 76

def attributes_to_reset
  snapshot_attributes
end

#resetObject



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_attributesObject



72
73
74
# File 'lib/maglev_record/read_write.rb', line 72

def snapshot_attributes
  attributes.reject{|attribute| attribute.include? 'valid' }
end