attribute-filters 1.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/.gemtest +0 -0
- data/.rspec +1 -0
- data/.yardopts +10 -0
- data/ChangeLog +23 -0
- data/LGPL-LICENSE +169 -0
- data/Manifest.txt +27 -0
- data/README.rdoc +87 -0
- data/Rakefile +90 -0
- data/attribute-filters.gemspec +61 -0
- data/docs/COPYING +57 -0
- data/docs/HISTORY +6 -0
- data/docs/LEGAL +11 -0
- data/docs/LGPL-LICENSE +166 -0
- data/docs/TODO +6 -0
- data/docs/rdoc.css +20 -0
- data/init.rb +1 -0
- data/lib/attribute-filters/attribute_set.rb +18 -0
- data/lib/attribute-filters/attribute_set_query.rb +90 -0
- data/lib/attribute-filters/common_filters.rb +4 -0
- data/lib/attribute-filters/dsl_filters.rb +145 -0
- data/lib/attribute-filters/dsl_sets.rb +104 -0
- data/lib/attribute-filters/railtie.rb +46 -0
- data/lib/attribute-filters/version.rb +24 -0
- data/lib/attribute-filters.rb +11 -0
- data/spec/attribute-filters_spec.rb +5 -0
- data/spec/spec_helper.rb +2 -0
- data.tar.gz.sig +0 -0
- metadata +215 -0
- metadata.gz.sig +0 -0
data/docs/LGPL-LICENSE
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
|
|
2
|
+
= GNU LESSER GENERAL PUBLIC LICENSE
|
|
3
|
+
Version 3, 29 June 2007
|
|
4
|
+
|
|
5
|
+
Copyright (C) 2007 Free Software Foundation, Inc. < http://fsf.org/ >
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
7
|
+
of this license document, but changing it is not allowed.
|
|
8
|
+
|
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
11
|
+
License, supplemented by the additional permissions listed below.
|
|
12
|
+
|
|
13
|
+
=== 0. Additional Definitions.
|
|
14
|
+
|
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
17
|
+
General Public License.
|
|
18
|
+
|
|
19
|
+
The Library" refers to a covered work governed by this License,
|
|
20
|
+
other than an Application or a Combined Work as defined below.
|
|
21
|
+
|
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
25
|
+
of using an interface provided by the Library.
|
|
26
|
+
|
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
28
|
+
Application with the Library. The particular version of the Library
|
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
|
30
|
+
Version".
|
|
31
|
+
|
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
35
|
+
based on the Application, and not on the Linked Version.
|
|
36
|
+
|
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
38
|
+
object code and/or source code for the Application, including any data
|
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
41
|
+
|
|
42
|
+
=== 1. Exception to Section 3 of the GNU GPL.
|
|
43
|
+
|
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
|
46
|
+
|
|
47
|
+
=== 2. Conveying Modified Versions.
|
|
48
|
+
|
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
|
51
|
+
that uses the facility (other than as an argument passed when the
|
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
|
53
|
+
version:
|
|
54
|
+
|
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
|
56
|
+
ensure that, in the event an Application does not supply the
|
|
57
|
+
function or data, the facility still operates, and performs
|
|
58
|
+
whatever part of its purpose remains meaningful, or
|
|
59
|
+
|
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
61
|
+
this License applicable to that copy.
|
|
62
|
+
|
|
63
|
+
=== 3. Object Code Incorporating Material from Library Header Files.
|
|
64
|
+
|
|
65
|
+
The object code form of an Application may incorporate material from
|
|
66
|
+
a header file that is part of the Library. You may convey such object
|
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
|
68
|
+
material is not limited to numerical parameters, data structure
|
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
|
71
|
+
|
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
|
73
|
+
Library is used in it and that the Library and its use are
|
|
74
|
+
covered by this License.
|
|
75
|
+
|
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
77
|
+
document.
|
|
78
|
+
|
|
79
|
+
=== 4. Combined Works.
|
|
80
|
+
|
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
|
82
|
+
taken together, effectively do not restrict modification of the
|
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
|
85
|
+
the following:
|
|
86
|
+
|
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
88
|
+
the Library is used in it and that the Library and its use are
|
|
89
|
+
covered by this License.
|
|
90
|
+
|
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
92
|
+
document.
|
|
93
|
+
|
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
|
95
|
+
execution, include the copyright notice for the Library among
|
|
96
|
+
these notices, as well as a reference directing the user to the
|
|
97
|
+
copies of the GNU GPL and this license document.
|
|
98
|
+
|
|
99
|
+
d) Do one of the following:
|
|
100
|
+
|
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
102
|
+
License, and the Corresponding Application Code in a form
|
|
103
|
+
suitable for, and under terms that permit, the user to
|
|
104
|
+
recombine or relink the Application with a modified version of
|
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
107
|
+
Corresponding Source.
|
|
108
|
+
|
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
111
|
+
a copy of the Library already present on the user's computer
|
|
112
|
+
system, and (b) will operate properly with a modified version
|
|
113
|
+
of the Library that is interface-compatible with the Linked
|
|
114
|
+
Version.
|
|
115
|
+
|
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
117
|
+
be required to provide such information under section 6 of the
|
|
118
|
+
GNU GPL, and only to the extent that such information is
|
|
119
|
+
necessary to install and execute a modified version of the
|
|
120
|
+
Combined Work produced by recombining or relinking the
|
|
121
|
+
Application with a modified version of the Linked Version. (If
|
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
126
|
+
for conveying Corresponding Source.)
|
|
127
|
+
|
|
128
|
+
=== 5. Combined Libraries.
|
|
129
|
+
|
|
130
|
+
You may place library facilities that are a work based on the
|
|
131
|
+
Library side by side in a single library together with other library
|
|
132
|
+
facilities that are not Applications and are not covered by this
|
|
133
|
+
License, and convey such a combined library under terms of your
|
|
134
|
+
choice, if you do both of the following:
|
|
135
|
+
|
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
|
137
|
+
on the Library, uncombined with any other library facilities,
|
|
138
|
+
conveyed under the terms of this License.
|
|
139
|
+
|
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
|
141
|
+
is a work based on the Library, and explaining where to find the
|
|
142
|
+
accompanying uncombined form of the same work.
|
|
143
|
+
|
|
144
|
+
=== 6. Revised Versions of the GNU Lesser General Public License.
|
|
145
|
+
|
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
148
|
+
versions will be similar in spirit to the present version, but may
|
|
149
|
+
differ in detail to address new problems or concerns.
|
|
150
|
+
|
|
151
|
+
Each version is given a distinguishing version number. If the
|
|
152
|
+
Library as you received it specifies that a certain numbered version
|
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
|
154
|
+
applies to it, you have the option of following the terms and
|
|
155
|
+
conditions either of that published version or of any later version
|
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
|
160
|
+
|
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
164
|
+
permanent authorization for you to choose that version for the
|
|
165
|
+
Library.
|
|
166
|
+
|
data/docs/TODO
ADDED
data/docs/rdoc.css
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
@import "rdoc_base.css";
|
|
2
|
+
|
|
3
|
+
#documentation > ul:first-of-type {
|
|
4
|
+
padding-bottom: 2em;
|
|
5
|
+
padding-top: 1.5em;
|
|
6
|
+
background-position: 20em 0%;
|
|
7
|
+
background-repeat: no-repeat;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
#documentation .method-description > p:first-of-type + p {
|
|
11
|
+
margin-top: 0.5em;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#documentation .method-description > ul {
|
|
15
|
+
margin-left: 1.2em;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
#documentation .method-description > p + ul {
|
|
19
|
+
margin-left: 1.8em;
|
|
20
|
+
}
|
data/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'attribute-filters'
|
|
@@ -0,0 +1,18 @@
|
|
|
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::AttributeSet class
|
|
8
|
+
# used to interact with attribute sets.
|
|
9
|
+
|
|
10
|
+
require 'set'
|
|
11
|
+
|
|
12
|
+
# @abstract This namespace is shared with ActveModel.
|
|
13
|
+
module ActiveModel
|
|
14
|
+
# This class is a data structure used to store
|
|
15
|
+
# sets of attributes.
|
|
16
|
+
class AttributeSet < ::Set
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -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::AttributeSet::Query class
|
|
8
|
+
# used to interact with attribute sets.
|
|
9
|
+
|
|
10
|
+
module ActiveModel
|
|
11
|
+
class AttributeSet
|
|
12
|
+
# This class contains proxy methods used to interact with
|
|
13
|
+
# AttributeSet instances. It's responsible for all of the DSL magic
|
|
14
|
+
# that allows sweet constructs like:
|
|
15
|
+
# some_attribute_set.all.present?
|
|
16
|
+
class Query < BasicObject
|
|
17
|
+
# Creates new query object.
|
|
18
|
+
#
|
|
19
|
+
# @param attribute_set_object [AttributeSet] attribute set for which the query will be made
|
|
20
|
+
# @param am_object [Object] model object which has access to attributes (may be an instance of ActiveRecord or similar)
|
|
21
|
+
def initialize(attribute_set_object, am_object)
|
|
22
|
+
@attribute_set = attribute_set_object
|
|
23
|
+
@am_object = am_object
|
|
24
|
+
@next_method = nil
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# This is proxy method that causes some calls to be
|
|
28
|
+
# queued to for the next call. Is allows to create semi-natural
|
|
29
|
+
# syntax when querying attribute sets.
|
|
30
|
+
#
|
|
31
|
+
# When +method_sym+ is set to +:all+ or +:any+ then new query is created
|
|
32
|
+
# and the next method given in chain is passed to each element of the set with
|
|
33
|
+
# question mark added to its name.
|
|
34
|
+
#
|
|
35
|
+
# Example:
|
|
36
|
+
# some_attribute_set.all.present?
|
|
37
|
+
# is converted to
|
|
38
|
+
# some_attribute_set.all? { |atr| atr.present? }
|
|
39
|
+
#
|
|
40
|
+
# When +method_sym+ is set to +list+ or +show+ then new query
|
|
41
|
+
# is constructed and the next method given in chain is passed to
|
|
42
|
+
# any element collected by applying +select+ to set.
|
|
43
|
+
#
|
|
44
|
+
# Example:
|
|
45
|
+
# some_attribute_set.list.present?
|
|
46
|
+
# is converted to
|
|
47
|
+
# some_attribute_set.select { |atr| atr.present? }
|
|
48
|
+
#
|
|
49
|
+
# @param method_sym [Symbol,String] name of method that will be queued or called on attribute set
|
|
50
|
+
# @param args [Array] optional arguments to be passed to a method call or to a queued method call
|
|
51
|
+
# @yield optional block to be passed to a method call or to a queued method call
|
|
52
|
+
def method_missing(method_sym, *args, &block)
|
|
53
|
+
case method_sym.to_sym
|
|
54
|
+
when :are, :is, :be, :should
|
|
55
|
+
return self
|
|
56
|
+
end
|
|
57
|
+
if @method_to_call.nil?
|
|
58
|
+
case method_sym.to_sym
|
|
59
|
+
when :all, :any
|
|
60
|
+
::ActiveModel::AttributeSet::Query.new(@attribute_set, @am_object). # new obj. == thread-safe
|
|
61
|
+
next_step(method_sym.to_s << "?", args, block)
|
|
62
|
+
when :list, :show
|
|
63
|
+
::ActiveModel::AttributeSet::Query.new(@attribute_set, @am_object).
|
|
64
|
+
next_step(:select, args, block)
|
|
65
|
+
else
|
|
66
|
+
@attribute_set.method(method_sym).call(*args, &block)
|
|
67
|
+
end
|
|
68
|
+
else
|
|
69
|
+
method_sym, args, block = @next_method
|
|
70
|
+
@next_method = nil
|
|
71
|
+
@attribute_set.method(m).call { |a| @am_object[a].method(method_sym).call(*args, &block) }
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
protected
|
|
76
|
+
|
|
77
|
+
# Queues any method of the given name to be called when next
|
|
78
|
+
# query (method call) is made.
|
|
79
|
+
#
|
|
80
|
+
# @param method_name [Symbol] name of a method to be called on next call to any query method
|
|
81
|
+
# @param args [Array] arguments to be passed to the called method
|
|
82
|
+
# @param block [Proc] code block to be passed to the called method
|
|
83
|
+
# @return [AttributeSet] current query object
|
|
84
|
+
def next_step(method_name, args, block)
|
|
85
|
+
@next_method = [method_name, args, block]
|
|
86
|
+
return self
|
|
87
|
+
end
|
|
88
|
+
end # class Query
|
|
89
|
+
end # class AttributeSet
|
|
90
|
+
end # module ActiveModel
|
|
@@ -0,0 +1,145 @@
|
|
|
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 modules with methods that create DSL for managing attribute filters.
|
|
8
|
+
|
|
9
|
+
module ActiveModel
|
|
10
|
+
module AttributeFilters
|
|
11
|
+
# Gets names of attributes for which filters should be applied by
|
|
12
|
+
# selecting attributes that 1. have changed and 2. their names belong to
|
|
13
|
+
# the given set of attributes.
|
|
14
|
+
#
|
|
15
|
+
# @param process_unchanged [Boolean] if set to +true+ then all accessible attributes will
|
|
16
|
+
# be tested (which are also present in set o course), not just the changed ones
|
|
17
|
+
# @param set_name [AttributeSet] set of attributes used to select attributes
|
|
18
|
+
# @return [Hash{Symbol => Object}] list of attributes (attribute name => previous_value)
|
|
19
|
+
def attributes_to_filter(set_name, process_unchanged = false)
|
|
20
|
+
@__attr_op_apply ||= Array.new
|
|
21
|
+
if process_unchanged
|
|
22
|
+
@__attr_op_apply[0] ||= Hash.new
|
|
23
|
+
@__attr_op_apply[0][set_name] || begin
|
|
24
|
+
buf = @__attr_op_apply[0][set_name] = Hash.new
|
|
25
|
+
(attribute_set(set_name) & self.class.accessible_attributes.to_a).each do |a|
|
|
26
|
+
buf[a] = nil
|
|
27
|
+
end
|
|
28
|
+
buf
|
|
29
|
+
end
|
|
30
|
+
else
|
|
31
|
+
@__attr_op_apply[1] ||= Hash.new
|
|
32
|
+
@__attr_op_apply[1][set_name] ||= changed_attributes.select do |a,pv|
|
|
33
|
+
attribute_set(set_name).include?(a)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# This generic method applies a result of execution of the passed block
|
|
39
|
+
# to each of all changed attributes that are present in the given
|
|
40
|
+
# set of attributes. It is useful in creating filtering methods.
|
|
41
|
+
# The result of a block is used to set a new value for each processed attribute.
|
|
42
|
+
#
|
|
43
|
+
# @param set_name [Symbol] name of the attributes set
|
|
44
|
+
# @param args [Array] optional additional arguments that will be passed to the block
|
|
45
|
+
# @yield [attribute_value, previous_value, set_name, attribute_name, *args] block that will be called for each attribute
|
|
46
|
+
# @yieldparam attribute_value [Object] current attribute value that should be altered
|
|
47
|
+
# @yieldparam previous_value [Object] the value of an attribute before change was made by ORM
|
|
48
|
+
# @yieldparam set_name [Symbol] a name of the processed attribute set
|
|
49
|
+
# @yieldparam attribute_name [Object] a name of currently processed attribute
|
|
50
|
+
# @yieldparam args [Array] optional arguments passed to the method
|
|
51
|
+
# @yieldreturn [Object] the result of calling the block
|
|
52
|
+
# @return [void]
|
|
53
|
+
#
|
|
54
|
+
# @example
|
|
55
|
+
# class User < ActiveRecord::Base
|
|
56
|
+
#
|
|
57
|
+
# attributes_that :should_be_downcased => [ :username, :email ]
|
|
58
|
+
# before_filter :downcase_names
|
|
59
|
+
#
|
|
60
|
+
# def downcase_names
|
|
61
|
+
# filter_attributes_that :should_be_downcased do |atr|
|
|
62
|
+
# atr.mb_chars.downcase.to_s
|
|
63
|
+
# end
|
|
64
|
+
# end
|
|
65
|
+
#
|
|
66
|
+
# end
|
|
67
|
+
def filter_attrs_from_set(set_name, *args, &block)
|
|
68
|
+
flags = attr_filter_process_flags(args)
|
|
69
|
+
pb = flags[:process_blank]
|
|
70
|
+
pu = flags[:process_unchanged]
|
|
71
|
+
attributes_to_filter(set_name, pu).each do |a, pv|
|
|
72
|
+
v = self[a]
|
|
73
|
+
pv = v if pu
|
|
74
|
+
self[a] = yield(v, pv, set_name, a, *args) if (pb || v.present?)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
alias_method :attribute_filter_for_set, :filter_attrs_from_set
|
|
78
|
+
alias_method :filter_attributes_which, :filter_attrs_from_set
|
|
79
|
+
alias_method :filter_attributes_that, :filter_attrs_from_set
|
|
80
|
+
alias_method :filter_attributes_that_are, :filter_attrs_from_set
|
|
81
|
+
alias_method :filter_attributes_which_are, :filter_attrs_from_set
|
|
82
|
+
alias_method :alter_attributes_which, :filter_attrs_from_set
|
|
83
|
+
alias_method :alter_attributes_that, :filter_attrs_from_set
|
|
84
|
+
alias_method :alter_attributes_that_are, :filter_attrs_from_set
|
|
85
|
+
alias_method :alter_attributes_which_are, :filter_attrs_from_set
|
|
86
|
+
|
|
87
|
+
# This generic method calls the passed block
|
|
88
|
+
# on each of all changed attributes that are present in the given
|
|
89
|
+
# set of attributes. It is useful in creating filtering methods.
|
|
90
|
+
# The result of a block is ignored, so the only effective way of
|
|
91
|
+
# altering attribute values is calling altering method on attributes
|
|
92
|
+
# (usually with bang methods).
|
|
93
|
+
#
|
|
94
|
+
# @param set_name [Symbol] name of the attributes set
|
|
95
|
+
# @param args [Array] optional additional arguments that will be passed to the block
|
|
96
|
+
# @yield [attribute_value, previous_value, set_name, attribute_name, *args] block that will be called for each attribute
|
|
97
|
+
# @yieldparam attribute_value [Object] current attribute value that should be altered
|
|
98
|
+
# @yieldparam previous_value [Object] the value of an attribute before change was made by ORM
|
|
99
|
+
# @yieldparam set_name [Symbol] a name of the processed attribute set
|
|
100
|
+
# @yieldparam attribute_name [Object] a name of currently processed attribute
|
|
101
|
+
# @yieldparam args [Array] optional arguments passed to the method
|
|
102
|
+
# @yieldreturn [void]
|
|
103
|
+
# @return [void]
|
|
104
|
+
#
|
|
105
|
+
# @example
|
|
106
|
+
# class User < ActiveRecord::Base
|
|
107
|
+
#
|
|
108
|
+
# attributes_that :should_be_stripped => [ :username, :email ]
|
|
109
|
+
# before_filter :strip_names
|
|
110
|
+
#
|
|
111
|
+
# def strip_names
|
|
112
|
+
# for_attributes_that :should_be_stripped do |atr|
|
|
113
|
+
# atr.strip!
|
|
114
|
+
# end
|
|
115
|
+
# end
|
|
116
|
+
#
|
|
117
|
+
# end
|
|
118
|
+
def call_attrs_from_set(set_name, *args, &block)
|
|
119
|
+
flags = attr_filter_process_flags(args)
|
|
120
|
+
pb = flags[:process_blank]
|
|
121
|
+
pu = flags[:process_unchanged]
|
|
122
|
+
attributes_to_filter(set_name, pu).each do |a, pv|
|
|
123
|
+
v = self[a]
|
|
124
|
+
pv = v if pu
|
|
125
|
+
yield(v, pv, set_name, a, *args) if (pb || v.present?)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
alias_method :attribute_call_for_set, :call_attrs_from_set
|
|
129
|
+
alias_method :for_attributes_which, :call_attrs_from_set
|
|
130
|
+
alias_method :for_attributes_that, :call_attrs_from_set
|
|
131
|
+
alias_method :for_attributes_that_are, :call_attrs_from_set
|
|
132
|
+
alias_method :for_attributes_which_are, :call_attrs_from_set
|
|
133
|
+
|
|
134
|
+
private
|
|
135
|
+
|
|
136
|
+
def attr_filter_process_flags(args)
|
|
137
|
+
flags = { :process_blank => false, :process_unchanged => false }
|
|
138
|
+
while flags.key?(a=args[0]) do
|
|
139
|
+
flags[a] = !!args.shift
|
|
140
|
+
end
|
|
141
|
+
flags
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
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 modules defining methods that create DSL for managing attribute sets.
|
|
8
|
+
|
|
9
|
+
module ActiveModel
|
|
10
|
+
# This module contains instance methods for getting and setting
|
|
11
|
+
# attricute sets established in classes (models).
|
|
12
|
+
module AttributeFilters
|
|
13
|
+
def self.included(base)
|
|
14
|
+
base.extend ClassMethods
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Allows to access the given attribute set.
|
|
18
|
+
#
|
|
19
|
+
# @param set_name [Symbol] name of attribute set
|
|
20
|
+
# @return [AttributeSet] attribute set
|
|
21
|
+
def attribute_set(set_name)
|
|
22
|
+
ActiveModel::AttributeSet::Query.new(self.class.attribute_set(set_name), self)
|
|
23
|
+
end
|
|
24
|
+
alias_method :attributes_that_are, :attribute_set
|
|
25
|
+
alias_method :are_attributes_that_are, :attribute_set
|
|
26
|
+
alias_method :from_attributes_that_are, :attribute_set
|
|
27
|
+
alias_method :within_attributes_that_are, :attribute_set
|
|
28
|
+
alias_method :attributes_that, :attribute_set
|
|
29
|
+
alias_method :attributes_are, :attribute_set
|
|
30
|
+
alias_method :attributes_for, :attribute_set
|
|
31
|
+
alias_method :are_attributes, :attribute_set
|
|
32
|
+
alias_method :are_attributes_for, :attribute_set
|
|
33
|
+
alias_method :attributes_set, :attribute_set
|
|
34
|
+
alias_method :are_properties_that_are, :attribute_set
|
|
35
|
+
alias_method :properties_that_are, :attribute_set
|
|
36
|
+
alias_method :properties_that, :attribute_set
|
|
37
|
+
alias_method :properties_are, :attribute_set
|
|
38
|
+
alias_method :properties_for, :attribute_set
|
|
39
|
+
alias_method :are_properties, :attribute_set
|
|
40
|
+
alias_method :are_properties_for, :attribute_set
|
|
41
|
+
alias_method :are_properties_that_are, :attribute_set
|
|
42
|
+
alias_method :properties_set, :attribute_set
|
|
43
|
+
|
|
44
|
+
# This module contains class methods
|
|
45
|
+
# that create DSL for managing attribute sets.
|
|
46
|
+
module ClassMethods
|
|
47
|
+
# @overload attribute_set()
|
|
48
|
+
# Gets all defined attribute sets.
|
|
49
|
+
# @return [Hash{Symbol => AttributeSet}] the collection of attribute sets indexed by their names
|
|
50
|
+
#
|
|
51
|
+
# @overload attribute_set(set_name)
|
|
52
|
+
# Gets the contents of an attribute set of the given name.
|
|
53
|
+
# @param set_name [Symbol,String] name of a set
|
|
54
|
+
# @return [AttributeSet] the collection of attribute sets
|
|
55
|
+
#
|
|
56
|
+
# @overload attribute_set(set_name, *attribute_names)
|
|
57
|
+
# Creates new set of attributes of the given name.
|
|
58
|
+
# @param set_name [Symbol,String] name of a set
|
|
59
|
+
# @param attribute_names [Array<Symbol,String>] names of attributes to be stored in set
|
|
60
|
+
# @return [nil]
|
|
61
|
+
#
|
|
62
|
+
# @overload attribute_set(set_options)
|
|
63
|
+
# Creates one new set or many new sets of attribute of the given name.
|
|
64
|
+
# @param set_options [Hash{Symbol,String => Array<Symbol,String>}] hash containing set names and arrays of attributes
|
|
65
|
+
# @return [nil]
|
|
66
|
+
def attribute_set(*args)
|
|
67
|
+
case args.size
|
|
68
|
+
when 0
|
|
69
|
+
attribute_sets
|
|
70
|
+
when 1
|
|
71
|
+
a = args.first
|
|
72
|
+
if a.is_a?(Hash)
|
|
73
|
+
a.each_pair { |k,v| attribute_set(k,v) }
|
|
74
|
+
nil
|
|
75
|
+
else
|
|
76
|
+
attribute_sets[a.to_sym] || ActiveModel::AttributeSet.new.freeze
|
|
77
|
+
end
|
|
78
|
+
else
|
|
79
|
+
set_name = args.shift.to_sym
|
|
80
|
+
attribute_sets[set_name] = ActiveModel::AttributeSet.new(args.flatten.compact.map{|x|x.to_s}).freeze
|
|
81
|
+
nil
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
alias_method :attributes_that_are, :attribute_set
|
|
85
|
+
alias_method :attributes_that, :attribute_set
|
|
86
|
+
alias_method :attributes_are, :attribute_set
|
|
87
|
+
alias_method :attributes_for, :attribute_set
|
|
88
|
+
alias_method :attributes_set, :attribute_set
|
|
89
|
+
alias_method :properties_that_are, :attribute_set
|
|
90
|
+
alias_method :properties_that, :attribute_set
|
|
91
|
+
alias_method :properties_are, :attribute_set
|
|
92
|
+
alias_method :properties_for, :attribute_set
|
|
93
|
+
alias_method :properties_set, :attribute_set
|
|
94
|
+
|
|
95
|
+
# Gets all defined attribute sets.
|
|
96
|
+
# @return [Hash{Symbol => AttributeSet}] the collection of attribute sets indexed by their names
|
|
97
|
+
def attribute_sets
|
|
98
|
+
@__attribute_sets ||= Hash.new
|
|
99
|
+
end
|
|
100
|
+
alias_method :attributes_sets, :attribute_sets
|
|
101
|
+
alias_method :properties_sets, :attribute_sets
|
|
102
|
+
end # module ClassMethods
|
|
103
|
+
end # module AttributeMethods
|
|
104
|
+
end # module ActiveModel
|
|
@@ -0,0 +1,46 @@
|
|
|
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 loads Attribute Filters goodies into Rails.
|
|
8
|
+
|
|
9
|
+
require 'attribute-filters'
|
|
10
|
+
|
|
11
|
+
module AttributeFilters
|
|
12
|
+
require 'rails'
|
|
13
|
+
|
|
14
|
+
# This class is a glue that allows us to integrate with Rails.
|
|
15
|
+
class Railtie < ::Rails::Railtie
|
|
16
|
+
# Alters ActiveModel::AttributeMethods.inluded method so
|
|
17
|
+
# when that module is included the ActiveModel::AttributeFilters
|
|
18
|
+
# module is also included.
|
|
19
|
+
def self.insert
|
|
20
|
+
require 'active_model'
|
|
21
|
+
if defined?(ActiveModel::AttributeMethods)
|
|
22
|
+
|
|
23
|
+
ActiveModel::AttributeMethods.class_eval do
|
|
24
|
+
|
|
25
|
+
def self.included_with_attribute_methods(base)
|
|
26
|
+
base.class_eval do
|
|
27
|
+
include ActiveModel::AttributeFilters
|
|
28
|
+
if method_defined?(:included_without_attribute_methods)
|
|
29
|
+
included_without_attribute_methods(base)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
if singleton_class.method_defined?(:included)
|
|
34
|
+
singleton_class.send(:alias_method_chain, :included, :attribute_methods)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end # ActiveModel::AttributeMethods.class_eval
|
|
38
|
+
|
|
39
|
+
end # if defined?(ActiveModel::AttributeMethods)
|
|
40
|
+
end # def self.insert
|
|
41
|
+
end # class Railtie
|
|
42
|
+
|
|
43
|
+
class Railtie
|
|
44
|
+
AttributeFilters::Railtie.insert
|
|
45
|
+
end # class Railtie
|
|
46
|
+
end # module AttributeFilters
|
|
@@ -0,0 +1,24 @@
|
|
|
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 version information.
|
|
8
|
+
|
|
9
|
+
module AttributeFilters
|
|
10
|
+
# @private
|
|
11
|
+
DEVELOPER = 'Paweł Wilk'
|
|
12
|
+
# @private
|
|
13
|
+
EMAIL = 'pw@gnu.org'
|
|
14
|
+
# @private
|
|
15
|
+
VERSION = '1.0.0'
|
|
16
|
+
# @private
|
|
17
|
+
NAME = 'attribute-filters'
|
|
18
|
+
# @private
|
|
19
|
+
SUMMARY = 'Concise way of filtering model attributes in Rails'
|
|
20
|
+
# @private
|
|
21
|
+
URL = 'https://rubygems.org/gems/attribute-filters/'
|
|
22
|
+
# @private
|
|
23
|
+
DESCRIPTION = 'Simple extension for Rails that adds some syntactic sugar (filters, attribute sets) to your models.'
|
|
24
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'attribute-filters'
|
|
4
|
+
require 'attribute-filters/version'
|
|
5
|
+
require 'attribute-filters/attribute_set'
|
|
6
|
+
require 'attribute-filters/attribute_set_query'
|
|
7
|
+
|
|
8
|
+
require 'attribute-filters/dsl_sets'
|
|
9
|
+
require 'attribute-filters/dsl_filters'
|
|
10
|
+
|
|
11
|
+
require 'attribute-filters/railtie' if defined? ::Rails
|
data/spec/spec_helper.rb
ADDED
data.tar.gz.sig
ADDED
|
Binary file
|