forked from mongodb/mongoid
/
attributes.rb
255 lines (238 loc) · 7.6 KB
/
attributes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# encoding: utf-8
require "mongoid/attributes/processing"
module Mongoid #:nodoc:
# This module contains the logic for handling the internal attributes hash,
# and how to get and set values.
module Attributes
extend ActiveSupport::Concern
include Processing
attr_reader :attributes
alias :raw_attributes :attributes
# Determine if an attribute is present.
#
# @example Is the attribute present?
# person.attribute_present?("title")
#
# @param [ String, Symbol ] name The name of the attribute.
#
# @return [ true, false ] True if present, false if not.
#
# @since 1.0.0
def attribute_present?(name)
attribute = read_attribute(name)
! attribute.blank? || attribute == false
end
alias :has_attribute? :attribute_present?
# Read a value from the document attributes. If the value does not exist
# it will return nil.
#
# @example Read an attribute.
# person.read_attribute(:title)
#
# @example Read an attribute (alternate syntax.)
# person[:title]
#
# @param [ String, Symbol ] name The name of the attribute to get.
#
# @return [ Object ] The value of the attribute.
#
# @since 1.0.0
def read_attribute(name)
attributes[name.to_s]
end
alias :[] :read_attribute
# Remove a value from the +Document+ attributes. If the value does not exist
# it will fail gracefully.
#
# @example Remove the attribute.
# person.remove_attribute(:title)
#
# @param [ String, Symbol ] name The name of the attribute to remove.
#
# @since 1.0.0
def remove_attribute(name)
_assigning do
access = name.to_s
attribute_will_change!(access)
attributes.delete(access)
end
end
# Override respond_to? so it responds properly for dynamic attributes.
#
# @example Does this object respond to the method?
# person.respond_to?(:title)
#
# @param [ Array ] *args The name of the method.
#
# @return [ true, false ] True if it does, false if not.
#
# @since 1.0.0
def respond_to?(*args)
(Mongoid.allow_dynamic_fields &&
attributes &&
attributes.has_key?(args.first.to_s)
) || super
end
# Write a single attribute to the document attribute hash. This will
# also fire the before and after update callbacks, and perform any
# necessary typecasting.
#
# @example Write the attribute.
# person.write_attribute(:title, "Mr.")
#
# @example Write the attribute (alternate syntax.)
# person[:title] = "Mr."
#
# @param [ String, Symbol ] name The name of the attribute to update.
# @param [ Object ] value The value to set for the attribute.
#
# @since 1.0.0
def write_attribute(name, value)
_assigning do
access = name.to_s
localized = fields[access].try(:localized?)
typed_value_for(access, value).tap do |value|
unless attributes[access] == value || attribute_changed?(access)
attribute_will_change!(access)
end
if localized
(attributes[access] ||= {}).merge!(value)
else
attributes[access] = value
end
end
end
end
alias :[]= :write_attribute
# Allows you to set all the attributes for a particular mass-assignment security role
# by passing in a hash of attributes with keys matching the attribute names
# (which again matches the column names) and the role name using the :as option.
# To bypass mass-assignment security you can use the :without_protection => true option.
#
# @example Assign the attributes.
# person.assign_attributes(:title => "Mr.")
#
# @example Assign the attributes (with a role).
# person.assign_attributes({ :title => "Mr." }, :as => :admin)
#
# @param [ Hash ] attrs The new attributes to set.
# @param [ Hash ] options Supported options: :without_protection, :as
#
# @since 2.2.1
def assign_attributes(attrs = nil, options = {})
_assigning do
process(attrs, options[:as] || :default, !options[:without_protection]) do |document|
document.identify if new? && id.blank?
end
end
end
# Writes the supplied attributes hash to the document. This will only
# overwrite existing attributes if they are present in the new +Hash+, all
# others will be preserved.
#
# @example Write the attributes.
# person.write_attributes(:title => "Mr.")
#
# @example Write the attributes (alternate syntax.)
# person.attributes = { :title => "Mr." }
#
# @param [ Hash ] attrs The new attributes to set.
# @param [ Boolean ] guard_protected_attributes False to skip mass assignment protection.
#
# @since 1.0.0
def write_attributes(attrs = nil, guard_protected_attributes = true)
assign_attributes(attrs, :without_protection => !guard_protected_attributes)
end
alias :attributes= :write_attributes
protected
# Set any missing default values in the attributes.
#
# @example Get the raw attributes after defaults have been applied.
# person.apply_defaults
#
# @return [ Hash ] The raw attributes.
#
# @since 2.0.0.rc.8
def apply_defaults
defaults.each do |name|
unless attributes.has_key?(name)
if field = fields[name]
attributes[name] = field.eval_default(self)
end
end
end
end
# Begin the assignment of attributes. While in this block embedded
# documents will not autosave themselves in order to allow the document to
# be in a valid state.
#
# @example Execute the assignment.
# _assigning do
# person.attributes = { :addresses => [ address ] }
# end
#
# @return [ Object ] The yielded value.
#
# @since 2.2.0
def _assigning
begin
Threaded.begin_assign
yield
ensure
Threaded.exit_assign
end
end
# Used for allowing accessor methods for dynamic attributes.
#
# @param [ String, Symbol ] name The name of the method.
# @param [ Array ] *args The arguments to the method.
def method_missing(name, *args)
attr = name.to_s
return super unless attributes.has_key?(attr.reader)
if attr.writer?
write_attribute(attr.reader, (args.size > 1) ? args : args.first)
else
read_attribute(attr.reader)
end
end
# Return the typecasted value for a field.
#
# @example Get the value typecasted.
# person.typed_value_for(:title, :sir)
#
# @param [ String, Symbol ] key The field name.
# @param [ Object ] value The uncast value.
#
# @return [ Object ] The cast value.
#
# @since 1.0.0
def typed_value_for(key, value)
fields.has_key?(key) ? fields[key].serialize(value) : value
end
module ClassMethods #:nodoc:
# Alias the provided name to the original field. This will provide an
# aliased getter, setter, existance check, and all dirty attribute
# methods.
#
# @example Alias the attribute.
# class Product
# include Mongoid::Document
# field :price, :type => Float
# alias_attribute :cost, :price
# end
#
# @param [ Symbol ] name The new name.
# @param [ Symbol ] original The original name.
#
# @since 2.3.0
def alias_attribute(name, original)
class_eval <<-RUBY
alias :#{name} :#{original}
alias :#{name}= :#{original}=
alias :#{name}? :#{original}?
RUBY
super
end
end
end
end