hydra_attribute 0.4.0.rc1 → 0.4.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +65 -35
- data/features/entity/create.feature +17 -0
- data/features/relation/query_methods/where.feature +46 -1
- data/features/step_definitions/model_steps.rb +13 -0
- data/gemfiles/3.1.gemfile.lock +13 -13
- data/gemfiles/3.2.gemfile.lock +13 -13
- data/lib/hydra_attribute/active_record/attribute_methods.rb +4 -5
- data/lib/hydra_attribute/active_record/relation/query_methods.rb +7 -1
- data/lib/hydra_attribute/builder.rb +3 -3
- data/lib/hydra_attribute/hydra_attribute.rb +4 -4
- data/lib/hydra_attribute/hydra_attribute_methods.rb +146 -5
- data/lib/hydra_attribute/hydra_methods.rb +420 -15
- data/lib/hydra_attribute/hydra_set_methods.rb +57 -1
- data/lib/hydra_attribute/hydra_value_methods.rb +7 -0
- data/lib/hydra_attribute/{memoize.rb → memoizable.rb} +1 -1
- data/lib/hydra_attribute/version.rb +1 -1
- data/lib/hydra_attribute.rb +1 -1
- data/spec/hydra_attribute/active_record/attribute_methods_spec.rb +17 -0
- data/spec/{hydra_attribute_methods_spec.rb → hydra_attribute/hydra_attribute_methods_spec.rb} +0 -0
- data/spec/{hydra_attribute_spec.rb → hydra_attribute/hydra_attribute_spec.rb} +0 -0
- data/spec/{hydra_methods_spec.rb → hydra_attribute/hydra_methods_spec.rb} +22 -23
- data/spec/{hydra_set_methods_spec.rb → hydra_attribute/hydra_set_methods_spec.rb} +0 -0
- data/spec/{hydra_set_spec.rb → hydra_attribute/hydra_set_spec.rb} +0 -0
- data/spec/{memoize_spec.rb → hydra_attribute/memoizable_spec.rb} +2 -2
- metadata +67 -30
@@ -1,7 +1,9 @@
|
|
1
1
|
module HydraAttribute
|
2
|
+
|
3
|
+
# @see HydraAttribute::HydraMethods::ClassMethods ClassMethods for additional documentation
|
2
4
|
module HydraMethods
|
3
5
|
extend ActiveSupport::Concern
|
4
|
-
extend
|
6
|
+
extend Memoizable
|
5
7
|
|
6
8
|
include HydraSetMethods
|
7
9
|
include HydraAttributeMethods
|
@@ -12,31 +14,234 @@ module HydraAttribute
|
|
12
14
|
end
|
13
15
|
|
14
16
|
module ClassMethods
|
15
|
-
extend
|
17
|
+
extend Memoizable
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
# Finds attribute for attribute sets. Returns all attributes if attribute set is undefined.
|
20
|
+
#
|
21
|
+
# @example attribute set exists
|
22
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
23
|
+
#
|
24
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
25
|
+
# hydra_set.hydra_attributes.create(name: 'title', backend_type: 'string')
|
26
|
+
# hydra_set.hydra_attributes.create(name: 'color', backend_type: 'string')
|
27
|
+
#
|
28
|
+
# Product.hydra_set_attributes('Default')
|
29
|
+
# # [<HydraAttribute::HydraAttribute name: "title">, <HydraAttribute::HydraAttribute name: "color">]
|
30
|
+
#
|
31
|
+
# @example attribute set doesn't exist
|
32
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
33
|
+
#
|
34
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
35
|
+
# hydra_set.hydra_attributes.create(name: 'title', backend_type: 'string')
|
36
|
+
#
|
37
|
+
# Product.hydra_set_attributes('FAKE')
|
38
|
+
# # [<HydraAttribute::HydraAttribute name: "name">, <HydraAttribute::HydraAttribute name: "title">]
|
39
|
+
#
|
40
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
41
|
+
# @return [ActiveRecord::Relation] contains preloaded attribute models
|
42
|
+
def hydra_set_attributes(hydra_set_identifier)
|
43
|
+
hydra_set = hydra_set(hydra_set_identifier)
|
19
44
|
hydra_set.nil? ? hydra_attributes : hydra_set.hydra_attributes
|
20
45
|
end
|
21
46
|
|
22
|
-
|
47
|
+
# Returns backend types for attributes which are assigned to passed attribute set.
|
48
|
+
# If attribute set doesn't exist, return all attribute backend types.
|
49
|
+
#
|
50
|
+
# @note This method is cacheable
|
51
|
+
#
|
52
|
+
# @example attribute set exists
|
53
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
54
|
+
#
|
55
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
56
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float')
|
57
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean')
|
58
|
+
#
|
59
|
+
# Product.hydra_set_attribute_backend_types('Default')
|
60
|
+
# # ["float", "boolean"]
|
61
|
+
#
|
62
|
+
# @example attribute set doesn't exist
|
63
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
64
|
+
#
|
65
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
66
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float')
|
67
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean')
|
68
|
+
#
|
69
|
+
# Product.hydra_set_attribute_backend_types('FAKE')
|
70
|
+
# # ["string", float", "boolean"]
|
71
|
+
#
|
72
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
73
|
+
# @return [Array<String>] contains unique list of attribute backend types
|
74
|
+
def hydra_set_attribute_backend_types(hydra_set_identifier)
|
75
|
+
hydra_set_attributes(hydra_set_identifier).map(&:backend_type).uniq
|
76
|
+
end
|
77
|
+
hydra_memoize :hydra_set_attribute_backend_types
|
78
|
+
|
79
|
+
# @!method hydra_set_attribute_ids(hydra_set_identifier)
|
80
|
+
# Returns ids for attributes which are assigned to passed attribute set.
|
81
|
+
# If attribute set doesn't exist, return all attribute ids.
|
82
|
+
#
|
83
|
+
# @note This method is cacheable
|
84
|
+
#
|
85
|
+
# @example attribute set exists
|
86
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string') # 1
|
87
|
+
#
|
88
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
89
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float') # 2
|
90
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean') # 3
|
91
|
+
#
|
92
|
+
# Product.hydra_set_attribute_ids('Default')
|
93
|
+
# # [2, 3]
|
94
|
+
#
|
95
|
+
# @example attribute set doesn't exist
|
96
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string') # 1
|
97
|
+
#
|
98
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
99
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float') # 2
|
100
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean') # 3
|
101
|
+
#
|
102
|
+
# Product.hydra_set_attribute_ids('FAKE')
|
103
|
+
# # [1, 2, 3]
|
104
|
+
#
|
105
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
106
|
+
# @return [Array<Fixnum>] contains unique list of attribute ids
|
107
|
+
|
108
|
+
# @!method hydra_set_attribute_names(hydra_set_identifier)
|
109
|
+
# Returns names for attributes which are assigned to passed attribute set.
|
110
|
+
# If attribute set doesn't exist, return all attribute names.
|
111
|
+
#
|
112
|
+
# @note This method is cacheable
|
113
|
+
#
|
114
|
+
# @example attribute set exists
|
115
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
116
|
+
#
|
117
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
118
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float')
|
119
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean')
|
120
|
+
#
|
121
|
+
# Product.hydra_set_attribute_names('Default')
|
122
|
+
# # ["price", "active"]
|
123
|
+
#
|
124
|
+
# @example attribute set doesn't exist
|
125
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
126
|
+
#
|
127
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
128
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float')
|
129
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean')
|
130
|
+
#
|
131
|
+
# Product.hydra_set_attribute_names('FAKE')
|
132
|
+
# # ["name", "price", "active"]
|
133
|
+
#
|
134
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
135
|
+
# @return [Array<String>] contains unique list of attribute names
|
136
|
+
%w(id name).each do |prefix|
|
23
137
|
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
24
|
-
def hydra_set_attribute_#{prefix}s(
|
25
|
-
hydra_set_attributes(
|
138
|
+
def hydra_set_attribute_#{prefix}s(hydra_set_identifier)
|
139
|
+
hydra_set_attributes(hydra_set_identifier).map(&:#{prefix})
|
26
140
|
end
|
27
141
|
hydra_memoize :hydra_set_attribute_#{prefix}s
|
28
142
|
EOS
|
29
143
|
end
|
30
144
|
|
31
|
-
|
32
|
-
|
145
|
+
# Groups attributes for attribute set by backend type.
|
146
|
+
# If attribute set doesn't exist, group all attributes.
|
147
|
+
#
|
148
|
+
# @note This method is cacheable
|
149
|
+
#
|
150
|
+
# @example attribute set exists
|
151
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
152
|
+
#
|
153
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
154
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float')
|
155
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean')
|
156
|
+
#
|
157
|
+
# Product.hydra_set_attributes_by_backend_type('Default')
|
158
|
+
# # {
|
159
|
+
# # "float"=>[<HydraAttribute::HydraAttribute name: 'price'>],
|
160
|
+
# # "boolean"=>[<HydraAttribute::HydraAttribute name: 'active'>]
|
161
|
+
# # }
|
162
|
+
#
|
163
|
+
# @example attribute set doesn't exist
|
164
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
165
|
+
#
|
166
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
167
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float')
|
168
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean')
|
169
|
+
#
|
170
|
+
# Product.hydra_set_attributes_by_backend_type('FAKE')
|
171
|
+
# # {
|
172
|
+
# # "string"=>[<HydraAttribute::HydraAttribute name: 'name'>],
|
173
|
+
# # "float"=>[<HydraAttribute::HydraAttribute name: 'price'>],
|
174
|
+
# # "boolean"=>[<HydraAttribute::HydraAttribute name: 'active'>]
|
175
|
+
# # }
|
176
|
+
#
|
177
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
178
|
+
# @return [Hash{String => Array<HydraAttribute::HydraAttribute>}] contains grouped attributes by their backend types
|
179
|
+
def hydra_set_attributes_by_backend_type(hydra_set_identifier)
|
180
|
+
hydra_set_attributes(hydra_set_identifier).group_by(&:backend_type)
|
33
181
|
end
|
34
182
|
hydra_memoize :hydra_set_attributes_by_backend_type
|
35
183
|
|
184
|
+
# @!method hydra_set_attribute_ids_by_backend_type(hydra_set_identifier)
|
185
|
+
# Groups attribute ids for attribute set by backend type.
|
186
|
+
# If attribute set doesn't exist, group all attribute ids.
|
187
|
+
#
|
188
|
+
# @note This method is cacheable
|
189
|
+
#
|
190
|
+
# @example attribute set exists
|
191
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string') # 1
|
192
|
+
#
|
193
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
194
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float') # 2
|
195
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean') # 3
|
196
|
+
#
|
197
|
+
# Product.hydra_set_attribute_ids_by_backend_type('Default')
|
198
|
+
# # {"float"=>[2], "boolean"=>[3]}
|
199
|
+
#
|
200
|
+
# @example attribute set doesn't exist
|
201
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string') # 1
|
202
|
+
#
|
203
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
204
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float') # 2
|
205
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean') # 3
|
206
|
+
#
|
207
|
+
# Product.hydra_set_attribute_ids_by_backend_type('FAKE')
|
208
|
+
# # {"string"=>[1], "float"=>[2], "boolean"=>[3]}
|
209
|
+
#
|
210
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
211
|
+
# @return [Hash{String => Array<Fixnum>}] contains grouped attribute ids by their backend types
|
212
|
+
|
213
|
+
# @!method hydra_set_attribute_names_by_backend_type(hydra_set_identifier)
|
214
|
+
# Groups attribute names for attribute set by backend type.
|
215
|
+
# If attribute set doesn't exist, group all attribute names.
|
216
|
+
#
|
217
|
+
# @note This method is cacheable
|
218
|
+
#
|
219
|
+
# @example attribute set exists
|
220
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
221
|
+
#
|
222
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
223
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float')
|
224
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean')
|
225
|
+
#
|
226
|
+
# Product.hydra_set_attribute_names_by_backend_type('Default')
|
227
|
+
# # {"float"=>["price"], "boolean"=>["active"]}
|
228
|
+
#
|
229
|
+
# @example attribute set doesn't exist
|
230
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
231
|
+
#
|
232
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
233
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float')
|
234
|
+
# hydra_set.hydra_attributes.create(name: 'active', backend_type: 'boolean')
|
235
|
+
#
|
236
|
+
# Product.hydra_set_attribute_names_by_backend_type('FAKE')
|
237
|
+
# # {"string"=>["name"], "float"=>["price"], "boolean"=>["active"]}
|
238
|
+
#
|
239
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
240
|
+
# @return [Hash{String => Array<String>}] contains grouped attribute names by their backend types
|
36
241
|
%w(id name).each do |prefix|
|
37
242
|
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
38
|
-
def hydra_set_attribute_#{prefix}s_by_backend_type(
|
39
|
-
hydra_set_attributes(
|
243
|
+
def hydra_set_attribute_#{prefix}s_by_backend_type(hydra_set_identifier)
|
244
|
+
hydra_set_attributes(hydra_set_identifier).each_with_object({}) do |hydra_attribute, object|
|
40
245
|
object[hydra_attribute.backend_type] ||= []
|
41
246
|
object[hydra_attribute.backend_type] << hydra_attribute.#{prefix}
|
42
247
|
end
|
@@ -45,20 +250,141 @@ module HydraAttribute
|
|
45
250
|
EOS
|
46
251
|
end
|
47
252
|
|
48
|
-
|
49
|
-
|
253
|
+
# Returns attributes for attribute set and backend type.
|
254
|
+
# If attribute set doesn't exist, returns all attributes for backend type.
|
255
|
+
#
|
256
|
+
# @example attribute set exists
|
257
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
258
|
+
#
|
259
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
260
|
+
# hydra_sets.hydra_attributes.create(name: 'title', backend_type: 'string')
|
261
|
+
# hydra_sets.hydra_attributes.create(name: 'price', backend_type: 'float')
|
262
|
+
#
|
263
|
+
# Product.hydra_set_attributes_for_backend_type('Default', 'float')
|
264
|
+
# # [<HydraAttribute::HydraAttribute name: "price">]
|
265
|
+
#
|
266
|
+
# @example attribute set doesn't exists
|
267
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
268
|
+
#
|
269
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
270
|
+
# hydra_sets.hydra_attributes.create(name: 'title', backend_type: 'string')
|
271
|
+
# hydra_sets.hydra_attributes.create(name: 'price', backend_type: 'float')
|
272
|
+
#
|
273
|
+
# Product.hydra_set_attributes_for_backend_type('FAKE', 'string')
|
274
|
+
# # [<HydraAttribute::HydraAttribute name: "name">, <HydraAttribute::HydraAttribute name: "title">]
|
275
|
+
#
|
276
|
+
# @example attributes doesn't exist for backend type
|
277
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
278
|
+
#
|
279
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
280
|
+
# hydra_sets.hydra_attributes.create(name: 'title', backend_type: 'string')
|
281
|
+
# hydra_sets.hydra_attributes.create(name: 'price', backend_type: 'float')
|
282
|
+
#
|
283
|
+
# Product.hydra_set_attributes_for_backend_type('Default', 'boolean')
|
284
|
+
# # []
|
285
|
+
#
|
286
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
287
|
+
# @param backend_type [String] see HydraAttribute::SUPPORTED_BACKEND_TYPES
|
288
|
+
# @return [Array] contains attribute models
|
289
|
+
def hydra_set_attributes_for_backend_type(hydra_set_identifier, backend_type)
|
290
|
+
hydra_attributes = hydra_set_attributes_by_backend_type(hydra_set_identifier)[backend_type]
|
50
291
|
hydra_attributes.nil? ? [] : hydra_attributes
|
51
292
|
end
|
52
293
|
|
294
|
+
# @!method hydra_set_attribute_ids_for_backend_type(hydra_set_identifier, backend_type)
|
295
|
+
# Returns attribute ids for attribute set and backend type.
|
296
|
+
# If attribute set doesn't exist, returns all attribute ids for backend type.
|
297
|
+
#
|
298
|
+
# @example attribute set exists
|
299
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string') # 1
|
300
|
+
#
|
301
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
302
|
+
# hydra_sets.hydra_attributes.create(name: 'title', backend_type: 'string') # 2
|
303
|
+
# hydra_sets.hydra_attributes.create(name: 'price', backend_type: 'float') # 3
|
304
|
+
#
|
305
|
+
# Product.hydra_set_attribute_ids_for_backend_type('Default', 'float') # [2]
|
306
|
+
#
|
307
|
+
# @example attribute set doesn't exists
|
308
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string') # 1
|
309
|
+
#
|
310
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
311
|
+
# hydra_sets.hydra_attributes.create(name: 'title', backend_type: 'string') # 2
|
312
|
+
# hydra_sets.hydra_attributes.create(name: 'price', backend_type: 'float') # 3
|
313
|
+
#
|
314
|
+
# Product.hydra_set_attribute_ids_for_backend_type('FAKE', 'string') # [1, 2]
|
315
|
+
#
|
316
|
+
# @example attributes doesn't exist for backend type
|
317
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string') # 1
|
318
|
+
#
|
319
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
320
|
+
# hydra_sets.hydra_attributes.create(name: 'title', backend_type: 'string') # 2
|
321
|
+
# hydra_sets.hydra_attributes.create(name: 'price', backend_type: 'float') # 3
|
322
|
+
#
|
323
|
+
# Product.hydra_set_attribute_ids_for_backend_type('Default', 'boolean') # []
|
324
|
+
#
|
325
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
326
|
+
# @param backend_type [String] see HydraAttribute::SUPPORTED_BACKEND_TYPES
|
327
|
+
# @return [Array<Fixnum>] contains attribute ids
|
328
|
+
|
329
|
+
# @!method hydra_set_attribute_names_for_backend_type(hydra_set_identifier, backend_type)
|
330
|
+
# Returns attribute names for attribute set and backend type.
|
331
|
+
# If attribute set doesn't exist, returns all attribute names for backend type.
|
332
|
+
#
|
333
|
+
# @example attribute set exists
|
334
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
335
|
+
#
|
336
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
337
|
+
# hydra_sets.hydra_attributes.create(name: 'title', backend_type: 'string')
|
338
|
+
# hydra_sets.hydra_attributes.create(name: 'price', backend_type: 'float')
|
339
|
+
#
|
340
|
+
# Product.hydra_set_attribute_names_for_backend_type('Default', 'float') # ["price"]
|
341
|
+
#
|
342
|
+
# @example attribute set doesn't exists
|
343
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
344
|
+
#
|
345
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
346
|
+
# hydra_sets.hydra_attributes.create(name: 'title', backend_type: 'string')
|
347
|
+
# hydra_sets.hydra_attributes.create(name: 'price', backend_type: 'float')
|
348
|
+
#
|
349
|
+
# Product.hydra_set_attribute_names_for_backend_type('FAKE', 'string') # ["name", "title"]
|
350
|
+
#
|
351
|
+
# @example attributes doesn't exist for backend type
|
352
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
353
|
+
#
|
354
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
355
|
+
# hydra_sets.hydra_attributes.create(name: 'title', backend_type: 'string')
|
356
|
+
# hydra_sets.hydra_attributes.create(name: 'price', backend_type: 'float')
|
357
|
+
#
|
358
|
+
# Product.hydra_set_attribute_names_for_backend_type('Default', 'boolean') # []
|
359
|
+
#
|
360
|
+
# @param hydra_set_identifier [Fixnum, String] id or name of attribute set
|
361
|
+
# @param backend_type [String] see HydraAttribute::SUPPORTED_BACKEND_TYPES
|
362
|
+
# @return [Array<String>] contains attribute names
|
53
363
|
%w(id name).each do |prefix|
|
54
364
|
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
55
|
-
def hydra_set_attribute_#{prefix}s_for_backend_type(
|
56
|
-
values = hydra_set_attribute_#{prefix}s_by_backend_type(
|
365
|
+
def hydra_set_attribute_#{prefix}s_for_backend_type(hydra_set_identifier, backend_type)
|
366
|
+
values = hydra_set_attribute_#{prefix}s_by_backend_type(hydra_set_identifier)[backend_type]
|
57
367
|
values.nil? ? [] : values
|
58
368
|
end
|
59
369
|
EOS
|
60
370
|
end
|
61
371
|
|
372
|
+
# Clear cache for the following methods:
|
373
|
+
# * hydra_set_attributes(hydra_set_identifier)
|
374
|
+
# * hydra_set_attribute_ids(hydra_set_identifier)
|
375
|
+
# * hydra_set_attribute_names(hydra_set_identifier)
|
376
|
+
# * hydra_set_attribute_backend_types(hydra_set_identifier)
|
377
|
+
# * hydra_set_attributes_by_backend_type(hydra_set_identifier)
|
378
|
+
# * hydra_set_attribute_ids_by_backend_type(hydra_set_identifier)
|
379
|
+
# * hydra_set_attribute_names_by_backend_type(hydra_set_identifier)
|
380
|
+
#
|
381
|
+
# Also calls the following methods:
|
382
|
+
# * clear_hydra_set_cache!
|
383
|
+
# * clear_hydra_attribute_cache!
|
384
|
+
# * clear_hydra_value_cache!
|
385
|
+
#
|
386
|
+
# @note This method should not be called manually. It used for hydra_attribute gem engine.
|
387
|
+
# @return [NilClass]
|
62
388
|
def clear_hydra_method_cache!
|
63
389
|
clear_hydra_set_cache!
|
64
390
|
clear_hydra_attribute_cache!
|
@@ -78,6 +404,22 @@ module HydraAttribute
|
|
78
404
|
end
|
79
405
|
end
|
80
406
|
|
407
|
+
# Returns hash with attribute names and values based on attribute set.
|
408
|
+
#
|
409
|
+
# @example
|
410
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
411
|
+
#
|
412
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
413
|
+
# hydra_set.hydra_attributes.create(name: 'title', backend_type: 'string')
|
414
|
+
# hydra_set.hydra_attributes.create(name: 'color', backend_type: 'string')
|
415
|
+
#
|
416
|
+
# product = Product.new
|
417
|
+
# product.hydra_attributes # {"name"=>nil, "title"=>nil, "color"=>nil}
|
418
|
+
#
|
419
|
+
# product.hydra_set_id = hydra_set.id
|
420
|
+
# product.hydra_attributes # {"title"=>nil, "color"=>nil}
|
421
|
+
#
|
422
|
+
# @return [Hash] contains attribute names and values
|
81
423
|
def hydra_attributes
|
82
424
|
hydra_value_models.inject({}) do |hydra_attributes, model|
|
83
425
|
hydra_attributes[model.hydra_attribute_name] = model.value
|
@@ -85,6 +427,59 @@ module HydraAttribute
|
|
85
427
|
end
|
86
428
|
end
|
87
429
|
|
430
|
+
# @!method hydra_attribute_ids
|
431
|
+
# Returns attribute ids
|
432
|
+
#
|
433
|
+
# @example
|
434
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string') # 1
|
435
|
+
#
|
436
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
437
|
+
# hydra_set.hydra_attributes.create(name: 'title', backend_type: 'string') # 2
|
438
|
+
# hydra_set.hydra_attributes.create(name: 'color', backend_type: 'string') # 3
|
439
|
+
#
|
440
|
+
# product = Product.new
|
441
|
+
# product.hydra_attribute_ids # [1, 2, 3]
|
442
|
+
#
|
443
|
+
# product.hydra_set_id = hydra_set.id
|
444
|
+
# product.hydra_attribute_ids # [2, 3]
|
445
|
+
#
|
446
|
+
# @return [Array] contains attribute ids
|
447
|
+
|
448
|
+
# @!method hydra_attribute_names
|
449
|
+
# Returns attribute names
|
450
|
+
#
|
451
|
+
# @example
|
452
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
453
|
+
#
|
454
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
455
|
+
# hydra_set.hydra_attributes.create(name: 'title', backend_type: 'string')
|
456
|
+
# hydra_set.hydra_attributes.create(name: 'color', backend_type: 'string')
|
457
|
+
#
|
458
|
+
# product = Product.new
|
459
|
+
# product.hydra_attribute_names # ["name", "title", "color"]
|
460
|
+
#
|
461
|
+
# product.hydra_set_id = hydra_set.id
|
462
|
+
# product.hydra_attribute_names # ["title", "color"]
|
463
|
+
#
|
464
|
+
# @return [Array] contains attribute names
|
465
|
+
|
466
|
+
# @!method hydra_attribute_backend_types
|
467
|
+
# Returns attribute backend types
|
468
|
+
#
|
469
|
+
# @example
|
470
|
+
# Product.hydra_attributes.create(name: 'name', backend_type: 'string')
|
471
|
+
#
|
472
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
473
|
+
# hydra_set.hydra_attributes.create(name: 'price', backend_type: 'float')
|
474
|
+
# hydra_set.hydra_attributes.create(name: 'total', backend_type: 'integer')
|
475
|
+
#
|
476
|
+
# product = Product.new
|
477
|
+
# product.hydra_attribute_backend_types # ["string", "float", "integer"]
|
478
|
+
#
|
479
|
+
# product.hydra_set_id = hydra_set.id
|
480
|
+
# product.hydra_attribute_backend_types # ["float", "integer"]
|
481
|
+
#
|
482
|
+
# @return [Array] contains attribute backend types
|
88
483
|
%w(ids names backend_types).each do |prefix|
|
89
484
|
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
90
485
|
def hydra_attribute_#{prefix}
|
@@ -93,6 +488,11 @@ module HydraAttribute
|
|
93
488
|
EOS
|
94
489
|
end
|
95
490
|
|
491
|
+
# Finds attribute representation as a model by identifier
|
492
|
+
#
|
493
|
+
# @note This method is cacheable
|
494
|
+
# @param identifier [Fixnum, String] id or name of attribute
|
495
|
+
# @return [ActiveRecord::Base]
|
96
496
|
def hydra_value_model(identifier)
|
97
497
|
hydra_attribute = self.class.hydra_attribute(identifier)
|
98
498
|
if hydra_attribute
|
@@ -102,6 +502,10 @@ module HydraAttribute
|
|
102
502
|
end
|
103
503
|
hydra_memoize :hydra_value_model
|
104
504
|
|
505
|
+
# Returns all attributes as models
|
506
|
+
#
|
507
|
+
# @note This method is cacheable
|
508
|
+
# @return [Array] contains values models
|
105
509
|
def hydra_value_models
|
106
510
|
self.class.hydra_set_attribute_backend_types(hydra_set_id).inject([]) do |models, backend_type|
|
107
511
|
models + hydra_value_association(backend_type).all_models
|
@@ -110,6 +514,7 @@ module HydraAttribute
|
|
110
514
|
hydra_memoize :hydra_value_models
|
111
515
|
|
112
516
|
private
|
517
|
+
# Resets locked attributes after setting new hydra_set_id
|
113
518
|
def write_attribute_with_hydra_set_id(attr_name, value)
|
114
519
|
if attr_name.to_s == 'hydra_set_id'
|
115
520
|
self.class.hydra_attribute_backend_types.each do |backend_type|
|
@@ -1,18 +1,47 @@
|
|
1
1
|
module HydraAttribute
|
2
|
+
# This error is raised when called method for attribute which doesn't exist in current hydra set
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
# Product.hydra_attributes.create(name: 'price', backend_type: 'float')
|
6
|
+
# Product.hydra_attributes.create(name: 'title', backend_type: 'string')
|
7
|
+
#
|
8
|
+
# hydra_set = Product.hydra_sets.create(name: 'Default')
|
9
|
+
# hydra_set.hydra_attributes = [Product.hydra_attribute('title')]
|
10
|
+
#
|
11
|
+
# product = Product.new(hydra_set_id: hydra_set.id)
|
12
|
+
# product.title = 'Toy' # ok
|
13
|
+
# product.price = 2.50 # raise HydraAttribute::MissingAttributeInHydraSetError
|
2
14
|
class MissingAttributeInHydraSetError < NoMethodError
|
3
15
|
end
|
4
16
|
|
17
|
+
# @see HydraAttribute::HydraSetMethods::ClassMethods ClassMethods for documentation
|
5
18
|
module HydraSetMethods
|
6
19
|
extend ActiveSupport::Concern
|
7
20
|
|
8
21
|
module ClassMethods
|
9
|
-
extend
|
22
|
+
extend Memoizable
|
10
23
|
|
24
|
+
# Returns attribute sets for current entity.
|
25
|
+
#
|
26
|
+
# @note This method is cacheable, therefore just one request to database is used
|
27
|
+
# @example
|
28
|
+
# Product.hydra_sets # [<HydraAttribute::HydraSet>, ...]
|
29
|
+
# Product.hydra_sets.create(name: 'Default') # create attribute set
|
30
|
+
# Product.hydra_sets.each(&:destroy) # remove all attribute sets
|
31
|
+
# @return [ActiveRecord::Relation] contains preloaded attribute sets
|
11
32
|
def hydra_sets
|
12
33
|
HydraSet.where(entity_type: base_class.model_name)
|
13
34
|
end
|
14
35
|
hydra_memoize :hydra_sets
|
15
36
|
|
37
|
+
# Finds attribute set by name or id
|
38
|
+
#
|
39
|
+
# @note This method is cacheable, therefore it's better to use it instead of manually searching.
|
40
|
+
# @example
|
41
|
+
# Product.hydra_set(10) # or
|
42
|
+
# Product.hydra_set('Default')
|
43
|
+
# @param identifier [Fixnum, String] id or name of attribute set
|
44
|
+
# @return [HydraAttribute::HydraSet, NilClass] attribute set model or +nil+
|
16
45
|
def hydra_set(identifier)
|
17
46
|
hydra_sets.find do |hydra_set|
|
18
47
|
hydra_set.id == identifier || hydra_set.name == identifier
|
@@ -20,6 +49,25 @@ module HydraAttribute
|
|
20
49
|
end
|
21
50
|
hydra_memoize :hydra_set
|
22
51
|
|
52
|
+
# @!method hydra_set_ids
|
53
|
+
# Returns all attribute set ids
|
54
|
+
#
|
55
|
+
# @note This method is cacheable.
|
56
|
+
# @example
|
57
|
+
# Product.hydra_sets.create(name: 'First') # 1
|
58
|
+
# Product.hydra_sets.create(name: 'Second') # 2
|
59
|
+
# Product.hydra_set_ids # [1, 2]
|
60
|
+
# @return [Array<Fixnum>] contains attribute set ids
|
61
|
+
|
62
|
+
# @!method hydra_set_names
|
63
|
+
# Returns all attribute set names
|
64
|
+
#
|
65
|
+
# @note This method is cacheable.
|
66
|
+
# @example
|
67
|
+
# Product.hydra_sets.create(name: 'First') # 1
|
68
|
+
# Product.hydra_sets.create(name: 'Second') # 2
|
69
|
+
# Product.hydra_set_names # ["First", "Second"]
|
70
|
+
# @return [Array<Fixnum>] contains attribute set names
|
23
71
|
%w(id name).each do |prefix|
|
24
72
|
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
25
73
|
def hydra_set_#{prefix}s
|
@@ -29,6 +77,14 @@ module HydraAttribute
|
|
29
77
|
EOS
|
30
78
|
end
|
31
79
|
|
80
|
+
# Clear cache for the following methods:
|
81
|
+
# * hydra_sets
|
82
|
+
# * hydra_set(identifier)
|
83
|
+
# * hydra_set_ids
|
84
|
+
# * hydra_set_names
|
85
|
+
#
|
86
|
+
# @note This method should not be called manually. It used for hydra_attribute gem engine.
|
87
|
+
# @return [NilClass]
|
32
88
|
def clear_hydra_set_cache!
|
33
89
|
[:@hydra_sets, :@hydra_set, :@hydra_set_ids, :@hydra_set_names].each do |variable|
|
34
90
|
remove_instance_variable(variable) if instance_variable_defined?(variable)
|
@@ -3,10 +3,17 @@ module HydraAttribute
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
+
# Clear cache
|
7
|
+
#
|
8
|
+
# @return [NilClass]
|
6
9
|
def clear_hydra_value_cache!
|
7
10
|
end
|
8
11
|
end
|
9
12
|
|
13
|
+
# Returns the association instance for the given backend type attribute name
|
14
|
+
#
|
15
|
+
# @param backend_type [String] one of the HydraAttribute::SUPPORTED_BACKEND_TYPES values
|
16
|
+
# @return [ActiveRecord::Associations::Association]
|
10
17
|
def hydra_value_association(backend_type)
|
11
18
|
association(::HydraAttribute::AssociationBuilder.association_name(backend_type))
|
12
19
|
end
|
data/lib/hydra_attribute.rb
CHANGED
@@ -18,7 +18,7 @@ require 'hydra_attribute/configuration'
|
|
18
18
|
require 'hydra_attribute/association_builder'
|
19
19
|
require 'hydra_attribute/builder'
|
20
20
|
require 'hydra_attribute/migrator'
|
21
|
-
require 'hydra_attribute/
|
21
|
+
require 'hydra_attribute/memoizable'
|
22
22
|
require 'hydra_attribute/hydra_attribute'
|
23
23
|
require 'hydra_attribute/hydra_set'
|
24
24
|
require 'hydra_attribute/entity_callbacks'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HydraAttribute::ActiveRecord::AttributeMethods do
|
4
|
+
describe '::ClassMethods' do
|
5
|
+
describe '#define_hydra_attribute_method' do
|
6
|
+
it 'should throw MissingAttributeInHydraSetError if we call generated hydra attribute method which does not exist in hydra set' do
|
7
|
+
Product.hydra_attributes.create(name: 'color', backend_type: 'string')
|
8
|
+
hydra_set = Product.hydra_sets.create(name: 'Default')
|
9
|
+
product = Product.new(hydra_set_id: hydra_set.id)
|
10
|
+
|
11
|
+
lambda do
|
12
|
+
product.color
|
13
|
+
end.should raise_error(HydraAttribute::MissingAttributeInHydraSetError, %(Hydra attribute "color" does not exist in hydra set "Default"))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/{hydra_attribute_methods_spec.rb → hydra_attribute/hydra_attribute_methods_spec.rb}
RENAMED
File without changes
|
File without changes
|