hydra_attribute 0.4.0.rc1 → 0.4.0.rc2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,15 +1,41 @@
1
1
  module HydraAttribute
2
+
3
+ # @see HydraAttribute::HydraAttributeMethods::ClassMethods ClassMethods for documentation.
2
4
  module HydraAttributeMethods
3
5
  extend ActiveSupport::Concern
4
6
 
5
7
  module ClassMethods
6
- extend Memoize
8
+ extend Memoizable
7
9
 
10
+ # Returns prepared +ActiveRecord::Relation+ object with preloaded attributes for current entity.
11
+ #
12
+ # @note This method is cacheable, so just one request per entity will be sent to the database.
13
+ # @example
14
+ # Product.hydra_attributes # ActiveRecord::Relation
15
+ # Product.hydra_attributes.map(&:name) # ["title", "color", ...]
16
+ # Product.hydra_attributes.create(name: 'title', backend_type: 'string') # Create and return new attribute
17
+ # Product.hydra_attributes.each(&:destroy) # Remove all attributes
18
+ # @return [ActiveRecord::Relation] contains attributes for current entity
8
19
  def hydra_attributes
9
20
  HydraAttribute.where(entity_type: base_class.model_name)
10
21
  end
11
22
  hydra_memoize :hydra_attributes
12
23
 
24
+ # Finds <tt>HydraAttribute::HydraAttribute</tt> model by +id+ or +name+. Returns +nil+ for unknown attribute.
25
+ #
26
+ # @note This method is cacheable per +identifier+. It's better to use it instead of manually searching from <tt>hydra_attributes</tt> collection.
27
+ # @example
28
+ # Product.hydra_attribute('name') # HydraAttribute::HydraAttribute
29
+ # Product.hydra_attribute('name') # Returns HydraAttribute::HydraAttribute from cache
30
+ # Product.hydra_attribute(10) # HydraAttribute::HydraAttribute
31
+ # Product.hydra_attribute('FAKE') # nil
32
+ #
33
+ # # It's better to use this method
34
+ # Product.hydra_attribute('name')
35
+ # # instead of manually searching
36
+ # Product.hydra_attributes.detect { |attr| attr.name == 'name' || attr.id == 'id' }
37
+ # @param identifier [String, Integer] accepts attribute name or id
38
+ # @return [HydraAttribute::HydraAttribute, NilClass] attribute model or nil if wasn't found
13
39
  def hydra_attribute(identifier)
14
40
  hydra_attributes.find do |hydra_attribute|
15
41
  hydra_attribute.id == identifier || hydra_attribute.name == identifier
@@ -17,11 +43,45 @@ module HydraAttribute
17
43
  end
18
44
  hydra_memoize :hydra_attribute
19
45
 
46
+ # Returns unique array of attribute backend types.
47
+ #
48
+ # @note This method is cacheable therefore, the definition of values is carried out only once.
49
+ # @example
50
+ # Product.hydra_attributes.create(name: 'one', backend_type: 'string')
51
+ # Product.hydra_attributes.create(name: 'two', backend_type: 'string')
52
+ # Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
53
+ #
54
+ # Product.hydra_attribute_backend_types # ["string", "integer"]
55
+ # @return [Array<String>}] contains backend types
20
56
  def hydra_attribute_backend_types
21
57
  hydra_attributes.map(&:backend_type).uniq
22
58
  end
23
59
  hydra_memoize :hydra_attribute_backend_types
24
60
 
61
+ # @!method hydra_attribute_ids
62
+ # Returns array of attribute ids.
63
+ #
64
+ # @note This method is cacheable therefore, the definition of values is carried out only once.
65
+ # @example
66
+ # Product.hydra_attributes.create(name: 'one', backend_type: 'string') # 1
67
+ # Product.hydra_attributes.create(name: 'two', backend_type: 'string') # 2
68
+ # Product.hydra_attributes.create(name: 'three', backend_type: 'integer') # 3
69
+ #
70
+ # Product.hydra_attribute_ids # [1, 2, 3]
71
+ # @return [Array<Fixnum>] contains attribute ids
72
+
73
+ ##
74
+ # @!method hydra_attribute_names
75
+ # Returns array of attribute names.
76
+ #
77
+ # @note This method is cacheable therefore, the definition of values is carried out only once.
78
+ # @example
79
+ # Product.hydra_attributes.create(name: 'one', backend_type: 'string')
80
+ # Product.hydra_attributes.create(name: 'two', backend_type: 'string')
81
+ # Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
82
+ #
83
+ # Product.hydra_attribute_names # ["one", "two", "three"]
84
+ # @return [Array<String>] contains attribute names
25
85
  %w(id name).each do |prefix|
26
86
  module_eval <<-EOS, __FILE__, __LINE__ + 1
27
87
  def hydra_attribute_#{prefix}s
@@ -31,11 +91,43 @@ module HydraAttribute
31
91
  EOS
32
92
  end
33
93
 
94
+ # Groups current attributes by backend type.
95
+ #
96
+ # @note This method is cacheable therefore, the group operation is carried out only once.
97
+ # @example
98
+ # Product.hydra_attributes_by_backend_type
99
+ # # {"string" => [...], "integer" => [...]}
100
+ # @return [Hash{String => Array<HydraAttribute::HydraAttribute>}] contains grouped attribute models by backend type
34
101
  def hydra_attributes_by_backend_type
35
102
  hydra_attributes.group_by(&:backend_type)
36
103
  end
37
104
  hydra_memoize :hydra_attributes_by_backend_type
38
105
 
106
+ # @!method hydra_attribute_ids_by_backend_type
107
+ # Group attribute ids by backend type.
108
+ #
109
+ # @note This method is cacheable therefore, the group operation is carried out only once.
110
+ # @example
111
+ # Product.hydra_attributes.create(name: 'one', backend_type: 'string') # 1
112
+ # Product.hydra_attributes.create(name: 'two', backend_type: 'string') # 2
113
+ # Product.hydra_attributes.create(name: 'three', backend_type: 'integer') # 3
114
+ #
115
+ # Product.hydra_attribute_ids_by_backend_type
116
+ # # {"string" => [1, 2], "integer" => [3]}
117
+ # @return [Hash{String => Array<Fixnum>}] contains grouped attribute ids by backend type
118
+
119
+ # @!method hydra_attribute_names_by_backend_type
120
+ # Group attribute names by backend type.
121
+ #
122
+ # @note This method is cacheable therefore, the group operation is carried out only once.
123
+ # @example
124
+ # Product.hydra_attributes.create(name: 'one', backend_type: 'string')
125
+ # Product.hydra_attributes.create(name: 'two', backend_type: 'string')
126
+ # Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
127
+ #
128
+ # Product.hydra_attribute_names_by_backend_type
129
+ # # {"string" => ["one", "two"], "integer" => ["three"]}
130
+ # @return [Hash{String => Array<String>}] contains grouped attribute names by backend type
39
131
  %w(id name).each do |prefix|
40
132
  module_eval <<-EOS, __FILE__, __LINE__ + 1
41
133
  def hydra_attribute_#{prefix}s_by_backend_type
@@ -48,11 +140,52 @@ module HydraAttribute
48
140
  EOS
49
141
  end
50
142
 
143
+ # Returns array of attributes for passed backend type.
144
+ #
145
+ # @note If no attributes, returns blank array.
146
+ # @example
147
+ # Product.hydra_attributes.create(name: 'one', backend_type: 'string')
148
+ # Product.hydra_attributes.create(name: 'two', backend_type: 'string')
149
+ # Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
150
+ #
151
+ # Product.hydra_attributes_for_backend_type('string')
152
+ # # [<HydraAttribute::HydraAttribute id: 1, name: "one", ...>, <HydraAttribute::HydraAttribute id: 2, name: "two", ...>]
153
+ #
154
+ # Product.hydra_attributes_for_backend_type('float') # []
155
+ # @param backend_type [String] backend type of attributes
156
+ # @return [Array<HydraAttribute::HydraAttribute>] contains attribute models for passed backend type
51
157
  def hydra_attributes_for_backend_type(backend_type)
52
158
  hydra_attributes = hydra_attributes_by_backend_type[backend_type]
53
159
  hydra_attributes.nil? ? [] : hydra_attributes
54
160
  end
55
161
 
162
+ # @!method hydra_attribute_ids_for_backend_type(backend_type)
163
+ # Returns array of attribute ids for passed backend type.
164
+ #
165
+ # @note If no attributes, returns blank array.
166
+ # @example
167
+ # Product.hydra_attributes.create(name: 'one', backend_type: 'string') # 1
168
+ # Product.hydra_attributes.create(name: 'two', backend_type: 'string') # 2
169
+ # Product.hydra_attributes.create(name: 'three', backend_type: 'integer') # 3
170
+ #
171
+ # Product.hydra_attributes_for_backend_type('string') # [1, 2]
172
+ # Product.hydra_attributes_for_backend_type('float') # []
173
+ # @param backend_type [String] backend type of attributes
174
+ # @return [Array<Fixnum>] contains attribute ids for passed backend type
175
+
176
+ # @!method hydra_attribute_names_for_backend_type(backend_type)
177
+ # Returns array of attribute names for passed backend type.
178
+ #
179
+ # @note If no attributes, returns blank array.
180
+ # @example
181
+ # Product.hydra_attributes.create(name: 'one', backend_type: 'string')
182
+ # Product.hydra_attributes.create(name: 'two', backend_type: 'string')
183
+ # Product.hydra_attributes.create(name: 'three', backend_type: 'integer')
184
+ #
185
+ # Product.hydra_attributes_for_backend_type('string') # ["one", "two"]
186
+ # Product.hydra_attributes_for_backend_type('float') # []
187
+ # @param backend_type [String] backend type of attributes
188
+ # @return [Array<HydraAttribute::HydraAttribute>] contains attribute models for passed backend type
56
189
  %w(id name).each do |prefix|
57
190
  module_eval <<-EOS, __FILE__, __LINE__ + 1
58
191
  def hydra_attribute_#{prefix}s_for_backend_type(backend_type)
@@ -62,6 +195,18 @@ module HydraAttribute
62
195
  EOS
63
196
  end
64
197
 
198
+ # Clear cache for the following methods:
199
+ # * hydra_attributes
200
+ # * hydra_attribute(identifier)
201
+ # * hydra_attribute_ids
202
+ # * hydra_attribute_names
203
+ # * hydra_attribute_backend_types
204
+ # * hydra_attributes_by_backend_type
205
+ # * hydra_attribute_ids_by_backend_type
206
+ # * hydra_attribute_names_by_backend_type
207
+ #
208
+ # @note This method should not be called manually. It used for hydra_attribute gem engine.
209
+ # @return [NilClass]
65
210
  def clear_hydra_attribute_cache!
66
211
  [
67
212
  :@hydra_attributes,
@@ -77,9 +222,5 @@ module HydraAttribute
77
222
  end
78
223
  end
79
224
  end
80
-
81
- def hydra_attribute?(name)
82
- self.class.hydra_attribute_names.include?(name.to_s)
83
- end
84
225
  end
85
226
  end