rules_processor 0.1.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/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
6
+ after_script: bundle exec codeclimate-test-reporter
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rules_processor.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'simplecov'
8
+ gem 'codeclimate-test-reporter', '~> 1.0.0'
9
+ end
10
+
11
+ group :development, :test do
12
+ gem 'rubocop', require: false
13
+ gem 'byebug'
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Rafal Grabowski
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,45 @@
1
+ [![Code Climate](https://codeclimate.com/github/rgrabowski/rules_processor/badges/gpa.svg)](https://codeclimate.com/github/rgrabowski/rules_processor)
2
+ [![Test Coverage](https://codeclimate.com/github/rgrabowski/rules_processor/badges/coverage.svg)](https://codeclimate.com/github/rgrabowski/rules_processor/coverage)
3
+ [![Issue Count](https://codeclimate.com/github/rgrabowski/rules_processor/badges/issue_count.svg)](https://codeclimate.com/github/rgrabowski/rules_processor)
4
+ [![Build Status](https://travis-ci.org/rgrabowski/rules_processor.svg?branch=master)](https://travis-ci.org/rgrabowski/rules_processor)
5
+ # RulesProcessor
6
+
7
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rules_processor`. To experiment with that code, run `bin/console` for an interactive prompt.
8
+
9
+ TODO: Delete this and the text above, and describe your gem
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'rules_processor'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install rules_processor
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
34
+
35
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
36
+
37
+ ## Contributing
38
+
39
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rules_processor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
40
+
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
45
+
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ RuboCop::RakeTask.new
8
+
9
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rules_processor"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ require 'rules_processor/version'
2
+ require 'rules_processor/configuration'
3
+ require 'rules_processor/condition_matcher'
4
+ require 'rules_processor/operations/base'
5
+ require 'rules_processor/operations/greater_than_equal'
6
+ require 'rules_processor/operations/greater_than'
7
+ require 'rules_processor/operations/include'
8
+ require 'rules_processor/operations/is_not'
9
+ require 'rules_processor/operations/is'
10
+ require 'rules_processor/operations/less_than_equal'
11
+ require 'rules_processor/operations/less_than'
12
+ require 'rules_processor/operations/not_include'
13
+ require 'rules_processor/operators_to_select.rb'
14
+ require 'rules_processor/processor'
15
+ require 'rules_processor/rule_matcher'
16
+
17
+ module RulesProcessor
18
+
19
+ class << self
20
+
21
+ attr_accessor :configuration
22
+
23
+ end
24
+
25
+ def self.configuration
26
+ @configuration ||= Configuration.new
27
+ end
28
+
29
+ def self.configure
30
+ yield(configuration)
31
+ end
32
+
33
+ end
@@ -0,0 +1,24 @@
1
+ module RulesProcessor
2
+ class ConditionMatcher
3
+
4
+ attr_accessor :records, :condition
5
+
6
+ def initialize(args = {})
7
+ @records = args[:records]
8
+ @condition = OpenStruct.new(args[:condition])
9
+ end
10
+
11
+ def matches?
12
+ klass = Object.const_get(klass_name)
13
+ klass.new(records: records, condition: condition).perform
14
+ end
15
+
16
+ private
17
+
18
+ def klass_name
19
+ camelized_operator_name = condition.operator.split('_').collect(&:capitalize).join
20
+ "RulesProcessor::Operations::#{camelized_operator_name}"
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ module RulesProcessor
2
+ class Configuration
3
+
4
+ attr_accessor :actions_class
5
+
6
+ def initialize
7
+ @actions_class = actions_class
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ module RulesProcessor
2
+ module Operations
3
+ class Base
4
+
5
+ attr_accessor :value,
6
+ :options,
7
+ :records,
8
+ :condition,
9
+ :record_value,
10
+ :condition_value,
11
+ :record_class,
12
+ :field
13
+
14
+ def initialize(args = {})
15
+ @records = args[:records]
16
+ @condition = OpenStruct.new(args[:condition])
17
+ @record_class = condition.field.split('_', 2)[0].to_sym
18
+ @field = condition.field.split('_', 2)[1].to_sym
19
+ @record_value = actual_value
20
+ @condition_value = nullified_value
21
+ end
22
+
23
+ def perform
24
+ fail NotImplementedError
25
+ end
26
+
27
+ private
28
+
29
+ def actual_value
30
+ records[record_class].public_send(field)
31
+ end
32
+
33
+ def nullified_value
34
+ return nil if condition.value == ''
35
+ condition.value
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,11 @@
1
+ module RulesProcessor
2
+ module Operations
3
+ class GreaterThan < Base
4
+
5
+ def perform
6
+ condition_value.to_i < record_value.to_i
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module RulesProcessor
2
+ module Operations
3
+ class GreaterThanEqual < Base
4
+
5
+ def perform
6
+ condition_value.to_i <= record_value.to_i
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'normalize_array.rb'
2
+ module RulesProcessor
3
+ module Operations
4
+ class Include < Base
5
+
6
+ include NormalizeArray
7
+
8
+ def perform
9
+ normalized_value(condition_value).any? { |i| normalized_value(record_value).include?(i) }
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module RulesProcessor
2
+ module Operations
3
+ class Is < Base
4
+
5
+ def perform
6
+ record_value.to_s == condition_value.to_s
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module RulesProcessor
2
+ module Operations
3
+ class IsNot < Base
4
+
5
+ def perform
6
+ record_value != condition_value
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module RulesProcessor
2
+ module Operations
3
+ class LessThan < Base
4
+
5
+ def perform
6
+ condition_value.to_i > record_value.to_i
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module RulesProcessor
2
+ module Operations
3
+ class LessThanEqual < Base
4
+
5
+ def perform
6
+ condition_value.to_i >= record_value.to_i
7
+ end
8
+
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module NormalizeArray
2
+
3
+ private
4
+
5
+ def normalized_value(value)
6
+ if value.is_a?(String)
7
+ value.tr(',.:', '').split.map(&:strip).map(&:downcase)
8
+ elsif value.is_a?(Array)
9
+ value.map(&:to_s).map(&:strip).map(&:downcase)
10
+ else
11
+ fail "Not supported comparison for #{value.class.inspect}"
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,14 @@
1
+ require_relative 'normalize_array.rb'
2
+ module RulesProcessor
3
+ module Operations
4
+ class NotInclude < Base
5
+
6
+ include NormalizeArray
7
+
8
+ def perform
9
+ normalized_value(condition_value).none? { |i| normalized_value(record_value).include?(i) }
10
+ end
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,21 @@
1
+ module RulesProcessor
2
+ module OperatorsToSelect
3
+
4
+ def self.base
5
+ [:is, :is_not].freeze
6
+ end
7
+
8
+ def self.includable
9
+ [:include,
10
+ :not_include].freeze
11
+ end
12
+
13
+ def self.comparable
14
+ [:less_than,
15
+ :less_than_equal,
16
+ :greater_than,
17
+ :greater_than_equal].freeze
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,62 @@
1
+ module RulesProcessor
2
+ class Processor
3
+
4
+ attr_accessor :records, :ruleset, :options
5
+
6
+ def initialize(args = {})
7
+ @records = args[:records]
8
+ @ruleset = args[:ruleset]
9
+ @options = args[:options]
10
+ end
11
+
12
+ def process
13
+ ruleset.each do |rule|
14
+ mark_skipped(rule) && next if already_matched?(rule)
15
+ mark_not_match(rule) && next unless rule_matches?(rule)
16
+
17
+ mark_match(rule) && actions_processor(rule)
18
+
19
+ process
20
+ break
21
+ end
22
+ matching_result
23
+ end
24
+
25
+ private
26
+
27
+ def matching_result
28
+ @matching_result ||= []
29
+ end
30
+
31
+ def mark_match(rule)
32
+ matching_result << {rule.id => :match}
33
+ already_matched << rule.id
34
+ end
35
+
36
+ def mark_not_match(rule)
37
+ matching_result << {rule.id => :not_match}
38
+ end
39
+
40
+ def mark_skipped(rule)
41
+ matching_result << {rule.id => :skipped}
42
+ end
43
+
44
+ def already_matched
45
+ @already_matched ||= []
46
+ end
47
+
48
+ def already_matched?(rule)
49
+ already_matched.include?(rule.id)
50
+ end
51
+
52
+ def rule_matches?(rule)
53
+ RulesProcessor::RuleMatcher.new(records: records, rule: rule, options: options).matches?
54
+ end
55
+
56
+ def actions_processor(rule)
57
+ klass = RulesProcessor.configuration.actions_class
58
+ klass.new(records: records, rule: rule, options: options).process
59
+ end
60
+
61
+ end
62
+ end