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.
- data.tar.gz.sig +0 -0
- data/.yardopts +1 -0
- data/ChangeLog +501 -0
- data/Gemfile +1 -1
- data/Gemfile.lock +7 -7
- data/Manifest.txt +7 -0
- data/README.md +59 -30
- data/Rakefile +1 -0
- data/attribute-filters.gemspec +4 -4
- data/docs/COMMON-FILTERS.md +1067 -0
- data/docs/HISTORY +42 -0
- data/docs/TODO +29 -12
- data/docs/USAGE.md +718 -818
- data/lib/attribute-filters.rb +4 -2
- data/lib/attribute-filters/attribute_set.rb +144 -73
- data/lib/attribute-filters/attribute_set_annotations.rb +110 -77
- data/lib/attribute-filters/attribute_set_attrquery.rb +51 -8
- data/lib/attribute-filters/attribute_set_enum.rb +44 -38
- data/lib/attribute-filters/attribute_set_query.rb +62 -12
- data/lib/attribute-filters/backports.rb +36 -4
- data/lib/attribute-filters/common_filters.rb +83 -37
- data/lib/attribute-filters/common_filters/bare.rb +29 -0
- data/lib/attribute-filters/common_filters/case.rb +34 -19
- data/lib/attribute-filters/common_filters/convert.rb +259 -0
- data/lib/attribute-filters/common_filters/join.rb +37 -30
- data/lib/attribute-filters/common_filters/order.rb +105 -0
- data/lib/attribute-filters/common_filters/pick.rb +90 -0
- data/lib/attribute-filters/common_filters/presence.rb +70 -0
- data/lib/attribute-filters/common_filters/split.rb +37 -21
- data/lib/attribute-filters/common_filters/squeeze.rb +28 -9
- data/lib/attribute-filters/common_filters/strip.rb +7 -3
- data/lib/attribute-filters/dsl_attr_virtual.rb +2 -1
- data/lib/attribute-filters/dsl_filters.rb +14 -61
- data/lib/attribute-filters/dsl_sets.rb +238 -88
- data/lib/attribute-filters/helpers.rb +7 -1
- data/lib/attribute-filters/meta_set.rb +38 -0
- data/lib/attribute-filters/version.rb +1 -1
- data/spec/attribute-filters_spec.rb +178 -16
- data/spec/spec_helper.rb +9 -4
- metadata +129 -69
- metadata.gz.sig +0 -0
|
@@ -44,6 +44,7 @@ module ActiveModel
|
|
|
44
44
|
end
|
|
45
45
|
end
|
|
46
46
|
end
|
|
47
|
+
filtering_method :join_attributes, :should_be_joined
|
|
47
48
|
|
|
48
49
|
# This submodule contains class methods needed to describe
|
|
49
50
|
# attribute joining.
|
|
@@ -52,42 +53,48 @@ module ActiveModel
|
|
|
52
53
|
# It uses attribute set annotations to register parameters used when joining.
|
|
53
54
|
#
|
|
54
55
|
# @param atr_name [String,Symbol] attribute name
|
|
55
|
-
# @param parameters [Hash] parameters hash
|
|
56
|
+
# @param parameters [Hash] parameters hash
|
|
57
|
+
# @option parameters :separator [String] separator passed to +join+ method call
|
|
58
|
+
# @option parameters :join_separator [String] separator passed to +join+ method call (alternative name)
|
|
59
|
+
# @option parameters :with [String] separator passed to +join+ method call (alternative name)
|
|
60
|
+
# @option parameters :from [String,Array<String>] names of source attributes used to join
|
|
61
|
+
# @option parameters :join_from [String,Array<String>] names of source attributes used to join (alternative name)
|
|
62
|
+
# @option parameters :source [String,Array<String>] names of source attributes used to join (alternative name)
|
|
63
|
+
# @option parameters :sources [String,Array<String>] names of source attributes used to join (alternative name)
|
|
64
|
+
# @option parameters :compact [Boolean] flag that causes sources to be compacted before joining
|
|
65
|
+
# @option parameters :join_compact [Boolean] flag that causes sources to be compacted before joining (alternative name)
|
|
56
66
|
# @return [void]
|
|
57
|
-
def
|
|
67
|
+
def join_attributes(atr_name, parameters = nil)
|
|
58
68
|
atr_name.is_a?(Hash) and return atr_name.each_pair { |k, v| join_attribute(k, v) }
|
|
69
|
+
# process reversed notation
|
|
70
|
+
p = parameters
|
|
59
71
|
if atr_name.is_a?(Array)
|
|
60
|
-
if
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
dst
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return join_attribute(dst, parameters)
|
|
72
|
+
if p.is_a?(Hash)
|
|
73
|
+
p = p.dup
|
|
74
|
+
dst = [:into, :in, :destination, :join_into].find { |k| p.key?(k) }
|
|
75
|
+
dst.nil? and raise ArgumentError, "you must specify destination attribute using :into => 'attribute_name'"
|
|
76
|
+
p[:from] = atr_name
|
|
77
|
+
return join_attribute(p.delete(dst), p)
|
|
78
|
+
else
|
|
79
|
+
return join_attribute(p, atr_name)
|
|
69
80
|
end
|
|
70
81
|
end
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
elsif parameters.key?(:join_separator)
|
|
79
|
-
a[:join_separator] = parameters[:join_separator]
|
|
80
|
-
end
|
|
81
|
-
from = parameters[:from] || parameters[:source] || parameters[:sources] || parameters[:join_from]
|
|
82
|
-
compact = parameters.key?(:compact) ? !!parameters[:compact] : !!parameters[:join_compact]
|
|
83
|
-
a.merge!({ :join_compact => compact, :join_from => from })
|
|
84
|
-
annotate_attributes_that(:should_be_joined, atr_name => a)
|
|
82
|
+
# setup attribute set
|
|
83
|
+
setup_attributes_that :should_be_joined, { atr_name => p },
|
|
84
|
+
{
|
|
85
|
+
:join_separator => [ :with, :separator, :join_separator ],
|
|
86
|
+
:join_from => [ :from, :source, :sources, :join_from ],
|
|
87
|
+
:join_compact => [ :compact, :join_compact ]
|
|
88
|
+
}, :join_from
|
|
85
89
|
end
|
|
86
|
-
alias_method :
|
|
87
|
-
alias_method :joint_attribute,
|
|
88
|
-
alias_method :joint_attributes,
|
|
89
|
-
alias_method :join_attributes_to,
|
|
90
|
-
alias_method :join_attributes_into,
|
|
90
|
+
alias_method :join_attribute, :join_attributes
|
|
91
|
+
alias_method :joint_attribute, :join_attributes
|
|
92
|
+
alias_method :joint_attributes, :join_attributes
|
|
93
|
+
alias_method :join_attributes_to, :join_attributes
|
|
94
|
+
alias_method :join_attributes_into, :join_attributes
|
|
95
|
+
alias_method :joins_attribute, :join_attributes
|
|
96
|
+
alias_method :joins_attributes_to, :join_attributes
|
|
97
|
+
alias_method :joins_attributes_into, :join_attributes
|
|
91
98
|
end # module ClassMethods
|
|
92
99
|
end # module Join
|
|
93
100
|
|
|
@@ -0,0 +1,105 @@
|
|
|
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::Order module
|
|
8
|
+
# containing ready-to-use filtering method.
|
|
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 manipulating order of attribute values.
|
|
17
|
+
module Order
|
|
18
|
+
extend CommonFilter
|
|
19
|
+
# Inverses order of attribute contents.
|
|
20
|
+
#
|
|
21
|
+
# The attrubutes are taken from the attribute set
|
|
22
|
+
# called +should_be_reversed+.
|
|
23
|
+
#
|
|
24
|
+
# @note If a value of currently processed attribute is an array
|
|
25
|
+
# then any element of the array is changed. The same with hash (its values are changed).
|
|
26
|
+
#
|
|
27
|
+
# @return [void]
|
|
28
|
+
def reverse_attributes
|
|
29
|
+
filter_attrs_from_set(:should_be_reversed) do |atr_val, atr_name, set_obj|
|
|
30
|
+
if set_obj.annotation(atr_name, :reverse_enumerable)
|
|
31
|
+
atr_val.respond_to?(:reverse) ? atr_val.reverse : atr_val
|
|
32
|
+
else
|
|
33
|
+
AFHelpers.each_element(atr_val) do |v|
|
|
34
|
+
v.respond_to?(:reverse) ? v.reverse : v
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
filtering_method :reverse_attributes, :should_be_reversed
|
|
40
|
+
|
|
41
|
+
# This submodule contains class methods used to easily define filter.
|
|
42
|
+
module ClassMethods
|
|
43
|
+
# Registers attributes that should be filled with some values.
|
|
44
|
+
def reverse_attributes(*args)
|
|
45
|
+
setup_attributes_that :should_be_reversed, args,
|
|
46
|
+
:reverse_enumerable => [:enum, :enums, :whole_enums, :reverse_enums, :reverse_enumerable]
|
|
47
|
+
end
|
|
48
|
+
alias_method :reverse_attribute, :reverse_attributes
|
|
49
|
+
alias_method :reverses_attribute, :reverse_attributes
|
|
50
|
+
alias_method :reverses_attributes, :reverse_attributes
|
|
51
|
+
end # module ClassMethods
|
|
52
|
+
end # module Order
|
|
53
|
+
|
|
54
|
+
# Randomizes order of attribute contents.
|
|
55
|
+
#
|
|
56
|
+
# The attrubutes are taken from the attribute set
|
|
57
|
+
# called +should_be_shuffled+.
|
|
58
|
+
#
|
|
59
|
+
# @note If a value of currently processed attribute is an array
|
|
60
|
+
# then any element of the array is changed. The same with hash (its values are changed).
|
|
61
|
+
#
|
|
62
|
+
# @return [void]
|
|
63
|
+
def shuffle_attributes
|
|
64
|
+
filter_attrs_from_set(:should_be_shuffled) do |atr_val, atr_name, set_obj|
|
|
65
|
+
shuffle_enum, rng = set_obj.annotation(atr_name, :shuffle_enumerable, :shuffle_generator)
|
|
66
|
+
rng = { :random => rng }
|
|
67
|
+
if shuffle_enum
|
|
68
|
+
if atr_val.is_a?(String)
|
|
69
|
+
atr_val.mb_chars.split("").shuffle(rng).join
|
|
70
|
+
else
|
|
71
|
+
atr_val.respond_to?(:shuffle) ? atr_val.shuffle(rng) : atr_val
|
|
72
|
+
end
|
|
73
|
+
else
|
|
74
|
+
AFHelpers.each_element(atr_val) do |v|
|
|
75
|
+
if v.is_a?(String)
|
|
76
|
+
v.mb_chars.split("").shuffle(rng).join
|
|
77
|
+
else
|
|
78
|
+
v.respond_to?(:shuffle) ? v.shuffle(rng) : v
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
filtering_method :shuffle_attributes, :should_be_shuffled
|
|
85
|
+
|
|
86
|
+
# This submodule contains class methods used to easily define filter.
|
|
87
|
+
module ClassMethods
|
|
88
|
+
# Registers attributes that should be shuffled.
|
|
89
|
+
def shuffle_attributes(*args)
|
|
90
|
+
setup_attributes_that :should_be_shuffled, args,
|
|
91
|
+
{
|
|
92
|
+
:shuffle_enumerable => [:enum, :enums, :whole_enums, :shuffle_enums, :shuffle_enumerable],
|
|
93
|
+
:shuffle_generator => [:random_generator, :generator, :rnd, :rng, :shuffle_generator]
|
|
94
|
+
}, :shuffle_generator
|
|
95
|
+
end
|
|
96
|
+
alias_method :shuffle_attribute, :shuffle_attributes
|
|
97
|
+
alias_method :shuffles_attribute, :shuffle_attributes
|
|
98
|
+
alias_method :shuffles_attributes, :shuffle_attributes
|
|
99
|
+
end # module ClassMethods
|
|
100
|
+
|
|
101
|
+
include Order
|
|
102
|
+
|
|
103
|
+
end # module Common
|
|
104
|
+
end # module AttributeFilters
|
|
105
|
+
end # module ActiveModel
|
|
@@ -0,0 +1,90 @@
|
|
|
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::Pick module
|
|
8
|
+
# containing ready-to-use filtering method.
|
|
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
|
+
# Picks attributes from leading and trailing spaces.
|
|
16
|
+
module Pick
|
|
17
|
+
extend CommonFilter
|
|
18
|
+
|
|
19
|
+
def pick_attributes_core(val, from, to, range, step)
|
|
20
|
+
from ||= 0
|
|
21
|
+
to ||= val.size - 1
|
|
22
|
+
step ||= 1
|
|
23
|
+
range ||= from..to
|
|
24
|
+
range.step(step).map { |n| val[n] }
|
|
25
|
+
end
|
|
26
|
+
private :pick_attributes_core
|
|
27
|
+
|
|
28
|
+
# Picks attributes from leading and trailing spaces.
|
|
29
|
+
#
|
|
30
|
+
# The attrubutes to be picked are taken from the attribute set called
|
|
31
|
+
# +should_be_picked+. It operates directly on attribute's contents.
|
|
32
|
+
#
|
|
33
|
+
# @note If a value of currently processed attribute is an array
|
|
34
|
+
# then any element of the array is changed. The same with hash (its values are changed).
|
|
35
|
+
#
|
|
36
|
+
# @return [void]
|
|
37
|
+
def pick_attributes
|
|
38
|
+
filter_attrs_from_set(:should_be_picked) do |atr_val, atr_name, set_obj|
|
|
39
|
+
pick_enum, step, from, to, range, sep, jnt = set_obj.annotation(atr_name, :pick_enumerable, :pick_step,
|
|
40
|
+
:pick_from, :pick_to, :pick_range,
|
|
41
|
+
:pick_separator, :pick_join)
|
|
42
|
+
if pick_enum
|
|
43
|
+
if atr_val.is_a?(String)
|
|
44
|
+
sep ||= ""
|
|
45
|
+
jnt ||= sep.is_a?(String) ? sep : nil
|
|
46
|
+
pick_attributes_core(atr_val.mb_chars.split(sep), from, to, range, step).join(jnt)
|
|
47
|
+
elsif atr_val.is_a?(Array)
|
|
48
|
+
pick_attributes_core(atr_val, from, to, range, step)
|
|
49
|
+
elsif atr_val.is_a?(Hash)
|
|
50
|
+
Hash[pick_attributes_core(atr_val.to_a, from, to, range, step)]
|
|
51
|
+
else
|
|
52
|
+
atr_val
|
|
53
|
+
end
|
|
54
|
+
else
|
|
55
|
+
sep ||= ""
|
|
56
|
+
jnt ||= sep.is_a?(String) ? sep : nil
|
|
57
|
+
AFHelpers.each_element(atr_val, String) do |v|
|
|
58
|
+
pick_attributes_core(v.mb_chars.split(sep), from, to, range, step).join(jnt)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
filtering_method :pick_attributes, :should_be_picked
|
|
64
|
+
|
|
65
|
+
# This submodule contains class methods used to easily define filter.
|
|
66
|
+
module ClassMethods
|
|
67
|
+
# Registers attributes that should be picked.
|
|
68
|
+
def pick_attributes(*args)
|
|
69
|
+
setup_attributes_that :should_be_picked, args,
|
|
70
|
+
{
|
|
71
|
+
:pick_enumerable => [:enum, :enums, :whole_enums, :shuffle_enums, :pick_enumerable],
|
|
72
|
+
:pick_step => [:step, :with_step, :each, :pick_step],
|
|
73
|
+
:pick_from => [:from, :head, :take, :first, :pick_first, :pick_head, :pick_from],
|
|
74
|
+
:pick_to => [:to, :tail, :last, :pick_last, :pick_tail, :pick_to],
|
|
75
|
+
:pick_range => [:range, :pick_range],
|
|
76
|
+
:pick_separator => [:separator, :regex, :split_with, :split_separator, :pick_separator],
|
|
77
|
+
:pick_join => [:joiner, :join, :join_with, :pick_join]
|
|
78
|
+
}, :pick_separator
|
|
79
|
+
end
|
|
80
|
+
alias_method :pick_attribute, :pick_attributes
|
|
81
|
+
alias_method :picks_attribute, :pick_attributes
|
|
82
|
+
alias_method :picks_attributes, :pick_attributes
|
|
83
|
+
end # module ClassMethods
|
|
84
|
+
end # module Pick
|
|
85
|
+
|
|
86
|
+
include Pick
|
|
87
|
+
|
|
88
|
+
end # module Common
|
|
89
|
+
end # module AttributeFilters
|
|
90
|
+
end # module ActiveModel
|
|
@@ -0,0 +1,70 @@
|
|
|
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::Presence module
|
|
8
|
+
# containing ready-to-use filtering method.
|
|
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 manipulating presence of attribute values.
|
|
17
|
+
module Presence
|
|
18
|
+
extend CommonFilter
|
|
19
|
+
# Presences-up attributes that are blank with +nil+ or the given values.
|
|
20
|
+
#
|
|
21
|
+
# The attrubutes are taken from the attribute set
|
|
22
|
+
# called +should_be_filled+.
|
|
23
|
+
#
|
|
24
|
+
# @note If a value of currently processed attribute is an array
|
|
25
|
+
# then any element of the array is changed. The same with hash (its values are changed).
|
|
26
|
+
#
|
|
27
|
+
# @return [void]
|
|
28
|
+
def fill_attributes
|
|
29
|
+
filter_attrs_from_set(:should_be_filled, :process_blank) do |atr_val, atr_name, set_obj|
|
|
30
|
+
if set_obj.annotation(atr_name, :fill_enumerable)
|
|
31
|
+
if atr_val.blank? || set_obj.annotation(atr_name, :fill_any)
|
|
32
|
+
set_obj.annotation(atr_name, :fill_value)
|
|
33
|
+
else
|
|
34
|
+
atr_val
|
|
35
|
+
end
|
|
36
|
+
else
|
|
37
|
+
AFHelpers.each_element(atr_val) do |v|
|
|
38
|
+
if v.blank? || set_obj.annotation(atr_name, :fill_any)
|
|
39
|
+
set_obj.annotation(atr_name, :fill_value)
|
|
40
|
+
else
|
|
41
|
+
v
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
filtering_method :fill_attributes, :should_be_filled
|
|
48
|
+
|
|
49
|
+
# This submodule contains class methods used to easily define filter.
|
|
50
|
+
module ClassMethods
|
|
51
|
+
# Registers attributes that should be filled with some values.
|
|
52
|
+
def fill_attributes(*args)
|
|
53
|
+
setup_attributes_that :should_be_filled, args,
|
|
54
|
+
{
|
|
55
|
+
:fill_value => [:with, :fill_with, :fill, :value, :content, :default, :fill_value],
|
|
56
|
+
:fill_any => [:all, :any, :fill_always, :always_fill, :always, :fill_present, :fill_all, :fill_any],
|
|
57
|
+
:fill_enumerable => [:replace_enumerable, :replace_enums, :enums, :whole_enums, :fill_enums, :fill_enumerable]
|
|
58
|
+
}, :fill_value
|
|
59
|
+
end
|
|
60
|
+
alias_method :fill_attribute, :fill_attributes
|
|
61
|
+
alias_method :fills_attribute, :fill_attributes
|
|
62
|
+
alias_method :fills_attributes, :fill_attributes
|
|
63
|
+
end # module ClassMethods
|
|
64
|
+
end # module Presence
|
|
65
|
+
|
|
66
|
+
include Presence
|
|
67
|
+
|
|
68
|
+
end # module Common
|
|
69
|
+
end # module AttributeFilters
|
|
70
|
+
end # module ActiveModel
|
|
@@ -29,11 +29,11 @@ module ActiveModel
|
|
|
29
29
|
pattern, limit, flatten, into = set_obj.annotation(atr_name, :split_pattern, :split_limit,
|
|
30
30
|
:split_flatten, :split_into)
|
|
31
31
|
if limit.nil?
|
|
32
|
-
r =
|
|
32
|
+
r = AFHelpers.each_element(atr_val, String) do |v|
|
|
33
33
|
v.mb_chars.split(pattern)
|
|
34
34
|
end
|
|
35
35
|
else
|
|
36
|
-
r =
|
|
36
|
+
r = AFHelpers.each_element(atr_val, String) do |v|
|
|
37
37
|
v.mb_chars.split(pattern, limit)
|
|
38
38
|
end
|
|
39
39
|
end
|
|
@@ -48,6 +48,7 @@ module ActiveModel
|
|
|
48
48
|
end
|
|
49
49
|
end
|
|
50
50
|
end
|
|
51
|
+
filtering_method :split_attributes, :should_be_splitted
|
|
51
52
|
|
|
52
53
|
# This submodule contains class methods needed to describe
|
|
53
54
|
# attribute splitting.
|
|
@@ -97,29 +98,44 @@ module ActiveModel
|
|
|
97
98
|
# end
|
|
98
99
|
#
|
|
99
100
|
# @param atr_name [String,Symbol] attribute name
|
|
100
|
-
# @param parameters [Hash] parameters hash
|
|
101
|
-
# @
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
101
|
+
# @param parameters [Hash] parameters hash
|
|
102
|
+
# @option parameters :pattern [String] pattern passed to +split+ method call
|
|
103
|
+
# @option parameters :split_pattern [String] pattern passed to +split+ method call (alternative name)
|
|
104
|
+
# @option parameters :with [String] pattern passed to +split+ method call (alternative name)
|
|
105
|
+
# @option parameters :limit [Fixnum] limit parameter passed to +split+ method call
|
|
106
|
+
# @option parameters :split_limit [Fixnum] limit parameter passed to +split+ method call (alternative name)
|
|
107
|
+
# @option parameters :into [String,Symbol,Array<String,Symbol>] names of attributes that results should be written to
|
|
108
|
+
# @option parameters :to [String,Symbol,Array<String,Symbol>] names of attributes that results should be written to (alternative name)
|
|
109
|
+
# @option parameters :split_into [String,Symbol,Array<String,Symbol>] names of attributes that results should be written to (alternative name)
|
|
110
|
+
# @option parameters :destination [String,Symbol,Array<String,Symbol>] names of attributes that results should be written to (alternative name)
|
|
111
|
+
# @option parameters :flatten [Boolean] flag that causes the resulting, intermediate array to be flattened
|
|
112
|
+
# @option parameters :split_flatten [Boolean] flag that causes the resulting, intermediate array to be flattened (alternative name)
|
|
113
|
+
# @return [nil]
|
|
114
|
+
def split_attributes(atr_name, parameters = nil)
|
|
115
|
+
if atr_name.is_a?(Hash)
|
|
116
|
+
atr_name.each_pair { |k, v| split_attribute(k, v) }
|
|
117
|
+
return nil
|
|
118
|
+
end
|
|
119
|
+
setup_attributes_that :should_be_splitted, { atr_name => parameters },
|
|
120
|
+
{
|
|
121
|
+
:split_pattern => [ :with, :pattern, :split_pattern ],
|
|
122
|
+
:split_into => [ :into, :to, :destination, :split_into ],
|
|
123
|
+
:split_limit => [ :limit, :split_limit ],
|
|
124
|
+
:split_flatten => [ :flatten, :split_flatten ]
|
|
125
|
+
}, :split_into
|
|
126
|
+
# write some defaults
|
|
127
|
+
limit, into = attribute_set(:should_be_splitted).get_annotations(atr_name, :split_limit, :split_into)
|
|
128
|
+
annotate_attribute_set(:should_be_splitted, atr_name, :split_limit, limit.to_i) unless limit.blank?
|
|
111
129
|
if into.blank?
|
|
112
|
-
|
|
130
|
+
annotate_attribute_set(:should_be_splitted, atr_name,:split_into, nil)
|
|
113
131
|
elsif !into.is_a?(Array)
|
|
114
|
-
|
|
132
|
+
annotate_attributes_that(:should_be_splitted, :split_into, into.respond_to?(:to_a) ? into.to_a : [ into ])
|
|
115
133
|
end
|
|
116
|
-
|
|
117
|
-
:split_flatten => flatten,
|
|
118
|
-
:split_pattern => pattern,
|
|
119
|
-
:split_limit => limit,
|
|
120
|
-
:split_into => into})
|
|
134
|
+
nil
|
|
121
135
|
end
|
|
122
|
-
alias_method :
|
|
136
|
+
alias_method :split_attribute, :split_attributes
|
|
137
|
+
alias_method :splits_attribute, :split_attributes
|
|
138
|
+
alias_method :splits_attributes, :split_attributes
|
|
123
139
|
end # module ClassMethods
|
|
124
140
|
end # module Split
|
|
125
141
|
|
|
@@ -22,23 +22,38 @@ module ActiveModel
|
|
|
22
22
|
# used with multibyte strings (containing diacritics).
|
|
23
23
|
#
|
|
24
24
|
# @note If a value of currently processed attribute is an array
|
|
25
|
-
# then any element of the array is changed.
|
|
25
|
+
# then any element of the array is changed. The same with hash (its values are changed).
|
|
26
26
|
#
|
|
27
27
|
# @return [void]
|
|
28
28
|
def squeeze_attributes
|
|
29
|
-
filter_attrs_from_set(:should_be_squeezed) do |
|
|
30
|
-
|
|
31
|
-
|
|
29
|
+
filter_attrs_from_set(:should_be_squeezed) do |atr_val, atr_name, set_obj|
|
|
30
|
+
other_str = set_obj.annotation(atr_name, :squeeze_other_str)
|
|
31
|
+
if other_str.nil?
|
|
32
|
+
AFHelpers.each_element(atr_val, String) do |v|
|
|
33
|
+
v.mb_chars.squeeze.to_s
|
|
34
|
+
end
|
|
35
|
+
else
|
|
36
|
+
AFHelpers.each_element(atr_val, String) do |v|
|
|
37
|
+
v.mb_chars.squeeze(other_str).to_s
|
|
38
|
+
end
|
|
32
39
|
end
|
|
33
40
|
end
|
|
34
41
|
end
|
|
42
|
+
filtering_method :squeeze_attributes, :should_be_squeezed
|
|
43
|
+
|
|
35
44
|
# This submodule contains class methods used to easily define filter.
|
|
36
45
|
module ClassMethods
|
|
37
46
|
# Registers attributes that should be squeezed.
|
|
38
47
|
def squeeze_attributes(*args)
|
|
39
|
-
|
|
48
|
+
setup_attributes_that :should_be_squeezed, args,
|
|
49
|
+
{
|
|
50
|
+
:squeeze_other_str => [:squeeze_other_str, :other_str, :string, :with_string,
|
|
51
|
+
:with_characters, :with_character, :characters]
|
|
52
|
+
}, :squeeze_other_str
|
|
40
53
|
end
|
|
41
|
-
alias_method :squeeze_attribute,
|
|
54
|
+
alias_method :squeeze_attribute, :squeeze_attributes
|
|
55
|
+
alias_method :squeezes_attribute, :squeeze_attributes
|
|
56
|
+
alias_method :squeezes_attributes, :squeeze_attributes
|
|
42
57
|
end # module ClassMethods
|
|
43
58
|
|
|
44
59
|
# Squeezes white characters in attributes, removes leading and trailing spaces and newlines.
|
|
@@ -48,23 +63,27 @@ module ActiveModel
|
|
|
48
63
|
# used with multibyte strings (containing diacritics).
|
|
49
64
|
#
|
|
50
65
|
# @note If a value of currently processed attribute is an array
|
|
51
|
-
# then any element of the array is changed.
|
|
66
|
+
# then any element of the array is changed. The same with hash (its values are changed).
|
|
52
67
|
#
|
|
53
68
|
# @return [void]
|
|
54
69
|
def squish_attributes
|
|
55
70
|
filter_attrs_from_set(:should_be_squished) do |atr|
|
|
56
|
-
|
|
71
|
+
AFHelpers.each_element(atr, String) do |v|
|
|
57
72
|
v.mb_chars.squish.to_s
|
|
58
73
|
end
|
|
59
74
|
end
|
|
60
75
|
end
|
|
76
|
+
filtering_method :squish_attributes, :should_be_squished
|
|
77
|
+
|
|
61
78
|
# This submodule contains class methods used to easily define filter.
|
|
62
79
|
module ClassMethods
|
|
63
80
|
# Registers attributes that should be squished.
|
|
64
81
|
def squish_attributes(*args)
|
|
65
82
|
attributes_that(:should_be_squished, args)
|
|
66
83
|
end
|
|
67
|
-
alias_method :squish_attribute,
|
|
84
|
+
alias_method :squish_attribute, :squish_attributes
|
|
85
|
+
alias_method :squishes_attribute, :squish_attributes
|
|
86
|
+
alias_method :squishes_attributes, :squish_attributes
|
|
68
87
|
end # module ClassMethods
|
|
69
88
|
end # module Squeeze
|
|
70
89
|
|