Module: ActiveType::VirtualAttributes

Extended by:
ActiveSupport::Concern
Included in:
Object, Record
Defined in:
lib/active_type/virtual_attributes.rb

Defined Under Namespace

Modules: ClassMethods, Serialization Classes: Builder, VirtualColumn

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute_for_inspect(value) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/active_type/virtual_attributes.rb', line 280

def self.attribute_for_inspect(value)
  if value.is_a?(String) && value.length > 50
    "#{value[0, 50]}...".inspect
  elsif value.is_a?(Date) || value.is_a?(Time)
    %("#{value.to_formatted_s(:db)}")
  elsif value.is_a?(Array) && value.size > 10
    inspected = value.first(10).inspect
    %(#{inspected[0...-1]}, ...])
  else
    value.inspect
  end
end

.deep_dup(hash) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/active_type/virtual_attributes.rb', line 129

def self.deep_dup(hash)
  result = hash.dup
  result.each do |key, value|
    result[key] = value.dup if value.duplicable?
  end
  result
end

Instance Method Details

#[](name) ⇒ Object



188
189
190
# File 'lib/active_type/virtual_attributes.rb', line 188

def [](name)
  read_existing_virtual_attribute(name) { super }
end

#[]=(name, value) ⇒ Object



205
206
207
# File 'lib/active_type/virtual_attributes.rb', line 205

def []=(name, value)
  write_existing_virtual_attribute(name, value) { super }
end

#_read_attribute(name) ⇒ Object



193
194
195
# File 'lib/active_type/virtual_attributes.rb', line 193

def _read_attribute(name)
  read_existing_virtual_attribute(name) { super }
end

#_write_attribute(name, value) ⇒ Object



210
211
212
# File 'lib/active_type/virtual_attributes.rb', line 210

def _write_attribute(name, value)
  write_existing_virtual_attribute(name, value) { super }
end

#attributesObject



222
223
224
225
226
# File 'lib/active_type/virtual_attributes.rb', line 222

def attributes
  self.class._virtual_column_names.each_with_object(super) do |name, attrs|
    attrs[name] = read_virtual_attribute(name)
  end
end

#changed?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/active_type/virtual_attributes.rb', line 228

def changed?
  self.class._virtual_column_names.any? { |attr| virtual_attributes_were[attr] != send(attr) } || super
end

#changesObject



232
233
234
235
236
237
238
239
240
# File 'lib/active_type/virtual_attributes.rb', line 232

def changes
  changes = self.class._virtual_column_names.each_with_object({}) do |attr, changes|
    current_value = send(attr)
    previous_value = virtual_attributes_were[attr]
    changes[attr] = [previous_value, current_value] if  previous_value != current_value
  end

  super.merge(changes)
end

#changes_appliedObject



243
244
245
246
247
248
249
250
# File 'lib/active_type/virtual_attributes.rb', line 243

def changes_applied
  super

  virtual_attributes.each do |attr, _|
    value = read_virtual_attribute(attr)
    virtual_attributes_were[attr] = value.duplicable? ? value.clone : value
  end
end

#initialize_dup(other) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/active_type/virtual_attributes.rb', line 152

def initialize_dup(other)
  @virtual_attributes_cache = {}
  @virtual_attributes = VirtualAttributes.deep_dup(virtual_attributes)
  @virtual_attributes_were = VirtualAttributes.deep_dup(virtual_attributes_were)

  super
end

#inspectObject

Returns the contents of the record as a nicely formatted string.



273
274
275
276
277
278
# File 'lib/active_type/virtual_attributes.rb', line 273

def inspect
  inspection = attributes.collect do |name, value|
    "#{name}: #{VirtualAttributes.attribute_for_inspect(value)}"
  end.sort.compact.join(", ")
  "#<#{self.class} #{inspection}>"
end

#read_attribute(name) ⇒ Object

in 6.1, read_attribute does not call _read_attribute



200
201
202
# File 'lib/active_type/virtual_attributes.rb', line 200

def read_attribute(name)
  read_existing_virtual_attribute(name) { super }
end

#read_existing_virtual_attribute(name, &block_when_not_virtual) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/active_type/virtual_attributes.rb', line 172

def read_existing_virtual_attribute(name, &block_when_not_virtual)
  if self.singleton_class._has_virtual_column?(name)
    read_virtual_attribute(name)
  else
    yield
  end
end

#read_virtual_attribute(name) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/active_type/virtual_attributes.rb', line 253

def read_virtual_attribute(name)
  name = name.to_s
  if virtual_attributes_cache.has_key?(name)
    virtual_attributes_cache[name]
  else
    virtual_attributes_cache[name] = begin
      virtual_column = self.singleton_class._virtual_column(name)
      raw_value = virtual_attributes.fetch(name) { virtual_column.default_value(self) }
      virtual_column.type_cast(raw_value)
    end
  end
end

#virtual_attributesObject



160
161
162
# File 'lib/active_type/virtual_attributes.rb', line 160

def virtual_attributes
  @virtual_attributes ||= {}
end

#virtual_attributes_cacheObject



168
169
170
# File 'lib/active_type/virtual_attributes.rb', line 168

def virtual_attributes_cache
  @virtual_attributes_cache ||= {}
end

#virtual_attributes_wereObject



164
165
166
# File 'lib/active_type/virtual_attributes.rb', line 164

def virtual_attributes_were
  @virtual_attributes_were ||= {}
end

#write_attribute(name, value) ⇒ Object

in 6.1, write_attribute does not call _write_attribute



217
218
219
# File 'lib/active_type/virtual_attributes.rb', line 217

def write_attribute(name, value)
  write_existing_virtual_attribute(name, value) { super }
end

#write_existing_virtual_attribute(name, value, &block_when_not_virtual) ⇒ Object



180
181
182
183
184
185
186
# File 'lib/active_type/virtual_attributes.rb', line 180

def write_existing_virtual_attribute(name, value, &block_when_not_virtual)
  if self.singleton_class._has_virtual_column?(name)
    write_virtual_attribute(name, value)
  else
    yield
  end
end

#write_virtual_attribute(name, value) ⇒ Object



266
267
268
269
270
# File 'lib/active_type/virtual_attributes.rb', line 266

def write_virtual_attribute(name, value)
  name = name.to_s
  virtual_attributes_cache.delete(name)
  virtual_attributes[name] = value
end