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



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

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



134
135
136
137
138
139
140
# File 'lib/active_type/virtual_attributes.rb', line 134

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

#[]=(name, value) ⇒ Object



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

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



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

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

#attributesObject



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

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

#initialize_dup(other) ⇒ Object



119
120
121
122
123
124
# File 'lib/active_type/virtual_attributes.rb', line 119

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

  super
end

#inspectObject

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



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

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



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/active_type/virtual_attributes.rb', line 165

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



126
127
128
# File 'lib/active_type/virtual_attributes.rb', line 126

def virtual_attributes
  @virtual_attributes ||= {}
end

#virtual_attributes_cacheObject



130
131
132
# File 'lib/active_type/virtual_attributes.rb', line 130

def virtual_attributes_cache
  @virtual_attributes_cache ||= {}
end

#write_virtual_attribute(name, value) ⇒ Object



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

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