decision_maker 0.7.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 275c305c35516a50ab4eb910de39bc3840dd6cf0
4
+ data.tar.gz: 7e8a9646909816845fa654cef73cf4579c4517ce
5
+ SHA512:
6
+ metadata.gz: de4cc9dde624b18c0c675cfe1f886b98cd95a49d08b8244a96a3e08dde2de739f16646f3c51ae297bb8406845b7577dd6efd8ed7489a99373c5f102fd8cc50ef
7
+ data.tar.gz: d45e28a926da2bb2cc00155a9a269ce815b22afdddf9b5b02ededc850bee1756ea5115ec915af40041e3a8e02c0756982de8272ee6559605099baa9a6c33d49b
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ - 2.2.3
5
+ - 2.1.0
6
+ - 2.0.0
7
+ before_install: gem install bundler -v 1.10.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in decision_maker.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Sunao Tanabe
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.
@@ -0,0 +1,90 @@
1
+ # DecisionMaker [![Build Status](https://travis-ci.org/sunaot/decision_maker.svg?branch=master)](https://travis-ci.org/sunaot/decision_maker)
2
+
3
+ DecisionMaker is a library which behaves as a decision table and it makes complicated dispatching logic much simpler. See following usages for details.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'decision_maker'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install decision_maker
20
+
21
+ ## Usage
22
+
23
+ Simple dynamic definition style.
24
+
25
+ ```ruby
26
+ require 'decision_maker'
27
+
28
+ ticket_price = DecisionMaker.generate do
29
+ rule(
30
+ child: { condition: 0..9, action: 600 },
31
+ student: { condition: 10..16, action: 1200 },
32
+ adult: { condition: ->(age) { age > 16 }, action: 2000 },
33
+ )
34
+ end
35
+
36
+ ticket_price.call(20) #=> 2000
37
+ ```
38
+
39
+ Class definition style.
40
+
41
+ ```ruby
42
+ require 'decision_maker'
43
+
44
+ TicketPrice = DecisionMaker.define do
45
+ rule(
46
+ child: { condition: 0..9, action: 600 },
47
+ student: { condition: 10..16, action: 1200 },
48
+ adult: { condition: ->(age) { age > 16 }, action: 2000 },
49
+ )
50
+ end
51
+
52
+ ticket_price = TicketPrice.new
53
+ ticket_price.call(20) #=> 2000
54
+ ```
55
+
56
+ Dynamic definition style with some name customizations.
57
+
58
+ ```ruby
59
+ require 'decision_maker'
60
+
61
+ ticket_price = DecisionMaker.generate do
62
+ name :calculate
63
+ condition_name :age
64
+ action_name :price
65
+
66
+ rule(
67
+ child: { age: 0..9, price: 600 },
68
+ student: { age: 10..16, price: 1200 },
69
+ adult: { age: ->(age) { age > 16 }, price: 2000 },
70
+ )
71
+ end
72
+
73
+ ticket_price.calculate(20) #=> 2000
74
+ ```
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+
80
+ 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).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sunaot/decision_maker.
85
+
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
90
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => %i( test build )
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "decision_maker"
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
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'decision_maker/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "decision_maker"
8
+ spec.version = DecisionMaker::VERSION
9
+ spec.authors = ['sunaot']
10
+ spec.email = ['sunao.tanabe@gmail.com']
11
+
12
+ spec.summary = %q{Easy way to treat complicated dispatching.}
13
+ spec.description = %q{DecisionMaker is a library which help you to gerate simple decision table and make complicated dispatching logic much simpler.}
14
+ spec.homepage = 'https://github.com/sunaot/decision_maker'
15
+ spec.license = 'MIT'
16
+ spec.required_ruby_version = '>= 2.0.0'
17
+
18
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
19
+ # delete this section to allow pushing this gem to any host.
20
+ if spec.respond_to?(:metadata)
21
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
22
+ else
23
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "minitest"
34
+ end
@@ -0,0 +1,42 @@
1
+ require_relative 'decision_maker/version'
2
+ require_relative 'decision_maker/dsl'
3
+ require_relative 'decision_maker/class_definition'
4
+
5
+ module DecisionMaker
6
+ # @example DecisionMaker.generate
7
+ # ticket_price = DecisionMaker.generate do
8
+ # rule(
9
+ # child: { condition: 0..9, action: 600 },
10
+ # student: { condition: 10..16, action: 1200 },
11
+ # adult: { condition: ->(age) { age > 16 }, action: 2000 },
12
+ # )
13
+ # end
14
+ #
15
+ # ticket_price.call(20) #=> 2000
16
+ #
17
+ # @example Customizing names are available. It makes your code more illustrative
18
+ # ticket_price = DecisionMaker.generate do
19
+ # name :calculate
20
+ # condition_name :age
21
+ # action_name :price
22
+ #
23
+ # rule(
24
+ # child: { age: 0..9, price: 600 },
25
+ # student: { age: 10..16, price: 1200 },
26
+ # adult: { age: ->(age) { age > 16 }, price: 2000 },
27
+ # )
28
+ # end
29
+ #
30
+ # ticket_price.calculate(20) #=> 2000
31
+ def self.generate(&definition)
32
+ decision_table = define(&definition)
33
+ decision_table.new
34
+ end
35
+
36
+ def self.define(&definition)
37
+ dsl = DSL.new
38
+ dsl.instance_eval(&definition)
39
+ d = dsl.definition
40
+ ClassDefinition.customize(d.rule_table, d.name, d.condition_name, d.action_name, d.error_handler)
41
+ end
42
+ end
@@ -0,0 +1,34 @@
1
+ module DecisionMaker
2
+ class ClassDefinition
3
+ def self.customize(rule_table, name, condition_name, action_name, error_handler)
4
+ args = [rule_table, name, condition_name, action_name, error_handler]
5
+ c = Class.new
6
+ c.class_exec(*args) do |table, name, condition_name, action_name, error_handler|
7
+ const_set 'TABLE', table
8
+
9
+ define_method(:rule_table) { self.class::TABLE }
10
+
11
+ define_method(name) do |key|
12
+ rule_table[label(key)].fetch(action_name)
13
+ end
14
+
15
+ define_method(:label) do |key|
16
+ found = rule_table.find do |label, rule|
17
+ rule[condition_name] === key
18
+ end
19
+ if found
20
+ rule_label, rule_content = found
21
+ rule_label
22
+ else
23
+ error_handler.call(key)
24
+ end
25
+ end
26
+
27
+ define_method(:for) do |label|
28
+ rule_table[label].fetch(action_name)
29
+ end
30
+ end
31
+ c
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,40 @@
1
+ module DecisionMaker
2
+ class DSL
3
+ def definition
4
+ porter
5
+ end
6
+
7
+ private
8
+ Default = {
9
+ rule_table: {},
10
+ name: :call,
11
+ condition_name: :condition,
12
+ action_name: :action,
13
+ error_handler: ->(key) { raise "cannot find rule for #{key}" },
14
+ }
15
+ Porter = Struct.new(*Default.keys)
16
+ attr_accessor :porter
17
+
18
+ def initialize
19
+ self.porter = Porter.new(*Default.values)
20
+ end
21
+
22
+ def table(rule_table)
23
+ porter.rule_table = rule_table
24
+ end
25
+ alias_method :rule, :table
26
+
27
+ def table_name(table_name)
28
+ self.class.module_exec(table_name) do |new_name|
29
+ alias_method new_name, :table
30
+ end
31
+ end
32
+ alias_method :rule_name, :table_name
33
+
34
+ names = Default.keys - [:rule_table]
35
+ names.each do |n|
36
+ define_method(n) {|val| porter.send("#{n}=", val) }
37
+ end
38
+ alias_method :on_error, :error_handler
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module DecisionMaker
2
+ VERSION = "0.7.0"
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: decision_maker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
+ platform: ruby
6
+ authors:
7
+ - sunaot
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-01-30 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: DecisionMaker is a library which help you to gerate simple decision table
56
+ and make complicated dispatching logic much simpler.
57
+ email:
58
+ - sunao.tanabe@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - decision_maker.gemspec
72
+ - lib/decision_maker.rb
73
+ - lib/decision_maker/class_definition.rb
74
+ - lib/decision_maker/dsl.rb
75
+ - lib/decision_maker/version.rb
76
+ homepage: https://github.com/sunaot/decision_maker
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ allowed_push_host: https://rubygems.org
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.0.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.4.5
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Easy way to treat complicated dispatching.
101
+ test_files: []
102
+ has_rdoc: