data_filter 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -3
- data/lib/data_filter/like_filter.rb +2 -2
- data/lib/data_filter/prefix_filter.rb +37 -0
- data/lib/data_filter/version.rb +1 -1
- data/lib/data_filter.rb +1 -0
- metadata +5 -5
- data/bin/console +0 -14
- data/bin/setup +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 332f6db7efb59c1ca7fef5a07295d33f682531bb
|
4
|
+
data.tar.gz: dfe88c3afd902a7698b1911e2bad58fc0343b27a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06b680bdcd3e803987fc9466d0bcc10a1467f36c0aa69a64b18705c9666cf9eac4b9e8b2b37aca738fd12f3ab922b5429a36b227b207925577ce66797c9094a9
|
7
|
+
data.tar.gz: 3ba38ef1ddf6b50f43fabb22bca09f7bc27c4bef470f48c8ff6958d97e3d0923fd4997ddc74bcef271af9a9ed0ca39fc07d4f9a70084462dc0c3f2431c307c65
|
data/README.md
CHANGED
@@ -7,9 +7,13 @@
|
|
7
7
|
|
8
8
|
an extensible DSL for filtering data sets
|
9
9
|
|
10
|
+
## Summary
|
11
|
+
|
12
|
+
`DataFilter` is a library for creating filters that are consistent, reusable, and easy to read. A filter is simply something that decides whether or not an element should be removed from a set. For example, we could create a `DataFilter::FilterSet` that is comprised of various filters and then pass an array into the filter set. The filter set will then remove elements that do not pass each of the filters.
|
13
|
+
|
10
14
|
## Installation
|
11
15
|
|
12
|
-
```
|
16
|
+
```
|
13
17
|
gem install data_filter
|
14
18
|
```
|
15
19
|
|
@@ -35,13 +39,16 @@ filter_set = DataFilter::FilterSet.create do
|
|
35
39
|
# Check if within range
|
36
40
|
range_filter :age, ceiling: params[:max_age]
|
37
41
|
|
42
|
+
# Check if ranges overlap
|
43
|
+
range_filter :start, :end, floor: Date.parse('2015-01-01')
|
44
|
+
|
38
45
|
# Add a custom filter
|
39
46
|
add_filter -> (user) { user if user.student || user.age > 25 }
|
40
47
|
end
|
41
48
|
|
42
49
|
data = [
|
43
|
-
User.create(name: 'Josh', age: 26, student: false, gender: :male),
|
44
|
-
User.create(name: 'Lauren', age: 25, student: true, gender: :female)
|
50
|
+
User.create(name: 'Josh', age: 26, student: false, gender: :male, start: Date.parse('2007-01-01'), end: Date.parse('2013-01-01')),
|
51
|
+
User.create(name: 'Lauren', age: 25, student: true, gender: :female, start: Date.parse('2008-01-01'), end: Date.parse('2016-01-01'))
|
45
52
|
]
|
46
53
|
|
47
54
|
# By default data which doesn't match all of the filters will be filtered out
|
@@ -51,6 +58,10 @@ filter_set.call(data)
|
|
51
58
|
## Changelog
|
52
59
|
|
53
60
|
```
|
61
|
+
* v0.3.0
|
62
|
+
|
63
|
+
- Added PrefixFilter
|
64
|
+
|
54
65
|
* v0.2.0
|
55
66
|
|
56
67
|
- Fix RangeOverlapFilter edge cases
|
@@ -36,12 +36,12 @@ module DataFilter
|
|
36
36
|
when Hash
|
37
37
|
match?(actual.values.flatten, search_term)
|
38
38
|
when Array
|
39
|
-
actual.any? {|item| match?(item, search_term)}
|
39
|
+
actual.any? { |item| match?(item, search_term) }
|
40
40
|
when String
|
41
41
|
regexp =
|
42
42
|
normalize(search_term, true)
|
43
43
|
.split(' ')
|
44
|
-
.map {|term| Regexp.escape(term)}
|
44
|
+
.map { |term| Regexp.escape(term) }
|
45
45
|
.join('|')
|
46
46
|
.insert(0, '(')
|
47
47
|
.insert(-1, ')')
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module DataFilter
|
2
|
+
# Used to filter a data item by a prefix by seeing if
|
3
|
+
# the data field value starts with the prefix
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# object = MyModel.new(text: 'hello world!')
|
7
|
+
# filter = DataFilter::PrefixFilter.new(:text, 'hello')
|
8
|
+
# filter.call(object)
|
9
|
+
# # => #<MyModel text: 'hello world'>
|
10
|
+
class PrefixFilter
|
11
|
+
# @param field_sym [Symbol] name of the data method we want
|
12
|
+
# to filter
|
13
|
+
# @param prefix [String] the value we want to use when
|
14
|
+
# filtering the data item
|
15
|
+
def initialize(field_sym, prefix)
|
16
|
+
@field_sym = field_sym
|
17
|
+
@prefix = prefix
|
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
|
+
starts_with?(item.public_send(@field_sym), @prefix)
|
27
|
+
item
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def starts_with?(actual, prefix)
|
34
|
+
actual.match(/\A#{prefix}/i)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/data_filter/version.rb
CHANGED
data/lib/data_filter.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_filter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Josh Bodah
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,13 +80,12 @@ files:
|
|
80
80
|
- LICENSE.txt
|
81
81
|
- README.md
|
82
82
|
- Rakefile
|
83
|
-
- bin/console
|
84
|
-
- bin/setup
|
85
83
|
- data_filter.gemspec
|
86
84
|
- lib/data_filter.rb
|
87
85
|
- lib/data_filter/filter_set.rb
|
88
86
|
- lib/data_filter/keyword_filter.rb
|
89
87
|
- lib/data_filter/like_filter.rb
|
88
|
+
- lib/data_filter/prefix_filter.rb
|
90
89
|
- lib/data_filter/range_filter.rb
|
91
90
|
- lib/data_filter/range_overlap_filter.rb
|
92
91
|
- lib/data_filter/truthy_filter.rb
|
@@ -111,8 +110,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
110
|
version: '0'
|
112
111
|
requirements: []
|
113
112
|
rubyforge_project:
|
114
|
-
rubygems_version: 2.
|
113
|
+
rubygems_version: 2.4.8
|
115
114
|
signing_key:
|
116
115
|
specification_version: 4
|
117
116
|
summary: an extensible DSL for filtering data sets
|
118
117
|
test_files: []
|
118
|
+
has_rdoc:
|
data/bin/console
DELETED
@@ -1,14 +0,0 @@
|
|
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
|