Module: ActiveType::VirtualAttributes

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

Defined Under Namespace

Modules: ClassMethods Classes: Builder, VirtualColumn

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attribute_for_inspect(value) ⇒ Object



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

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



105
106
107
108
109
110
111
# File 'lib/active_type/virtual_attributes.rb', line 105

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



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

def [](name)
  if self.singleton_class._has_virtual_column?(name)
    read_virtual_attribute(name)
  else
    super
  end
end

#[]=(name, value) ⇒ Object



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

def []=(name, value)
  if self.singleton_class._has_virtual_column?(name)
    write_virtual_attribute(name, value)
  else
    super
  end
end

#_read_attribute(name) ⇒ Object

ActiveRecord 4.2.1



156
157
158
159
160
161
162
# File 'lib/active_type/virtual_attributes.rb', line 156

def _read_attribute(name)
  if self.singleton_class._has_virtual_column?(name)
    read_virtual_attribute(name)
  else
    super
  end
end

#attributesObject



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

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)


178
179
180
# File 'lib/active_type/virtual_attributes.rb', line 178

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

#changesObject



182
183
184
185
186
187
188
189
190
# File 'lib/active_type/virtual_attributes.rb', line 182

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



193
194
195
196
197
198
199
200
# File 'lib/active_type/virtual_attributes.rb', line 193

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



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

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.



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

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

#read_virtual_attribute(name) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/active_type/virtual_attributes.rb', line 203

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



135
136
137
# File 'lib/active_type/virtual_attributes.rb', line 135

def virtual_attributes
  @virtual_attributes ||= {}
end

#virtual_attributes_cacheObject



143
144
145
# File 'lib/active_type/virtual_attributes.rb', line 143

def virtual_attributes_cache
  @virtual_attributes_cache ||= {}
end

#virtual_attributes_wereObject



139
140
141
# File 'lib/active_type/virtual_attributes.rb', line 139

def virtual_attributes_were
  @virtual_attributes_were ||= {}
end

#write_virtual_attribute(name, value) ⇒ Object



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

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