rule_table 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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +4 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +126 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/rule_table.rb +83 -0
- data/lib/rule_table/version.rb +3 -0
- data/rule_table.gemspec +24 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 46a83fa6f320199978d1215ce239cbf385613b3d
|
4
|
+
data.tar.gz: dab18566c442b77de292320c71a36bdfd03ef988
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b8a4714b815af8360c8e7e776d09366f87f703b5738c98e1e2600a5bc0ac7308bd4db6cf036b0c6e2ce198e28fb90a1247426e9696cc1e5a85434609366c9b1f
|
7
|
+
data.tar.gz: 91b2f8bff0fb924a063542e95d309c8749b92c706adc368ef02b3dc0aabf19f21a8fc583361a0e8dc059e9ae1d479ba4e15a8baf1ab966aa955fd17ff66ec0c5
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 iain
|
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,126 @@
|
|
1
|
+
# RuleTable
|
2
|
+
|
3
|
+
Based on [RubyTapas episode 358](http://www.rubytapas.com/episodes/358-Rule-Table),
|
4
|
+
this is a simple rule table, but with a more fancy API. In fact, I'm using all
|
5
|
+
of Ruby's meta programming tricks to get here. That might not be your cup of tea
|
6
|
+
and that's okay.
|
7
|
+
|
8
|
+
In short, RuleTable allows you to flatten the state space of a problem.
|
9
|
+
Instead of branching, and branching, and branching again, we can see all the
|
10
|
+
criteria for a given target in one place.
|
11
|
+
|
12
|
+
Essentially, it turns a big nested set of rules into a flat list of rules.
|
13
|
+
|
14
|
+
This is a tiny gem, made in one evening.
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
Add this line to your application's Gemfile:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'rule_table'
|
22
|
+
```
|
23
|
+
|
24
|
+
And then execute:
|
25
|
+
|
26
|
+
$ bundle
|
27
|
+
|
28
|
+
Or install it yourself as:
|
29
|
+
|
30
|
+
$ gem install rule_table
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
To get the full context of this example, see the RubyTapas episode.
|
35
|
+
|
36
|
+
First up, let's define the matchers. These allow you to do define checks that
|
37
|
+
can be used later on. A matcher has a name and block. The name is used to
|
38
|
+
reference it later on. The block defines how the matcher should be applied. We
|
39
|
+
use `instance_exec` to put you inside the object you're matching against.
|
40
|
+
|
41
|
+
``` ruby
|
42
|
+
RuleTable.matcher :os do |pattern|
|
43
|
+
pattern === os
|
44
|
+
end
|
45
|
+
|
46
|
+
RuleTable.matcher :width do |pattern|
|
47
|
+
pattern === resolution.width
|
48
|
+
end
|
49
|
+
|
50
|
+
RuleTable.matcher :height do |pattern|
|
51
|
+
pattern === resolution.height
|
52
|
+
end
|
53
|
+
|
54
|
+
RuleTable.matcher :misc do |pattern|
|
55
|
+
pattern === user_agent_misc
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
Next up is defining the rules. Rules are order dependent. The first rule for
|
60
|
+
which everything matches, is the one that is returned.
|
61
|
+
|
62
|
+
``` ruby
|
63
|
+
TABLE = RuleTable.new do
|
64
|
+
|
65
|
+
rule :ios_hi, match(:os, /ios/i),
|
66
|
+
match(:width, 1024..2732),
|
67
|
+
match(:height, 768..2048)
|
68
|
+
|
69
|
+
rule :ios_lo, match(:os, /ios/i),
|
70
|
+
match(:width, 0...1024),
|
71
|
+
match(:height, 0...768)
|
72
|
+
|
73
|
+
rule :ereader, match(:os, /android/i),
|
74
|
+
match(:misc, /inky/i)
|
75
|
+
|
76
|
+
# more rules omitted
|
77
|
+
|
78
|
+
rule :unknown
|
79
|
+
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
Finally, we can find the rule:
|
84
|
+
|
85
|
+
``` ruby
|
86
|
+
device = OpenStruct.new(
|
87
|
+
os: "Android",
|
88
|
+
resolution: OpenStruct.new(
|
89
|
+
width: 1430,
|
90
|
+
height: 1080
|
91
|
+
),
|
92
|
+
user_agent_misc: "(Inky)",
|
93
|
+
)
|
94
|
+
|
95
|
+
TABLE.match(device) # => :ereader
|
96
|
+
```
|
97
|
+
|
98
|
+
Finally, there is also a way to debug why a certain target was found. Simply
|
99
|
+
replace the message send `match` with `match_with_trace`.
|
100
|
+
|
101
|
+
```
|
102
|
+
target, trace = TABLE.match_with_trace(device)
|
103
|
+
target # => :ereader
|
104
|
+
trace # =>
|
105
|
+
# [
|
106
|
+
# { target: :ios_hi, matched: [] },
|
107
|
+
# { target: :ios_lo, matched: [] },
|
108
|
+
# { target: :ereader, matched: [:os, :misc] }
|
109
|
+
# ]
|
110
|
+
```
|
111
|
+
|
112
|
+
## Development
|
113
|
+
|
114
|
+
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.
|
115
|
+
|
116
|
+
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).
|
117
|
+
|
118
|
+
## Contributing
|
119
|
+
|
120
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/iain/rule_table.
|
121
|
+
|
122
|
+
|
123
|
+
## License
|
124
|
+
|
125
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
126
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rule_table"
|
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
data/lib/rule_table.rb
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "rule_table/version"
|
2
|
+
|
3
|
+
module RuleTable
|
4
|
+
|
5
|
+
def self.matcher(name, &block)
|
6
|
+
matchers[name] = block
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.matchers
|
10
|
+
@matchers ||= {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.new(&block)
|
14
|
+
Table.new.tap { |table| TableDefiner.new(table, &block) }
|
15
|
+
end
|
16
|
+
|
17
|
+
class Table
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@rules = []
|
21
|
+
end
|
22
|
+
|
23
|
+
def add_rule_for(target, *matchers)
|
24
|
+
@rules << [ target, matchers ]
|
25
|
+
end
|
26
|
+
|
27
|
+
def match(object)
|
28
|
+
@rules.find { |(_target, matchers)|
|
29
|
+
matchers.all? { |m| m.matches?(object) }
|
30
|
+
}.first
|
31
|
+
end
|
32
|
+
|
33
|
+
def match_with_trace(object)
|
34
|
+
trace = []
|
35
|
+
result = @rules.find { |(target, matchers)|
|
36
|
+
partial_trace = { target: target, matched: [] }
|
37
|
+
matchers.all? { |m|
|
38
|
+
m.matches?(object).tap { |match_result|
|
39
|
+
partial_trace[:matched] << m.matcher_name if match_result
|
40
|
+
}
|
41
|
+
}.tap {
|
42
|
+
trace << partial_trace
|
43
|
+
}
|
44
|
+
}.first
|
45
|
+
[ result, trace ]
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
class TableDefiner
|
51
|
+
|
52
|
+
def initialize(table, &block)
|
53
|
+
@table = table
|
54
|
+
instance_eval(&block)
|
55
|
+
end
|
56
|
+
|
57
|
+
def rule(*args)
|
58
|
+
@table.add_rule_for(*args)
|
59
|
+
end
|
60
|
+
|
61
|
+
def match(matcher_name, *args)
|
62
|
+
ConfiguredMatcher.new(matcher_name, *args)
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
class ConfiguredMatcher
|
68
|
+
|
69
|
+
attr_reader :matcher_name
|
70
|
+
|
71
|
+
def initialize(matcher_name, *args)
|
72
|
+
@matcher_name = matcher_name
|
73
|
+
@matcher = RuleTable.matchers.fetch(matcher_name)
|
74
|
+
@args = args
|
75
|
+
end
|
76
|
+
|
77
|
+
def matches?(object)
|
78
|
+
object.instance_exec(*@args, &@matcher)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
data/rule_table.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rule_table/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rule_table"
|
8
|
+
spec.version = RuleTable::VERSION
|
9
|
+
spec.authors = ["iain"]
|
10
|
+
spec.email = ["iain@iain.nl"]
|
11
|
+
|
12
|
+
spec.summary = %q{A simple implementation of a rule table with lots of metaprogramming.}
|
13
|
+
spec.homepage = "https://github.com/iain/rule_table"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rule_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- iain
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-11-25 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: rspec
|
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:
|
56
|
+
email:
|
57
|
+
- iain@iain.nl
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/rule_table.rb
|
72
|
+
- lib/rule_table/version.rb
|
73
|
+
- rule_table.gemspec
|
74
|
+
homepage: https://github.com/iain/rule_table
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.4.5.1
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: A simple implementation of a rule table with lots of metaprogramming.
|
98
|
+
test_files: []
|
99
|
+
has_rdoc:
|