data_filter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/data_filter.gemspec +24 -0
- data/lib/data_filter/filter_set.rb +185 -0
- data/lib/data_filter/keyword_filter.rb +36 -0
- data/lib/data_filter/like_filter.rb +61 -0
- data/lib/data_filter/range_filter.rb +53 -0
- data/lib/data_filter/range_overlap_filter.rb +52 -0
- data/lib/data_filter/truthy_filter.rb +41 -0
- data/lib/data_filter/version.rb +3 -0
- data/lib/data_filter.rb +11 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c24377c49a51265afaddbcc4a5a2c595a408e5a7
|
4
|
+
data.tar.gz: c1cdf33d9fd1fb2fa603830f532bb8f37498bbfe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 06d289a95c37300bd77ec74d7fb5f162a5c50fc2fab77b2db4e23e1ca0f9ffeecf806d53577ac5b535cf9b499ef801f4ac8dbe4f2d7b5e4cc6c940f79bd52700
|
7
|
+
data.tar.gz: 505a8fa5f671a0c481b0f6244298eacf7f5e9e92db7d1ebde08e8a2451b81673961387f85e207c4e7687d914e0d7c94374d93f7c869b827d085ce003836e2c73
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Josh Bodah
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# DataFilter
|
2
|
+
|
3
|
+
an extensible DSL for filtering data sets
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```rb
|
8
|
+
gem install data_filter
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
`DataFilter::FilterSet::create` provides a DSL for creating a collection
|
14
|
+
of filters which can be applied to your data. The DSL is designed to be
|
15
|
+
controller friendly and will only apply filters if a parameter is specified.
|
16
|
+
If a filter doesn't do what you need then you can pass any object that responds
|
17
|
+
to `#call` (e.g. a lambda) to `add_filter`.
|
18
|
+
|
19
|
+
```rb
|
20
|
+
filter_set = DataFilter::FilterSet.create do
|
21
|
+
# Fuzzy comparison
|
22
|
+
like_filter :name, by: params[:name]
|
23
|
+
|
24
|
+
# Keyword search
|
25
|
+
keyword_filter [:gender], by: params[:gender]
|
26
|
+
|
27
|
+
# Match truthy/falsey values
|
28
|
+
truthy_filter :student, match: params[:is_student]
|
29
|
+
|
30
|
+
# Check if within range
|
31
|
+
range_filter :age, ceiling: params[:max_age]
|
32
|
+
|
33
|
+
# Add a custom filter
|
34
|
+
add_filter -> (user) { user if user.student || user.age > 25 }
|
35
|
+
end
|
36
|
+
|
37
|
+
data = [
|
38
|
+
User.create(name: 'Josh', age: 26, student: false, gender: :male),
|
39
|
+
User.create(name: 'Lauren', age: 25, student: true, gender: :female)
|
40
|
+
]
|
41
|
+
|
42
|
+
# By default data which doesn't match all of the filters will be filtered out
|
43
|
+
filter_set.call(data)
|
44
|
+
```
|
45
|
+
|
46
|
+
## License
|
47
|
+
|
48
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
49
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "data_filter"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/data_filter.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'data_filter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "data_filter"
|
8
|
+
spec.version = DataFilter::VERSION
|
9
|
+
spec.authors = ["Josh Bodah"]
|
10
|
+
spec.email = ["jb3689@yahoo.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{an extensible DSL for filtering data sets}
|
13
|
+
spec.homepage = "https://github.com/backupify/data_filter"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
end
|
@@ -0,0 +1,185 @@
|
|
1
|
+
module DataFilter
|
2
|
+
# Represents a collection of data filters that can be called on
|
3
|
+
# data. Provides a DSL for creating a filter set and only adding
|
4
|
+
# filters the filters that you need.
|
5
|
+
class FilterSet
|
6
|
+
attr_reader :filters
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@filters = []
|
10
|
+
end
|
11
|
+
|
12
|
+
# Add a filter to the filter set
|
13
|
+
#
|
14
|
+
# @param filter [#call]
|
15
|
+
# a callable filter. Can be a proc, lambda, or any object
|
16
|
+
# that responds to #call
|
17
|
+
# @return [FilterSet] the amended filter set
|
18
|
+
def add_filter(filter)
|
19
|
+
@filters << filter
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
# Run the filter set on a single data item
|
24
|
+
#
|
25
|
+
# @param item [Object] some item that we want to pass through all
|
26
|
+
# of the filters in the filter set
|
27
|
+
# @return [Object, nil] the original item or nil
|
28
|
+
def filter(item)
|
29
|
+
@filters.reduce(item) { |i, filter| i if filter.call(i) }
|
30
|
+
end
|
31
|
+
|
32
|
+
# Run the filter set on a collection of data items
|
33
|
+
#
|
34
|
+
# @param items [Enumerable<Object>] collection of items that we want to
|
35
|
+
# pass through all of the filters in the filter set
|
36
|
+
# @return [Enumerable<Object>] the filtered results
|
37
|
+
def batch(items)
|
38
|
+
items.select { |i| filter(i) }
|
39
|
+
end
|
40
|
+
|
41
|
+
# A DSL for creating a series of filters that can be called
|
42
|
+
#
|
43
|
+
# Provides a cleaner way to define a {DataFilter::FilterSet}
|
44
|
+
# with a bunch of different filters
|
45
|
+
#
|
46
|
+
# Conditionally adds filters to the set based on whether or not
|
47
|
+
# any valid search terms are provided (useful for Controller params)
|
48
|
+
#
|
49
|
+
# @example Office365::Mail::MessagesController
|
50
|
+
# filter_set = DataFilter::FilterSet.create do
|
51
|
+
# like_filter :to, by: params[:to]
|
52
|
+
# like_filter :from, by: params[:from]
|
53
|
+
# like_filter :cc, by: params[:cc]
|
54
|
+
# like_filter :bcc, by: params[:bcc]
|
55
|
+
# like_filter :subject, by: params[:subject]
|
56
|
+
#
|
57
|
+
# keyword_filter [:to, :from, :cc, :bcc, :subject], by: params[:keyword]
|
58
|
+
#
|
59
|
+
# range_filter :date, floor: start_date, ceiling: end_date
|
60
|
+
#
|
61
|
+
# if params[:has_attachment] === true
|
62
|
+
# range_filter :attachment_count, floor: 1
|
63
|
+
# elsif params[:has_attachment] === false
|
64
|
+
# range_filter :attachment_count, ceiling: 0, nil_default: 0
|
65
|
+
# end
|
66
|
+
# end
|
67
|
+
module DSL
|
68
|
+
def self.included(base)
|
69
|
+
base.extend(ClassMethods)
|
70
|
+
end
|
71
|
+
|
72
|
+
module ClassMethods
|
73
|
+
# Initializes a new {DataFilter::FilterSet} using a block.
|
74
|
+
# The block conforms to the DSL defined in this method.
|
75
|
+
# Delegates undefined messages to the caller's scope.
|
76
|
+
#
|
77
|
+
# @yield the DSL block
|
78
|
+
# @return [DataFilter::FilterSet] the filter set evaluated
|
79
|
+
# with the DSL
|
80
|
+
def create(&block)
|
81
|
+
original_self = eval 'self', block.binding
|
82
|
+
instance = new
|
83
|
+
instance.instance_variable_set(:@original_self, original_self)
|
84
|
+
instance.instance_eval &block
|
85
|
+
instance
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Adds a {DataFilter::LikeFilter} to the filter set
|
90
|
+
#
|
91
|
+
# @param field_sym [Symbol] name of the data method we want
|
92
|
+
# to filter
|
93
|
+
# @option opts [Object] :by the value we want to use when
|
94
|
+
# filtering the data item
|
95
|
+
def like_filter(field_sym, opts = {})
|
96
|
+
if opts[:by]
|
97
|
+
@filters << LikeFilter.new(field_sym, opts[:by])
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
# Adds a {DataFilter::KeywordFilter} to the filter set
|
102
|
+
#
|
103
|
+
# @param field_syms [Array<Symbol>] a collection of all of the data
|
104
|
+
# methods we want to inspect when filtering
|
105
|
+
# @option opts [Object] :by the value we want to use when filtering
|
106
|
+
# the data item
|
107
|
+
def keyword_filter(field_syms, opts = {})
|
108
|
+
if opts[:by]
|
109
|
+
@filters << KeywordFilter.new(field_syms, opts[:by])
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# Adds a {DataFilter::RangeFilter} to the filter set
|
114
|
+
#
|
115
|
+
# @param field_sym [Symbol] name of the data method we want to
|
116
|
+
# filter
|
117
|
+
# @option opts [Comparable] :floor the range beginning we want to
|
118
|
+
# filter the data item by
|
119
|
+
# @option opts [Comparable] :ceiling the range end we want to filter
|
120
|
+
# the data item by
|
121
|
+
# @option opts [Comparable] :nil_default the value to use if the
|
122
|
+
# data item has no field value
|
123
|
+
def range_filter(field_sym, opts = {})
|
124
|
+
if opts[:floor] || opts[:ceiling]
|
125
|
+
@filters << RangeFilter.new(field_sym, **opts)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# Adds a {DataFilter::RangeOverlapFilter} to the filter set
|
130
|
+
#
|
131
|
+
# @param start_sym [Symbol] name of the start field we want to
|
132
|
+
# filter
|
133
|
+
# @param end_sym [Symbol] name of the end field we want to
|
134
|
+
# filter
|
135
|
+
# @option opts [Comparable] :floor the range beginning we want to
|
136
|
+
# filter the data item by
|
137
|
+
# @option opts [Comparable] :ceiling the range end we want to filter
|
138
|
+
# the data item by
|
139
|
+
# @option opts [Comparable] :nil_default the value to use if the
|
140
|
+
# data item has no field value
|
141
|
+
def range_overlap_filter(start_sym, end_sym, opts = {})
|
142
|
+
if opts[:floor] || opts[:ceiling]
|
143
|
+
@filters << RangeOverlapFilter.new(start_sym, end_sym, **opts)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
# Adds a {DataFilter::TruthyFilter} to the filter set
|
148
|
+
#
|
149
|
+
# @param field_sym [Symbol] name of the field to match on
|
150
|
+
# @param match [Object] truthy/falsey value to use to determine whether
|
151
|
+
# the filter should match/filter truthy fields or falsey fields
|
152
|
+
def truthy_filter(field_sym, match: nil)
|
153
|
+
# Skip filter if match is not specified
|
154
|
+
return if match.nil?
|
155
|
+
if is_falsey?(match)
|
156
|
+
@filters << TruthyFilter.new(field_sym, invert: true)
|
157
|
+
else
|
158
|
+
@filters << TruthyFilter.new(field_sym)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
# Used to support the DSL. Calls out to the parent scope if
|
163
|
+
# we receive a message we can't respond to
|
164
|
+
def method_missing(sym, *args, &block)
|
165
|
+
@original_self.send(sym, *args, &block)
|
166
|
+
end
|
167
|
+
|
168
|
+
# Used to support the DSL. Calls out to the parent scope if
|
169
|
+
# we receive a message we can't respond to
|
170
|
+
def respond_to_missing?(sym, include_all = false)
|
171
|
+
@original_self.respond_to?(sym, include_all)
|
172
|
+
end
|
173
|
+
|
174
|
+
private
|
175
|
+
|
176
|
+
|
177
|
+
# TODO DRY up
|
178
|
+
def is_falsey?(val)
|
179
|
+
[false, 'false'].include?(val)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
include DSL
|
184
|
+
end
|
185
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module DataFilter
|
2
|
+
# Used to filter a data item by a search term by seeing if
|
3
|
+
# ANY of the data fields' values are similar to the search term
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# object = MyModel.new(text: 'hello world', name: 'goodbye', phrase: 'yo')
|
7
|
+
# filter = DataFilter::KeywordFilter.new([:name, :phrase], 'hello')
|
8
|
+
# filter.call(object)
|
9
|
+
# # => nil
|
10
|
+
class KeywordFilter
|
11
|
+
# @param field_syms [Array<Symbol>] a collection of all of the data
|
12
|
+
# methods we want to inspect when filtering
|
13
|
+
# @param search_term [String] the value we want to use when filtering
|
14
|
+
# the data item
|
15
|
+
def initialize(field_syms, search_term)
|
16
|
+
@field_syms = field_syms
|
17
|
+
@search_term = search_term
|
18
|
+
end
|
19
|
+
|
20
|
+
# Filters the item
|
21
|
+
#
|
22
|
+
# @param item [Comparable] the item we want to filter
|
23
|
+
# @return [Object] the original data item
|
24
|
+
def call(item)
|
25
|
+
item if @field_syms.any? { |s| match?(item, s) }
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# :nodoc:
|
31
|
+
def match?(item, field_sym)
|
32
|
+
item.respond_to?(field_sym) &&
|
33
|
+
DataFilter::LikeFilter.new(field_sym, @search_term).call(item)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module DataFilter
|
2
|
+
# Used to filter a data item by a search term by seeing if
|
3
|
+
# the data field value is similar to the search term
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# object = MyModel.new(text: 'hello world!')
|
7
|
+
# filter = DataFilter::LikeFilter.new(:text, 'hello')
|
8
|
+
# filter.call(object)
|
9
|
+
# # => #<MyModel text: 'hello world'>
|
10
|
+
class LikeFilter
|
11
|
+
# @param field_sym [Symbol] name of the data method we want
|
12
|
+
# to filter
|
13
|
+
# @param search_term [String] the value we want to use when
|
14
|
+
# filtering the data item
|
15
|
+
def initialize(field_sym, search_term)
|
16
|
+
@field_sym = field_sym
|
17
|
+
@search_term = search_term
|
18
|
+
end
|
19
|
+
|
20
|
+
# Filters the item
|
21
|
+
#
|
22
|
+
# @param item [Object] the item we want to filter
|
23
|
+
# @return [Object, nil] the original data item
|
24
|
+
def call(item)
|
25
|
+
if item.respond_to?(@field_sym) &&
|
26
|
+
match?(item.public_send(@field_sym), @search_term)
|
27
|
+
item
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
# :nodoc:
|
34
|
+
def match?(actual, search_term)
|
35
|
+
case actual
|
36
|
+
when Hash
|
37
|
+
match?(actual.values.flatten, search_term)
|
38
|
+
when Array
|
39
|
+
actual.any? {|item| match?(item, search_term)}
|
40
|
+
when String
|
41
|
+
regexp =
|
42
|
+
normalize(search_term, true)
|
43
|
+
.split(' ')
|
44
|
+
.map {|term| Regexp.escape(term)}
|
45
|
+
.join('|')
|
46
|
+
.insert(0, '(')
|
47
|
+
.insert(-1, ')')
|
48
|
+
normalize(actual, false).match(/#{regexp}/i)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def normalize(str, use_cache = false)
|
53
|
+
if use_cache
|
54
|
+
@normalize_cache ||= {}
|
55
|
+
@normalize_cache[str] ||= str.gsub(/[^\w\s]/, ' ')
|
56
|
+
else
|
57
|
+
str.gsub(/[^\w\s]/, ' ')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module DataFilter
|
2
|
+
# Used to filter a data item by some range by seeing if
|
3
|
+
# the data field value falls within that range
|
4
|
+
#
|
5
|
+
# @example with a ceiling
|
6
|
+
# object = MyModel.new(created_at: Date.parse('2001-01-13'))
|
7
|
+
# filter = DataFilter::RangeFilter.new(:created_at, ceiling: Date.parse('2003-01-01'))
|
8
|
+
# filter.call(object)
|
9
|
+
# # => #<MyModel created_at: #<Date '2001-01-13'>>
|
10
|
+
#
|
11
|
+
# @example with a floor
|
12
|
+
# object = MyModel.new(file_count: 300)
|
13
|
+
# filter = DataFilter::RangeFilter.new(:file_count, floor: 1)
|
14
|
+
# filter_return = filter.call(object)
|
15
|
+
# # => #<MyModel file_count: 300>
|
16
|
+
# has_file = filter_return.present?
|
17
|
+
# # => true
|
18
|
+
class RangeFilter
|
19
|
+
# @param field_sym [Symbol] the field to filter on
|
20
|
+
# @param floor [Comparable] the range beginning we want to filter the data
|
21
|
+
# item by
|
22
|
+
# @param ceiling [Comparable] the range end we want to filter the data item
|
23
|
+
# by
|
24
|
+
# @param nil_default [Comparable] the value to use if the data item has no
|
25
|
+
# field value
|
26
|
+
def initialize(field_sym, floor: nil, ceiling: nil, nil_default: nil)
|
27
|
+
@field_sym = field_sym
|
28
|
+
@floor = floor
|
29
|
+
@ceiling = ceiling
|
30
|
+
@nil_default = nil_default
|
31
|
+
end
|
32
|
+
|
33
|
+
# Filters the item
|
34
|
+
#
|
35
|
+
# @param item [Comparable] the item we want to filter
|
36
|
+
# @return [Object] the original data item
|
37
|
+
def call(item)
|
38
|
+
if item.respond_to?(@field_sym)
|
39
|
+
actual = item.public_send(@field_sym)
|
40
|
+
actual = @nil_default if actual.nil?
|
41
|
+
item if in_range?(actual)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
# :nodoc:
|
48
|
+
def in_range?(actual)
|
49
|
+
return false if actual.nil?
|
50
|
+
(@floor.nil? || actual >= @floor) && (@ceiling.nil? || actual <= @ceiling)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module DataFilter
|
2
|
+
# Used to filter a data item by a set of ranges by seeing if
|
3
|
+
# the data field value intersects that range
|
4
|
+
#
|
5
|
+
# @example with a floor and ceiling
|
6
|
+
# event = MyModel.new(start_time: Date.parse('2001-01-13'), end_time: Date.parse('2002-01-13'))
|
7
|
+
# filter = DataFilter::RangeOverlapFilter
|
8
|
+
# .new(:start_time, :end_time, floor: Date.parse('2000-01-13'), ceiling: Date.parse('2003-01-13'))
|
9
|
+
# filter.call(object)
|
10
|
+
# # => #<MyModel start_time: #<Date '2001-01-13'>, end_time: #<Date '2002-01-13'>
|
11
|
+
class RangeOverlapFilter
|
12
|
+
# @param start_sym [Symbol] the range start to filter on
|
13
|
+
# @param end_sym [Symbol] the range end to filter on
|
14
|
+
# @param floor [Comparable] the range beginning we want to filter the data
|
15
|
+
# item by
|
16
|
+
# @param ceiling [Comparable] the range end we want to filter the data item
|
17
|
+
# by
|
18
|
+
# @param nil_default [Comparable] the value to use if the data item has no
|
19
|
+
# field value
|
20
|
+
def initialize(start_sym, end_sym, floor: nil, ceiling: nil, nil_default: nil)
|
21
|
+
@start_sym = start_sym
|
22
|
+
@end_sym = end_sym
|
23
|
+
@floor = floor
|
24
|
+
@ceiling = ceiling
|
25
|
+
@nil_default = nil_default
|
26
|
+
end
|
27
|
+
|
28
|
+
# Filters the item
|
29
|
+
#
|
30
|
+
# @param item [Comparable] the item we want to filter
|
31
|
+
# @return [Object] the original data item
|
32
|
+
def call(item)
|
33
|
+
if item.respond_to?(@start_sym) && item.respond_to?(@end_sym)
|
34
|
+
actual_start = item.public_send(@start_sym)
|
35
|
+
actual_start = @nil_default if actual_start.nil?
|
36
|
+
|
37
|
+
actual_end = item.public_send(@end_sym)
|
38
|
+
actual_end = @nil_default if actual_end.nil?
|
39
|
+
|
40
|
+
item if in_range?(actual_start, actual_end)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
# :nodoc:
|
47
|
+
def in_range?(actual_start, actual_end)
|
48
|
+
return false if actual_start.nil? || actual_end.nil?
|
49
|
+
(@floor..@ceiling).overlaps?(actual_start..actual_end)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module DataFilter
|
2
|
+
# Used to filter a data item by whether it is truthy/falsey
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
# object = MyModel.new(is_alive: 'false')
|
6
|
+
# filter = DataFilter::TruthyFilter.new(:is_alive)
|
7
|
+
# filter.call(object)
|
8
|
+
# # => nil
|
9
|
+
class TruthyFilter
|
10
|
+
# @param field_sym [Symbol] the name of the field to filter by
|
11
|
+
# @param invert [Boolean] (default: false) set to true if you
|
12
|
+
# would rather match when the field is falsey instead of when
|
13
|
+
# it is truthy
|
14
|
+
def initialize(field_sym, invert: false)
|
15
|
+
@field_sym = field_sym
|
16
|
+
@invert = invert
|
17
|
+
end
|
18
|
+
|
19
|
+
# Filters the item
|
20
|
+
#
|
21
|
+
# @param item [Object] the item we want to filter
|
22
|
+
# @return [Object, nil] the original data item
|
23
|
+
def call(item)
|
24
|
+
if item.respond_to?(@field_sym)
|
25
|
+
val = item.public_send(@field_sym)
|
26
|
+
is_falsey = is_falsey?(val)
|
27
|
+
is_match = (@invert ? is_falsey : !is_falsey)
|
28
|
+
if is_match
|
29
|
+
item
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
# @private
|
37
|
+
def is_falsey?(val)
|
38
|
+
[false, 'false', 0, nil].include?(val)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/data_filter.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'data_filter/version'
|
2
|
+
|
3
|
+
require 'data_filter/filter_set'
|
4
|
+
|
5
|
+
require 'data_filter/keyword_filter'
|
6
|
+
require 'data_filter/range_filter'
|
7
|
+
require 'data_filter/like_filter'
|
8
|
+
require 'data_filter/range_overlap_filter'
|
9
|
+
require 'data_filter/truthy_filter'
|
10
|
+
|
11
|
+
module DataFilter; end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data_filter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josh Bodah
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-09-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- jb3689@yahoo.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- CODE_OF_CONDUCT.md
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- data_filter.gemspec
|
72
|
+
- lib/data_filter.rb
|
73
|
+
- lib/data_filter/filter_set.rb
|
74
|
+
- lib/data_filter/keyword_filter.rb
|
75
|
+
- lib/data_filter/like_filter.rb
|
76
|
+
- lib/data_filter/range_filter.rb
|
77
|
+
- lib/data_filter/range_overlap_filter.rb
|
78
|
+
- lib/data_filter/truthy_filter.rb
|
79
|
+
- lib/data_filter/version.rb
|
80
|
+
homepage: https://github.com/backupify/data_filter
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.2.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: an extensible DSL for filtering data sets
|
104
|
+
test_files: []
|