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
.deep_dup(hash) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/active_type/virtual_attributes.rb', line 81
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
112
113
114
115
116
117
118
|
# File 'lib/active_type/virtual_attributes.rb', line 112
def [](name)
if self.singleton_class._has_virtual_column?(name)
read_virtual_attribute(name)
else
super
end
end
|
#[]=(name, value) ⇒ Object
129
130
131
132
133
134
135
|
# File 'lib/active_type/virtual_attributes.rb', line 129
def []=(name, value)
if self.singleton_class._has_virtual_column?(name)
write_virtual_attribute(name, value)
else
super
end
end
|
#_read_attribute(name) ⇒ Object
121
122
123
124
125
126
127
|
# File 'lib/active_type/virtual_attributes.rb', line 121
def _read_attribute(name)
if self.singleton_class._has_virtual_column?(name)
read_virtual_attribute(name)
else
super
end
end
|
#attributes ⇒ Object
137
138
139
140
141
|
# File 'lib/active_type/virtual_attributes.rb', line 137
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
97
98
99
100
101
102
|
# File 'lib/active_type/virtual_attributes.rb', line 97
def initialize_dup(other)
@virtual_attributes_cache = {}
@virtual_attributes = VirtualAttributes.deep_dup(@virtual_attributes)
super
end
|
#read_virtual_attribute(name) ⇒ Object
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/active_type/virtual_attributes.rb', line 143
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_attributes ⇒ Object
104
105
106
|
# File 'lib/active_type/virtual_attributes.rb', line 104
def virtual_attributes
@virtual_attributes ||= {}
end
|
#virtual_attributes_cache ⇒ Object
108
109
110
|
# File 'lib/active_type/virtual_attributes.rb', line 108
def virtual_attributes_cache
@virtual_attributes_cache ||= {}
end
|
#write_virtual_attribute(name, value) ⇒ Object
156
157
158
159
160
|
# File 'lib/active_type/virtual_attributes.rb', line 156
def write_virtual_attribute(name, value)
name = name.to_s
virtual_attributes_cache.delete(name)
virtual_attributes[name] = value
end
|