showcase_filter 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b9efe3a7eae6a21bc263f7bc758aa8de6b11c275
4
+ data.tar.gz: e464717f66488bf624ba072bff344d28da6f012a
5
+ SHA512:
6
+ metadata.gz: 032dd8b2f033415543f5ab4879f7fb5915e5d07c705650c874a248fba2e00e771315a4fc4c30411135be08c498b2a7f673d1244c00965c042437478ee4d7299b
7
+ data.tar.gz: 60da9f8b18cff20b6eecfebe56ca39cd7d90607907f188d50d31b13382ca8912dc0d2d06983ded2565852fb226b005ceafc6386b590dd7829faa13e88cd8e8b2
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.gem
15
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format d
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in showcase_filter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Lucas
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # ShowcaseFilter
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'showcase_filter'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install showcase_filter
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/showcase_filter/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task :console do
5
+ load_irb
6
+ end
7
+
8
+ def load_irb
9
+ require 'irb'
10
+ require 'rspec'
11
+ require_relative 'spec/spec_helper'
12
+ ARGV.clear
13
+ IRB.start
14
+ end
15
+
16
+ RSpec::Core::RakeTask.new
17
+
18
+ task default: :spec
19
+ task test: :spec
@@ -0,0 +1,5 @@
1
+ module ShowcaseFilter
2
+ module Constants
3
+ ExcludeEntity = Struct.new(:exclude)
4
+ end
5
+ end
@@ -0,0 +1,52 @@
1
+ module ShowcaseFilter
2
+ class Core
3
+ def self.filter(collection)
4
+ collection.inject(ShowcaseFilter::Models::GroupCollection.new([])) {|arr, entity|
5
+ attribute = yield(entity, Constants::ExcludeEntity.new(true))
6
+ a = arr.select {|group| group.label == attribute}
7
+ if attribute.class != Constants::ExcludeEntity
8
+ if a.empty?
9
+ group = Models::Group.new(label: attribute)
10
+ else
11
+ group = a.first
12
+ end
13
+ group.entities << entity
14
+ arr << group
15
+ end
16
+ arr
17
+ }.uniq
18
+ end
19
+
20
+ def self.match(expression, group_array)
21
+ groups = ShowcaseFilter::Models::GroupCollection.new(group_array).flatten
22
+ match_expression = self.evaluate_expression(expression)
23
+
24
+ if match_expression.nil?
25
+ return ShowcaseFilter::Models::GroupCollection.new([])
26
+ end
27
+
28
+ group1 = groups.find_by_label(match_expression.group1)
29
+ group2 = groups.find_by_label(match_expression.group2)
30
+
31
+ if group1.nil? or group2.nil?
32
+ return ShowcaseFilter::Models::GroupCollection.new([])
33
+ else
34
+ return group1.entities.send(match_expression.operator.to_sym, group2.entities)
35
+ end
36
+ end
37
+
38
+ def self.evaluate_expression(expression)
39
+ # Supported operators are : union(+), exclusion(-) and intersection(&)
40
+ regex = /(\w+)\s*(\+|\-|\&)\s*(\w+)/
41
+ match_result = expression.match(regex)
42
+ return nil if match_result.nil?
43
+
44
+ ShowcaseFilter::Models::Expression.new({
45
+ :expression => match_result[0],
46
+ :operator => match_result[2],
47
+ :group1 => match_result[1],
48
+ :group2 => match_result[3]
49
+ })
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,9 @@
1
+ module ShowcaseFilter
2
+ module Models
3
+ class EntityCollection < Array
4
+ def rows(value)
5
+ self.each_slice(value).to_a
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ module ShowcaseFilter
2
+ module Models
3
+ class Expression
4
+ include ::Virtus.model
5
+
6
+ attribute :expression, String
7
+ attribute :operator, String
8
+ attribute :group1, String
9
+ attribute :group2, String
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ module ShowcaseFilter
2
+ module Models
3
+ class Group
4
+ include ::Virtus.model
5
+
6
+ attribute :label, String
7
+ attribute :entities, Array
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,13 @@
1
+ module ShowcaseFilter
2
+ module Models
3
+ class GroupCollection < Array
4
+ def +(other_array)
5
+ self.concat(other_array)
6
+ end
7
+
8
+ def find_by_label(label)
9
+ self.select { |group| group.label == label }.first
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module ShowcaseFilter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "showcase_filter/version"
2
+ require "showcase_filter/core"
3
+ require "showcase_filter/constants/exclude_entity"
4
+ require "showcase_filter/models/entity_collection"
5
+ require "showcase_filter/models/group_collection"
6
+ require "showcase_filter/models/group"
7
+ require "showcase_filter/models/expression"
8
+
9
+
10
+ module ShowcaseFilter
11
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'showcase_filter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "showcase_filter"
8
+ spec.version = ShowcaseFilter::VERSION
9
+ spec.authors = ["Lucas Santana", "Renato Alves"]
10
+ spec.email = ["lucas.santanadesouza@gmail.com", "renatodex@gmail.com"]
11
+ spec.summary = %q{Group, intersect and Match collections using custom rules }
12
+ spec.description = %q{Group, intersect and Match collections using custom rules.}
13
+ spec.homepage = "https://github.com/lojabasico/showcase_filter"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "virtus", "~> 1.0"
25
+ spec.add_development_dependency "pry", "~> 0.0"
26
+ end
@@ -0,0 +1,134 @@
1
+ require 'spec_helper'
2
+
3
+ def variants
4
+ [
5
+ Variant.new("gola c", "branca", "p", 1),
6
+ Variant.new("gola v", "preta", "m", 3),
7
+ Variant.new("gola c", "branca", "m", 0),
8
+ Variant.new("gola c", "azul", "GG", 1)
9
+ ]
10
+ end
11
+
12
+ Variant = Struct.new(:name, :color, :size, :count_on_hand)
13
+
14
+ describe ShowcaseFilter do
15
+ context "entities collection" do
16
+ it "should know how to break entities in row" do
17
+ entities_collection = ShowcaseFilter::Models::EntityCollection.new([1,2,3,4,5,6,7,8])
18
+ rows = entities_collection.rows(4)
19
+ expect(rows.first.join(',')).to eq '1,2,3,4'
20
+ expect(rows.last.join(',')).to eq '5,6,7,8'
21
+ end
22
+ end
23
+
24
+ context "group collection" do
25
+ it {
26
+ s = ShowcaseFilter::Core.filter(variants) do |entity, exclude|
27
+ entity.color
28
+ end
29
+
30
+ expect(s.find_by_label("azul").label).to eq "azul"
31
+ }
32
+
33
+ it "concat group collections should generate a new GroupCollection" do
34
+ g1 = ShowcaseFilter::Models::GroupCollection.new([1])
35
+ g2 = ShowcaseFilter::Models::GroupCollection.new([2])
36
+ expect((g1+g2).count).to eq 2
37
+ expect(g1+g2).to be_a ShowcaseFilter::Models::GroupCollection
38
+ end
39
+ end
40
+
41
+ context "evaluate expression" do
42
+ [
43
+ { :type => "union", :expression => "dog+cat", :match1 => "dog", :match2 => "+", :match3 => "cat" },
44
+ { :type => "intersection", :expression => "dog&cat", :match1 => "dog", :match2 => "&", :match3 => "cat" },
45
+ { :type => "exclusion", :expression => "dog-cat", :match1 => "dog", :match2 => "-", :match3 => "cat" },
46
+ { :type => "spaces", :expression => "dog + cat", :match1 => "dog", :match2 => "+", :match3 => "cat" }
47
+ ].each do |info|
48
+ it "should evaluate #{info[:type]} expression" do
49
+ match = ShowcaseFilter::Core.evaluate_expression(info[:expression])
50
+ expect(match).to be_a ShowcaseFilter::Models::Expression
51
+ expect(match.group1).to eq info[:match1]
52
+ expect(match.operator).to eq info[:match2]
53
+ expect(match.group2).to eq info[:match3]
54
+ end
55
+ end
56
+
57
+ it "should return false for invalid expressions" do
58
+ match = ShowcaseFilter::Core.evaluate_expression("dog ** cat")
59
+ expect(match).to be nil
60
+ end
61
+ end
62
+
63
+ context "group intersection" do
64
+ before(:each) do
65
+ @g1 = ShowcaseFilter::Core.filter(variants) do |entity|
66
+ entity.color
67
+ end
68
+ @g2 = ShowcaseFilter::Core.filter(variants) do |entity|
69
+ entity.size
70
+ end
71
+ end
72
+
73
+ it {
74
+ g3 = ShowcaseFilter::Core.match("branca & p", [@g1,@g2])
75
+ expect(g3.count).to eq 1
76
+ }
77
+
78
+ it "should return empty array if any group is empty" do
79
+ g3 = ShowcaseFilter::Core.match("branca & pp", [@g1,@g2])
80
+ expect(g3.count).to eq 0
81
+ end
82
+
83
+ it "should return empty array if operator is invalid" do
84
+ g3 = ShowcaseFilter::Core.match("branca x pp", [@g1,@g2])
85
+ expect(g3.count).to eq 0
86
+ end
87
+
88
+ it "should return empty array if arrays are invalid" do
89
+ g3 = ShowcaseFilter::Core.match("branca x pp", [nil,nil])
90
+ expect(g3.count).to eq 0
91
+ end
92
+
93
+ it "should match class" do
94
+ g3 = ShowcaseFilter::Core.match("branca & p", [@g1,@g2])
95
+ expect(g3.count).to eq 1
96
+ end
97
+ end
98
+
99
+ context "auto group discover" do
100
+ it "normal case" do
101
+ expect(variants.count).to eq 4
102
+
103
+ s = ShowcaseFilter::Core.filter(variants) do |entity|
104
+ entity.color
105
+ end
106
+
107
+ expect(s.count).to eq 3
108
+ expect(s.first.label).to eq "branca"
109
+ expect(s.first.entities.count).to eq 2
110
+ expect(s[1].label).to eq "preta"
111
+ expect(s[1].entities.count).to eq 1
112
+ expect(s[2].label).to eq "azul"
113
+ expect(s[2].entities.count).to eq 1
114
+ end
115
+
116
+ it "excluding case" do
117
+ s = ShowcaseFilter::Core.filter(variants) do |entity, exclude|
118
+ if entity.count_on_hand > 0
119
+ entity.color
120
+ else
121
+ exclude
122
+ end
123
+ end
124
+
125
+ expect(s.count).to eq 3
126
+ expect(s.first.label).to eq "branca"
127
+ expect(s.first.entities.count).to eq 1
128
+ expect(s[1].label).to eq "preta"
129
+ expect(s[1].entities.count).to eq 1
130
+ expect(s[2].label).to eq "azul"
131
+ expect(s[2].entities.count).to eq 1
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,80 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+ require 'virtus'
4
+ require 'pry'
5
+
6
+ require "showcase_filter"
7
+
8
+ RSpec.configure do |config|
9
+ # rspec-expectations config goes here. You can use an alternate
10
+ # assertion/expectation library such as wrong or the stdlib/minitest
11
+ # assertions if you prefer.
12
+ config.expect_with :rspec do |expectations|
13
+ # This option will default to `true` in RSpec 4. It makes the `description`
14
+ # and `failure_message` of custom matchers include text for helper methods
15
+ # defined using `chain`, e.g.:
16
+ # be_bigger_than(2).and_smaller_than(4).description
17
+ # # => "be bigger than 2 and smaller than 4"
18
+ # ...rather than:
19
+ # # => "be bigger than 2"
20
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
21
+ end
22
+
23
+ # rspec-mocks config goes here. You can use an alternate test double
24
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
25
+ config.mock_with :rspec do |mocks|
26
+ # Prevents you from mocking or stubbing a method that does not exist on
27
+ # a real object. This is generally recommended, and will default to
28
+ # `true` in RSpec 4.
29
+ mocks.verify_partial_doubles = true
30
+ end
31
+
32
+ # The settings below are suggested to provide a good initial experience
33
+ # with RSpec, but feel free to customize to your heart's content.
34
+ =begin
35
+ # These two settings work together to allow you to limit a spec run
36
+ # to individual examples or groups you care about by tagging them with
37
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
38
+ # get run.
39
+ config.filter_run :focus
40
+ config.run_all_when_everything_filtered = true
41
+
42
+ # Limits the available syntax to the non-monkey patched syntax that is
43
+ # recommended. For more details, see:
44
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
45
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
46
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
47
+ config.disable_monkey_patching!
48
+
49
+ # This setting enables warnings. It's recommended, but in some cases may
50
+ # be too noisy due to issues in dependencies.
51
+ config.warnings = true
52
+
53
+ # Many RSpec users commonly either run the entire suite or an individual
54
+ # file, and it's useful to allow more verbose output when running an
55
+ # individual spec file.
56
+ if config.files_to_run.one?
57
+ # Use the documentation formatter for detailed output,
58
+ # unless a formatter has already been configured
59
+ # (e.g. via a command-line flag).
60
+ config.default_formatter = 'doc'
61
+ end
62
+
63
+ # Print the 10 slowest examples and example groups at the
64
+ # end of the spec run, to help surface which specs are running
65
+ # particularly slow.
66
+ config.profile_examples = 10
67
+
68
+ # Run specs in random order to surface order dependencies. If you find an
69
+ # order dependency and want to debug it, you can fix the order by providing
70
+ # the seed, which is printed after each run.
71
+ # --seed 1234
72
+ config.order = :random
73
+
74
+ # Seed global randomization in this process using the `--seed` CLI option.
75
+ # Setting this allows you to use `--seed` to deterministically reproduce
76
+ # test failures related to randomization by passing the same `--seed` value
77
+ # as the one that triggered the failure.
78
+ Kernel.srand config.seed
79
+ =end
80
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: showcase_filter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Santana
8
+ - Renato Alves
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-03-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: virtus
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '0.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '0.0'
84
+ description: Group, intersect and Match collections using custom rules.
85
+ email:
86
+ - lucas.santanadesouza@gmail.com
87
+ - renatodex@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - lib/showcase_filter.rb
99
+ - lib/showcase_filter/constants/exclude_entity.rb
100
+ - lib/showcase_filter/core.rb
101
+ - lib/showcase_filter/models/entity_collection.rb
102
+ - lib/showcase_filter/models/expression.rb
103
+ - lib/showcase_filter/models/group.rb
104
+ - lib/showcase_filter/models/group_collection.rb
105
+ - lib/showcase_filter/version.rb
106
+ - showcase_filter.gemspec
107
+ - spec/lib/showcase_filter_spec.rb
108
+ - spec/spec_helper.rb
109
+ homepage: https://github.com/lojabasico/showcase_filter
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.4.2
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Group, intersect and Match collections using custom rules
133
+ test_files:
134
+ - spec/lib/showcase_filter_spec.rb
135
+ - spec/spec_helper.rb