Module: Hstore::Document
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/hstore/document.rb
Defined Under Namespace
Modules: ClassMethods
Classes: OwnerMissingError
Constant Summary
collapse
- Attribute =
ActiveRecord::AttributeMethods::Serialization::Attribute
Instance Method Summary
collapse
Instance Method Details
#attributes ⇒ Object
59
60
61
62
63
|
# File 'lib/hstore/document.rb', line 59
def attributes
attrs = {}
@attributes.each_key { |name| attrs[name] = read_attribute(name) }
attrs
end
|
#destroy ⇒ Object
XXX the proper way would have been to introduce an embedded_in macro, which would even support polymorphic associations, but it’s a lot of work, overhead, and I think that accesing the parent from embedded object should be discouraged as a matter of principle.
129
130
131
132
|
# File 'lib/hstore/document.rb', line 129
def destroy
@_destroyed = true
_owner.update_attribute(embedded_as, nil) if _owner
end
|
#destroyed? ⇒ Boolean
113
114
115
|
# File 'lib/hstore/document.rb', line 113
def destroyed?
@_destroyed || (_owner && _owner.destroyed?)
end
|
#eql?(other) ⇒ Boolean
Also known as:
==
65
66
67
|
# File 'lib/hstore/document.rb', line 65
def eql?(other)
other.attributes == attributes
end
|
#initialize(attributes = nil, options = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/hstore/document.rb', line 41
def initialize(attributes = nil, options = {})
@attributes = {}
assign_attributes(self.class._attr_defaults)
if options[:serialized] and attributes.present?
attributes = attributes.stringify_keys
self.class.serialized_attributes.each do |key, coder|
if attributes.key?(key)
attributes[key] = Attribute.new(coder, attributes[key], :serialized)
end
end
assign_attributes(attributes.except(self.class.serialized_attributes.keys))
else
assign_attributes(attributes) if attributes
end
changed_attributes.clear
end
|
#marked_for_destruction? ⇒ Boolean
117
118
119
|
# File 'lib/hstore/document.rb', line 117
def marked_for_destruction?
_owner && _owner.marked_for_destruction?
end
|
#persisted? ⇒ Boolean
109
110
111
|
# File 'lib/hstore/document.rb', line 109
def persisted?
! changed? && _owner && _owner.persisted?
end
|
#read_attribute(name) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/hstore/document.rb', line 77
def read_attribute(name)
name = name.to_s
if @attributes.key?(name)
val = @attributes[name]
if self.class.serialized_attributes.key?(name)
val = val.unserialized_value
end
val
else
_attr_defaults[name]
end
end
|
#save(options = {}) ⇒ Object
121
122
123
|
# File 'lib/hstore/document.rb', line 121
def save(options = {})
perform_validations(options) ? create_or_update : false
end
|
#to_hstore ⇒ Object
134
135
136
|
# File 'lib/hstore/document.rb', line 134
def to_hstore
PgHstore.dump(attributes_after_type_cast, true)
end
|
#update_attribute(name, value) ⇒ Object
70
71
72
73
74
75
|
# File 'lib/hstore/document.rb', line 70
def update_attribute(name, value)
name = name.to_s
raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
send("#{name}=", value)
save(validate: false)
end
|
#update_attributes(attributes, options) ⇒ Object
99
100
101
102
|
# File 'lib/hstore/document.rb', line 99
def update_attributes(attributes, options)
assign_attributes(attributes, options)
save
end
|
#update_attributes!(attributes, options) ⇒ Object
104
105
106
107
|
# File 'lib/hstore/document.rb', line 104
def update_attributes!(attributes, options)
assign_attributes(attributes, options)
save!
end
|
#write_attribute(name, value) ⇒ Object
90
91
92
93
94
95
96
97
|
# File 'lib/hstore/document.rb', line 90
def write_attribute(name, value)
name = name.to_s
if coder = self.class.serialized_attributes[name] and ! value.respond_to?(:serialized_value)
value = Attribute.new(coder, value, :unserialized)
end
attribute_will_change!(name)
@attributes[name] = value
end
|