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



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/active_type/virtual_attributes.rb', line 272

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_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



122
123
124
125
126
127
128
# File 'lib/active_type/virtual_attributes.rb', line 122

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



180
181
182
# File 'lib/active_type/virtual_attributes.rb', line 180

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

#[]=(name, value) ⇒ Object



197
198
199
# File 'lib/active_type/virtual_attributes.rb', line 197

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

#_read_attribute(name) ⇒ Object



185
186
187
# File 'lib/active_type/virtual_attributes.rb', line 185

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

#_write_attribute(name, value) ⇒ Object



202
203
204
# File 'lib/active_type/virtual_attributes.rb', line 202

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

#attributesObject



214
215
216
217
218
# File 'lib/active_type/virtual_attributes.rb', line 214

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)


220
221
222
# File 'lib/active_type/virtual_attributes.rb', line 220

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

#changesObject



224
225
226
227
228
229
230
231
232
# File 'lib/active_type/virtual_attributes.rb', line 224

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



235
236
237
238
239
240
241
242
# File 'lib/active_type/virtual_attributes.rb', line 235

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



144
145
146
147
148
149
150
# File 'lib/active_type/virtual_attributes.rb', line 144

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.



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

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



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

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

#read_existing_virtual_attribute(name, &block_when_not_virtual) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/active_type/virtual_attributes.rb', line 164

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



245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/active_type/virtual_attributes.rb', line 245

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



152
153
154
# File 'lib/active_type/virtual_attributes.rb', line 152

def virtual_attributes
  @virtual_attributes ||= {}
end

#virtual_attributes_cacheObject



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

def virtual_attributes_cache
  @virtual_attributes_cache ||= {}
end

#virtual_attributes_wereObject



156
157
158
# File 'lib/active_type/virtual_attributes.rb', line 156

def virtual_attributes_were
  @virtual_attributes_were ||= {}
end

#write_attribute(name, value) ⇒ Object

in 6.1, write_attribute does not call _write_attribute



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

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

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



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

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



258
259
260
261
262
# File 'lib/active_type/virtual_attributes.rb', line 258

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