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



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

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



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

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



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

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

#[]=(name, value) ⇒ Object



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

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



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

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

#attributesObject



170
171
172
173
174
# File 'lib/active_type/virtual_attributes.rb', line 170

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)


176
177
178
# File 'lib/active_type/virtual_attributes.rb', line 176

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

#changesObject



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

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



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

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



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

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.



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

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



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

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



133
134
135
# File 'lib/active_type/virtual_attributes.rb', line 133

def virtual_attributes
  @virtual_attributes ||= {}
end

#virtual_attributes_cacheObject



141
142
143
# File 'lib/active_type/virtual_attributes.rb', line 141

def virtual_attributes_cache
  @virtual_attributes_cache ||= {}
end

#virtual_attributes_wereObject



137
138
139
# File 'lib/active_type/virtual_attributes.rb', line 137

def virtual_attributes_were
  @virtual_attributes_were ||= {}
end

#write_virtual_attribute(name, value) ⇒ Object



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

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