Class: MR::Model::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/mr/model/fields.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Field

Returns a new instance of Field.



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/mr/model/fields.rb', line 117

def initialize(name)
  @name = name.to_s
  @reader_method_name  = @name
  @was_method_name     = "#{@name}_was"
  @changed_method_name = "#{@name}_changed?"
  @writer_method_name  = "#{@reader_method_name}="
  @attribute_reader_method_name = @reader_method_name
  @attribute_writer_method_name = @writer_method_name
  @attribute_was_method_name = "#{@reader_method_name}_was"
  @attribute_changed_method_name = "#{@reader_method_name}_changed?"
end

Instance Attribute Details

#changed_method_nameObject (readonly)

Returns the value of attribute changed_method_name.



114
115
116
# File 'lib/mr/model/fields.rb', line 114

def changed_method_name
  @changed_method_name
end

#nameObject (readonly)

Returns the value of attribute name.



113
114
115
# File 'lib/mr/model/fields.rb', line 113

def name
  @name
end

#reader_method_nameObject (readonly)

Returns the value of attribute reader_method_name.



114
115
116
# File 'lib/mr/model/fields.rb', line 114

def reader_method_name
  @reader_method_name
end

#was_method_nameObject (readonly)

Returns the value of attribute was_method_name.



114
115
116
# File 'lib/mr/model/fields.rb', line 114

def was_method_name
  @was_method_name
end

#writer_method_nameObject (readonly)

Returns the value of attribute writer_method_name.



115
116
117
# File 'lib/mr/model/fields.rb', line 115

def writer_method_name
  @writer_method_name
end

Instance Method Details

#changed?(record) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/mr/model/fields.rb', line 141

def changed?(record)
  record.send(@attribute_changed_method_name)
end

#define_reader_on(model_class) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/mr/model/fields.rb', line 145

def define_reader_on(model_class)
  field = self
  model_class.class_eval do

    define_method(field.reader_method_name) do
      field.read(record)
    end
    define_method(field.changed_method_name) do
      field.changed?(record)
    end
    define_method(field.was_method_name) do
      field.was(record)
    end

  end
end

#define_writer_on(model_class) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/mr/model/fields.rb', line 162

def define_writer_on(model_class)
  field = self
  model_class.class_eval do

    define_method(field.writer_method_name) do |value|
      field.write(value, record)
    end

  end
end

#read(record) ⇒ Object



129
130
131
# File 'lib/mr/model/fields.rb', line 129

def read(record)
  record.send(@attribute_reader_method_name)
end

#was(record) ⇒ Object



137
138
139
# File 'lib/mr/model/fields.rb', line 137

def was(record)
  record.send(@attribute_was_method_name)
end

#write(value, record) ⇒ Object



133
134
135
# File 'lib/mr/model/fields.rb', line 133

def write(value, record)
  record.send(@attribute_writer_method_name, value)
end