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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: affd888942cfb7bbac784fd91eef29c15b1d157b
4
- data.tar.gz: b76273d0e917aec6d45e141175014547235c19f2
3
+ metadata.gz: 332f6db7efb59c1ca7fef5a07295d33f682531bb
4
+ data.tar.gz: dfe88c3afd902a7698b1911e2bad58fc0343b27a
5
5
  SHA512:
6
- metadata.gz: b1155098d7771a6fdcd50938918e77bb60fc40cb36401e4db4e2fc7a485d260fa248152440d75e4c19b442fa3e7904f66be7a5ea2793fc8134f5791abd0328fc
7
- data.tar.gz: f06a4205f198f30b1ede769a4868ae162a5c89891b608712efb3d11a4ff6495ad6f333c52b9f5d669a27f44446db537065f40216ebaa00c091b355609d233f02
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
- ```rb
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
@@ -1,3 +1,3 @@
1
1
  module DataFilter
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/data_filter.rb CHANGED
@@ -7,5 +7,6 @@ require 'data_filter/range_filter'
7
7
  require 'data_filter/like_filter'
8
8
  require 'data_filter/range_overlap_filter'
9
9
  require 'data_filter/truthy_filter'
10
+ require 'data_filter/prefix_filter'
10
11
 
11
12
  module DataFilter; end
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.2.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: 2015-09-18 00:00:00.000000000 Z
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.2.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
data/bin/setup DELETED
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
-
5
- bundle install
6
-
7
- # Do any other automated setup that you need to do here