Class: MR::FakeRecord::Attribute
- Inherits:
-
Object
- Object
- MR::FakeRecord::Attribute
- Defined in:
- lib/mr/fake_record/attributes.rb
Instance Attribute Summary collapse
-
#changed_method_name ⇒ Object
readonly
Returns the value of attribute changed_method_name.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#null ⇒ Object
readonly
ActiveRecord methods.
-
#primary ⇒ Object
readonly
ActiveRecord methods.
-
#reader_method_name ⇒ Object
readonly
Returns the value of attribute reader_method_name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#was_method_name ⇒ Object
readonly
Returns the value of attribute was_method_name.
-
#writer_method_name ⇒ Object
readonly
Returns the value of attribute writer_method_name.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #changed?(record) ⇒ Boolean
- #define_on(record_class) ⇒ Object
-
#initialize(name, type, options = nil) ⇒ Attribute
constructor
A new instance of Attribute.
- #read(record) ⇒ Object
- #was(record) ⇒ Object
- #write(value, record) ⇒ Object
Constructor Details
#initialize(name, type, options = nil) ⇒ Attribute
Returns a new instance of Attribute.
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mr/fake_record/attributes.rb', line 106 def initialize(name, type, = nil) ||= {} @name = name.to_s @type = type.to_sym @primary = (@type == :primary_key) @null = .key?(:null) ? !![:null] : true @reader_method_name = @name @writer_method_name = "#{@reader_method_name}=" @was_method_name = "#{@reader_method_name}_was" @changed_method_name = "#{@reader_method_name}_changed?" end |
Instance Attribute Details
#changed_method_name ⇒ Object (readonly)
Returns the value of attribute changed_method_name.
101 102 103 |
# File 'lib/mr/fake_record/attributes.rb', line 101 def changed_method_name @changed_method_name end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
99 100 101 |
# File 'lib/mr/fake_record/attributes.rb', line 99 def name @name end |
#null ⇒ Object (readonly)
ActiveRecord methods
104 105 106 |
# File 'lib/mr/fake_record/attributes.rb', line 104 def null @null end |
#primary ⇒ Object (readonly)
ActiveRecord methods
104 105 106 |
# File 'lib/mr/fake_record/attributes.rb', line 104 def primary @primary end |
#reader_method_name ⇒ Object (readonly)
Returns the value of attribute reader_method_name.
100 101 102 |
# File 'lib/mr/fake_record/attributes.rb', line 100 def reader_method_name @reader_method_name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
99 100 101 |
# File 'lib/mr/fake_record/attributes.rb', line 99 def type @type end |
#was_method_name ⇒ Object (readonly)
Returns the value of attribute was_method_name.
101 102 103 |
# File 'lib/mr/fake_record/attributes.rb', line 101 def was_method_name @was_method_name end |
#writer_method_name ⇒ Object (readonly)
Returns the value of attribute writer_method_name.
100 101 102 |
# File 'lib/mr/fake_record/attributes.rb', line 100 def writer_method_name @writer_method_name end |
Instance Method Details
#<=>(other) ⇒ Object
141 142 143 |
# File 'lib/mr/fake_record/attributes.rb', line 141 def <=>(other) self.name <=> other.name end |
#==(other) ⇒ Object
135 136 137 138 139 |
# File 'lib/mr/fake_record/attributes.rb', line 135 def ==(other) self.name == other.name && self.type == other.type && self.null == other.null end |
#changed?(record) ⇒ Boolean
131 132 133 |
# File 'lib/mr/fake_record/attributes.rb', line 131 def changed?(record) read(record) != was(record) end |
#define_on(record_class) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mr/fake_record/attributes.rb', line 145 def define_on(record_class) attribute = self record_class.class_eval do attr_accessor attribute.reader_method_name define_method(attribute.was_method_name) do attribute.was(self) end define_method(attribute.changed_method_name) do attribute.changed?(self) end end end |
#read(record) ⇒ Object
119 120 121 |
# File 'lib/mr/fake_record/attributes.rb', line 119 def read(record) record.send(@reader_method_name) end |
#was(record) ⇒ Object
127 128 129 |
# File 'lib/mr/fake_record/attributes.rb', line 127 def was(record) record.saved_attributes[@name] end |
#write(value, record) ⇒ Object
123 124 125 |
# File 'lib/mr/fake_record/attributes.rb', line 123 def write(value, record) record.send(@writer_method_name, value) end |