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



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/active_type/virtual_attributes.rb', line 277

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



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



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

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

#[]=(name, value) ⇒ Object



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

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

#_read_attribute(name) ⇒ Object



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

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

#_write_attribute(name, value) ⇒ Object



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

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

#attribute_namesObject



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

def attribute_names
  super + self.class._virtual_column_names
end

#attributesObject



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

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)


225
226
227
# File 'lib/active_type/virtual_attributes.rb', line 225

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

#changesObject



229
230
231
232
233
234
235
236
237
# File 'lib/active_type/virtual_attributes.rb', line 229

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



240
241
242
243
244
245
246
247
# File 'lib/active_type/virtual_attributes.rb', line 240

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



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

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.



270
271
272
273
274
275
# File 'lib/active_type/virtual_attributes.rb', line 270

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



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

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

#read_existing_virtual_attribute(name, &block_when_not_virtual) ⇒ Object



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

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



250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/active_type/virtual_attributes.rb', line 250

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



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

def virtual_attributes
  @virtual_attributes ||= {}
end

#virtual_attributes_cacheObject



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

def virtual_attributes_cache
  @virtual_attributes_cache ||= {}
end

#virtual_attributes_wereObject



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

def virtual_attributes_were
  @virtual_attributes_were ||= {}
end

#write_attribute(name, value) ⇒ Object

in 6.1, write_attribute does not call _write_attribute



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

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



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

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



263
264
265
266
267
# File 'lib/active_type/virtual_attributes.rb', line 263

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