attribute-filters 1.4.0 → 2.0.0

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.
Files changed (41) hide show
  1. data.tar.gz.sig +0 -0
  2. data/.yardopts +1 -0
  3. data/ChangeLog +501 -0
  4. data/Gemfile +1 -1
  5. data/Gemfile.lock +7 -7
  6. data/Manifest.txt +7 -0
  7. data/README.md +59 -30
  8. data/Rakefile +1 -0
  9. data/attribute-filters.gemspec +4 -4
  10. data/docs/COMMON-FILTERS.md +1067 -0
  11. data/docs/HISTORY +42 -0
  12. data/docs/TODO +29 -12
  13. data/docs/USAGE.md +718 -818
  14. data/lib/attribute-filters.rb +4 -2
  15. data/lib/attribute-filters/attribute_set.rb +144 -73
  16. data/lib/attribute-filters/attribute_set_annotations.rb +110 -77
  17. data/lib/attribute-filters/attribute_set_attrquery.rb +51 -8
  18. data/lib/attribute-filters/attribute_set_enum.rb +44 -38
  19. data/lib/attribute-filters/attribute_set_query.rb +62 -12
  20. data/lib/attribute-filters/backports.rb +36 -4
  21. data/lib/attribute-filters/common_filters.rb +83 -37
  22. data/lib/attribute-filters/common_filters/bare.rb +29 -0
  23. data/lib/attribute-filters/common_filters/case.rb +34 -19
  24. data/lib/attribute-filters/common_filters/convert.rb +259 -0
  25. data/lib/attribute-filters/common_filters/join.rb +37 -30
  26. data/lib/attribute-filters/common_filters/order.rb +105 -0
  27. data/lib/attribute-filters/common_filters/pick.rb +90 -0
  28. data/lib/attribute-filters/common_filters/presence.rb +70 -0
  29. data/lib/attribute-filters/common_filters/split.rb +37 -21
  30. data/lib/attribute-filters/common_filters/squeeze.rb +28 -9
  31. data/lib/attribute-filters/common_filters/strip.rb +7 -3
  32. data/lib/attribute-filters/dsl_attr_virtual.rb +2 -1
  33. data/lib/attribute-filters/dsl_filters.rb +14 -61
  34. data/lib/attribute-filters/dsl_sets.rb +238 -88
  35. data/lib/attribute-filters/helpers.rb +7 -1
  36. data/lib/attribute-filters/meta_set.rb +38 -0
  37. data/lib/attribute-filters/version.rb +1 -1
  38. data/spec/attribute-filters_spec.rb +178 -16
  39. data/spec/spec_helper.rb +9 -4
  40. metadata +129 -69
  41. metadata.gz.sig +0 -0
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
+ # Copyright:: (c) 2012 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {file:LGPL-LICENSE GNU Lesser General Public License} or {file:COPYING Ruby License}.
6
+ #
7
+ # This file contains ActiveModel::AttributeFilters::Common::Bare module.
8
+
9
+ # @abstract This namespace is shared with ActveModel.
10
+ module ActiveModel
11
+ module AttributeFilters
12
+ # This module contains common, ready-to-use filtering methods.
13
+ module Common
14
+
15
+ # This module contains bare attribute filters.
16
+ module Bare
17
+ extend CommonFilter
18
+
19
+ # This submodule contains class methods used to easily define filter.
20
+ module ClassMethods
21
+ end # module ClassMethods
22
+
23
+ end # module Bare
24
+
25
+ include Bare
26
+
27
+ end # module Common
28
+ end # module AttributeFilters
29
+ end # module ActiveModel
@@ -16,6 +16,7 @@ module ActiveModel
16
16
  # This module contains attribute filters responsible for changing the case of letters.
17
17
  module Case
18
18
  extend CommonFilter
19
+
19
20
  # Downcases attributes.
20
21
  #
21
22
  # The attrubutes to be downcased are taken from the attribute set
@@ -23,16 +24,17 @@ module ActiveModel
23
24
  # used with multibyte strings (containing diacritics).
24
25
  #
25
26
  # @note If a value of currently processed attribute is an array
26
- # then any element of the array is changed.
27
+ # then any element of the array is changed. The same with hash (its values are changed).
27
28
  #
28
29
  # @return [void]
29
30
  def downcase_attributes
30
31
  filter_attrs_from_set(:should_be_downcased) do |atr|
31
- AttributeFiltersHelpers.each_element(atr, String) do |v|
32
+ AFHelpers.each_element(atr, String) do |v|
32
33
  v.mb_chars.downcase.to_s
33
34
  end
34
35
  end
35
36
  end
37
+ filtering_method :downcase_attributes, :should_be_downcased
36
38
 
37
39
  # This submodule contains class methods used to easily define filter.
38
40
  module ClassMethods
@@ -40,7 +42,9 @@ module ActiveModel
40
42
  def downcase_attributes(*args)
41
43
  attributes_that(:should_be_downcased, args)
42
44
  end
43
- alias_method :downcase_attribute, :downcase_attributes
45
+ alias_method :downcase_attribute, :downcase_attributes
46
+ alias_method :downcases_attribute, :downcase_attributes
47
+ alias_method :downcases_attributes, :downcase_attributes
44
48
  end # module ClassMethods
45
49
 
46
50
  # Upcases attributes.
@@ -50,16 +54,17 @@ module ActiveModel
50
54
  # used with multibyte strings (containing diacritics).
51
55
  #
52
56
  # @note If a value of currently processed attribute is an array
53
- # then any element of the array is changed.
57
+ # then any element of the array is changed. The same with hash (its values are changed).
54
58
  #
55
59
  # @return [void]
56
60
  def upcase_attributes
57
61
  filter_attrs_from_set(:should_be_upcased) do |atr|
58
- AttributeFiltersHelpers.each_element(atr, String) do |v|
62
+ AFHelpers.each_element(atr, String) do |v|
59
63
  v.mb_chars.upcase.to_s
60
64
  end
61
65
  end
62
66
  end
67
+ filtering_method :upcase_attributes, :should_be_upcased
63
68
 
64
69
  # This submodule contains class methods used to easily define filter.
65
70
  module ClassMethods
@@ -67,7 +72,9 @@ module ActiveModel
67
72
  def upcase_attributes(*args)
68
73
  attributes_that(:should_be_upcased, args)
69
74
  end
70
- alias_method :upcase_attribute, :upcase_attributes
75
+ alias_method :upcase_attribute, :upcase_attributes
76
+ alias_method :upcases_attribute, :upcase_attributes
77
+ alias_method :upcases_attributes, :upcase_attributes
71
78
  end # module ClassMethods
72
79
 
73
80
  # Titleizes attributes.
@@ -77,16 +84,17 @@ module ActiveModel
77
84
  # used with multibyte strings (containing diacritics).
78
85
  #
79
86
  # @note If a value of currently processed attribute is an array
80
- # then any element of the array is changed.
87
+ # then any element of the array is changed. The same with hash (its values are changed).
81
88
  #
82
89
  # @return [void]
83
90
  def titleize_attributes
84
91
  filter_attrs_from_set(:should_be_titleized) do |atr|
85
- AttributeFiltersHelpers.each_element(atr, String) do |v|
92
+ AFHelpers.each_element(atr, String) do |v|
86
93
  v.mb_chars.titleize.to_s
87
94
  end
88
95
  end
89
96
  end
97
+ filtering_method :titleize_attributes, :should_be_titleized
90
98
 
91
99
  # This submodule contains class methods used to easily define filter.
92
100
  module ClassMethods
@@ -94,7 +102,9 @@ module ActiveModel
94
102
  def titleize_attributes(*args)
95
103
  attributes_that(:should_be_titleized, args)
96
104
  end
97
- alias_method :titleize_attribute, :titleize_attributes
105
+ alias_method :titleize_attribute, :titleize_attributes
106
+ alias_method :titleizes_attribute, :titleize_attributes
107
+ alias_method :titleizes_attributes, :titleize_attributes
98
108
  end # module ClassMethods
99
109
 
100
110
  # Capitalizes attributes.
@@ -104,16 +114,17 @@ module ActiveModel
104
114
  # used with multibyte strings (containing diacritics).
105
115
  #
106
116
  # @note If a value of currently processed attribute is an array
107
- # then any element of the array is changed.
117
+ # then any element of the array is changed. The same with hash (its values are changed).
108
118
  #
109
119
  # @return [void]
110
120
  def capitalize_attributes
111
121
  filter_attrs_from_set(:should_be_capitalized) do |atr|
112
- AttributeFiltersHelpers.each_element(atr, String) do |v|
122
+ AFHelpers.each_element(atr, String) do |v|
113
123
  v.mb_chars.capitalize.to_s
114
124
  end
115
125
  end
116
126
  end
127
+ filtering_method :capitalize_attributes, :should_be_capitalized
117
128
 
118
129
  # Fully capitalizes attributes (capitalizes each word and squeezes spaces).
119
130
  #
@@ -122,18 +133,18 @@ module ActiveModel
122
133
  # This method is safe to be used with multibyte strings (containing diacritics).
123
134
  #
124
135
  # @note If a value of currently processed attribute is an array
125
- # then any element of the array is changed.
136
+ # then any element of the array is changed. The same with hash (its values are changed).
126
137
  #
127
138
  # @return [void]
128
- def titleize_with_squeezed_spaces
129
- s = attribute_set_simple(:should_be_fully_capitalized) + attribute_set_simple(:should_be_titleized)
130
- filter_attrs_from_set(s) do |atr|
131
- AttributeFiltersHelpers.each_element(atr, String) do |v|
139
+ def fully_capitalize_attributes
140
+ filter_attrs_from_set(:should_be_fully_capitalized) do |atr|
141
+ AFHelpers.each_element(atr, String) do |v|
132
142
  v.mb_chars.split(' ').map { |n| n.capitalize }.join(' ')
133
143
  end
134
144
  end
135
145
  end
136
- alias_method :fully_capitalize_attributes, :titleize_with_squeezed_spaces
146
+ filtering_method :fully_capitalize_attributes, :should_be_fully_capitalized
147
+ alias_method :titleize_with_squeezed_spaces, :fully_capitalize_attributes
137
148
 
138
149
  # This submodule contains class methods used to easily define filter.
139
150
  module ClassMethods
@@ -141,13 +152,17 @@ module ActiveModel
141
152
  def capitalize_attributes(*args)
142
153
  attributes_that(:should_be_capitalized, args)
143
154
  end
144
- alias_method :capitalize_attribute, :capitalize_attributes
155
+ alias_method :capitalize_attribute, :capitalize_attributes
156
+ alias_method :capitalizes_attribute, :capitalize_attributes
157
+ alias_method :capitalizes_attributes, :capitalize_attributes
145
158
 
146
159
  # Registers attributes that should be fully capitalized.
147
160
  def fully_capitalize_attributes(*args)
148
161
  attributes_that(:should_be_fully_capitalized, args)
149
162
  end
150
- alias_method :fully_capitalize_attribute, :fully_capitalize_attributes
163
+ alias_method :fully_capitalize_attribute, :fully_capitalize_attributes
164
+ alias_method :fully_capitalizes_attribute, :fully_capitalize_attributes
165
+ alias_method :fully_capitalizes_attributes, :fully_capitalize_attributes
151
166
  alias_method :titleize_with_squeezed_spaces, :fully_capitalize_attributes
152
167
  end # module ClassMethods
153
168
  end # module Case
@@ -0,0 +1,259 @@
1
+ # encoding: utf-8
2
+ #
3
+ # Author:: Paweł Wilk (mailto:pw@gnu.org)
4
+ # Copyright:: (c) 2012 by Paweł Wilk
5
+ # License:: This program is licensed under the terms of {file:LGPL-LICENSE GNU Lesser General Public License} or {file:COPYING Ruby License}.
6
+ #
7
+ # This file contains ActiveModel::AttributeFilters::Common::Convert module
8
+ # containing ready-to-use filtering methods for performing the type conversion.
9
+
10
+ # @abstract This namespace is shared with ActveModel.
11
+ module ActiveModel
12
+ module AttributeFilters
13
+ # This module contains common, ready-to-use filtering methods.
14
+ module Common
15
+
16
+ # This module contains attribute filters responsible for converting the attributes.
17
+ module Convert
18
+ extend CommonFilter
19
+
20
+ # Helper that is used by all converting filters.
21
+ def attributes_convert(set_name, default_key, *params, &block)
22
+ filter_attrs_from_set(set_name, *params) do |atr_val, atr_name, set_obj|
23
+ AFHelpers.each_element(atr_val) do |v|
24
+ begin
25
+ yield(v, atr_name, set_obj)
26
+ rescue NoMethodError, ArgumentError
27
+ if set_obj.has_annotation?(atr_name, default_key)
28
+ set_obj.annotation(atr_name, default_key)
29
+ else
30
+ v
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ private :attributes_convert
37
+
38
+ # Convert attributes to strings.
39
+ #
40
+ # The attrubutes are taken from the attribute set
41
+ # called +should_be_strings+.
42
+ #
43
+ # @note If a value of currently processed attribute is an array
44
+ # then each element of the array is changed.
45
+ #
46
+ # @return [void]
47
+ def attributes_to_s
48
+ attributes_convert(:should_be_strings, :to_s_default) do |v, atr_name, set_obj|
49
+ if set_obj.has_annotation?(atr_name, :to_s_base)
50
+ v = 0 if v.nil?
51
+ if v.method(:to_s).arity != 0
52
+ v.to_s(set_obj.annotation(atr_name, :to_s_base) || 10)
53
+ elsif set_obj.has_annotation?(atr_name, :to_s_default)
54
+ set_obj.annotation(atr_name, :to_s_default)
55
+ else
56
+ v
57
+ end
58
+ else
59
+ v.to_s
60
+ end
61
+ end
62
+ end
63
+ filtering_method :attributes_to_s, :should_be_strings
64
+ alias_method :attributes_to_strings, :attributes_to_s
65
+ alias_method :convert_to_strings, :attributes_to_s
66
+
67
+ # This submodule contains class methods used to easily define filter.
68
+ module ClassMethods
69
+ # Registers attributes that should be converted.
70
+ def attributes_to_s(*args)
71
+ setup_attributes_that :should_be_strings, args,
72
+ {
73
+ :to_s_default => [:default, :on_error, :to_s_default],
74
+ :to_s_base => [:base, :with_base, :to_s_base]
75
+ }, :to_s_base
76
+ end
77
+ alias_method :convert_to_string, :attributes_to_s
78
+ alias_method :convert_to_strings, :attributes_to_s
79
+ alias_method :converts_to_string, :attributes_to_s
80
+ alias_method :converts_to_strings, :attributes_to_s
81
+ end # module ClassMethods
82
+
83
+ # Convert attributes to integers.
84
+ #
85
+ # The attrubutes are taken from the attribute set
86
+ # called +should_be_integers+.
87
+ #
88
+ # @note If a value of currently processed attribute is an array
89
+ # then each element of the array is changed.
90
+ #
91
+ # @return [void]
92
+ def attributes_to_i
93
+ attributes_convert(:should_be_integers, :to_i_default) do |v, atr_name, set_obj|
94
+ if set_obj.has_annotation?(atr_name, :to_i_base)
95
+ if v.method(:to_i).arity != 0
96
+ v.to_i(set_obj.annotation(atr_name, :to_i_base) || 10)
97
+ elsif set_obj.has_annotation?(atr_name, :to_i_default)
98
+ set_obj.annotation(atr_name, :to_i_default)
99
+ else
100
+ v
101
+ end
102
+ else
103
+ v.to_i
104
+ end
105
+ end
106
+ end
107
+ filtering_method :attributes_to_i, :should_be_integers
108
+ alias_method :attributes_to_integers, :attributes_to_i
109
+ alias_method :convert_to_integers, :attributes_to_i
110
+
111
+ # This submodule contains class methods used to easily define filter.
112
+ module ClassMethods
113
+ # Registers attributes that should be converted.
114
+ def attributes_to_i(*args)
115
+ setup_attributes_that :should_be_integers, args,
116
+ {
117
+ :to_i_default => [:default, :on_error, :to_i_default],
118
+ :to_i_base => [:base, :with_base, :to_i_base]
119
+ }, :to_i_base
120
+ end
121
+ alias_method :convert_to_integer, :attributes_to_i
122
+ alias_method :convert_to_integers, :attributes_to_i
123
+ alias_method :converts_to_integer, :attributes_to_i
124
+ alias_method :converts_to_integers, :attributes_to_i
125
+ end # module ClassMethods
126
+
127
+ # Convert attributes to floats.
128
+ #
129
+ # The attrubutes are taken from the attribute set
130
+ # called +should_be_floats+.
131
+ #
132
+ # @note If a value of currently processed attribute is an array
133
+ # then each element of the array is changed.
134
+ #
135
+ # @return [void]
136
+ def attributes_to_f
137
+ attributes_convert(:should_be_floats, :to_f_default) { |v| v.to_f }
138
+ end
139
+ filtering_method :attributes_to_f, :should_be_floats
140
+ alias_method :attributes_to_floats, :attributes_to_f
141
+ alias_method :convert_to_floats, :attributes_to_f
142
+
143
+ # This submodule contains class methods used to easily define filter.
144
+ module ClassMethods
145
+ # Registers attributes that should be converted.
146
+ def attributes_to_f(*args)
147
+ setup_attributes_that :should_be_floats, args,
148
+ { :to_f_default => [:default, :on_error, :to_f_default] },
149
+ :to_f_default
150
+ end
151
+ alias_method :convert_to_float, :attributes_to_f
152
+ alias_method :convert_to_floats, :attributes_to_f
153
+ alias_method :converts_to_float, :attributes_to_f
154
+ alias_method :converts_to_floats, :attributes_to_f
155
+ end # module ClassMethods
156
+
157
+ # Convert attributes to numbers.
158
+ #
159
+ # The attrubutes are taken from the attribute set
160
+ # called +should_be_numbers+. It works the same as converting
161
+ # to floats but uses different attribute set.
162
+ #
163
+ # @note If a value of currently processed attribute is an array
164
+ # then each element of the array is changed.
165
+ #
166
+ # @return [void]
167
+ def attributes_to_numbers
168
+ attributes_convert(:should_be_numbers, :to_num_default) { |v| v.to_f }
169
+ end
170
+ filtering_method :attributes_to_numbers, :should_be_numbers
171
+ alias_method :convert_to_numbers, :attributes_to_numbers
172
+ alias_method :attribute_to_numbers, :attributes_to_numbers
173
+
174
+ # This submodule contains class methods used to easily define filter.
175
+ module ClassMethods
176
+ # Registers attributes that should be converted.
177
+ def attributes_to_numbers(*args)
178
+ setup_attributes_that :should_be_numbers, args,
179
+ { :to_num_default => [:default, :on_error, :to_number_default, :to_num_default] },
180
+ :to_num_default
181
+ end
182
+ alias_method :convert_to_number, :attributes_to_numbers
183
+ alias_method :convert_to_numbers, :attributes_to_numbers
184
+ alias_method :converts_to_number, :attributes_to_numbers
185
+ alias_method :converts_to_numbers, :attributes_to_numbers
186
+ end # module ClassMethods
187
+
188
+ # Convert attributes to rationals.
189
+ #
190
+ # The attrubutes are taken from the attribute set
191
+ # called +should_be_rationals+.
192
+ #
193
+ # @note If a value of currently processed attribute is an array
194
+ # then each element of the array is changed.
195
+ #
196
+ # @return [void]
197
+ def attributes_to_r
198
+ attributes_convert(:should_be_rationals, :to_r_default) { |v| v.to_r }
199
+ end
200
+ filtering_method :attributes_to_r, :should_be_rationals
201
+ alias_method :attributes_to_rationals, :attributes_to_r
202
+ alias_method :attributes_to_fractions, :attributes_to_r
203
+ alias_method :convert_to_rationals, :attributes_to_r
204
+
205
+ # This submodule contains class methods used to easily define filter.
206
+ module ClassMethods
207
+ # Registers attributes that should be converted.
208
+ def attributes_to_r(*args)
209
+ setup_attributes_that :should_be_rationals, args,
210
+ { :to_r_default => [:default, :on_error, :to_r_default] },
211
+ :to_r_default
212
+ end
213
+ alias_method :convert_to_rational, :attributes_to_r
214
+ alias_method :convert_to_rationals, :attributes_to_r
215
+ alias_method :convert_to_fraction, :attributes_to_r
216
+ alias_method :convert_to_fractions, :attributes_to_r
217
+ alias_method :converts_to_rational, :attributes_to_r
218
+ alias_method :converts_to_rationals, :attributes_to_r
219
+ alias_method :converts_to_fraction, :attributes_to_r
220
+ alias_method :converts_to_fractions, :attributes_to_r
221
+ end # module ClassMethods
222
+
223
+ # Convert attributes to boolean values.
224
+ #
225
+ # The attrubutes are taken from the attribute set
226
+ # called +should_be_boolean+.
227
+ #
228
+ # @note If a value of currently processed attribute is an array
229
+ # then each element of the array is changed.
230
+ #
231
+ # @return [void]
232
+ def attributes_to_b
233
+ attributes_convert(:should_be_boolean, :to_b_default, :process_blank) { |v| !!v }
234
+ end
235
+ filtering_method :attributes_to_b, :should_be_boolean
236
+ alias_method :attributes_to_boolean, :attributes_to_b
237
+ alias_method :convert_to_boolean, :attributes_to_b
238
+
239
+ # This submodule contains class methods used to easily define filter.
240
+ module ClassMethods
241
+ # Registers attributes that should be converted.
242
+ def attributes_to_b(*args)
243
+ setup_attributes_that :should_be_boolean, args,
244
+ { :to_b_default => [:default, :on_error, :to_b_default] },
245
+ :to_b_default
246
+ end
247
+ alias_method :convert_to_boolean, :attributes_to_b
248
+ alias_method :convert_to_booleans, :attributes_to_b
249
+ alias_method :converts_to_boolean, :attributes_to_b
250
+ alias_method :converts_to_booleans, :attributes_to_b
251
+ end # module ClassMethods
252
+
253
+ end # module Convert
254
+
255
+ include Convert
256
+
257
+ end # module Common
258
+ end # module AttributeFilters
259
+ end # module ActiveModel