rbrules 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.travis.yml +7 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +74 -0
- data/Rakefile +12 -0
- data/lib/rbrules.rb +41 -0
- data/rbrules.gemspec +27 -0
- data/spec/rbrules_spec.rb +48 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3299d0a6a211dc230999c6236d00bb62cf825dbb
|
4
|
+
data.tar.gz: 3a8f7bc7ee3eeeeca9a753463679e1f2ad537b90
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f503edffbbf3a4dd0ce74afdd8dc9e0f96ff57ee3bb00ccd75a5cda6f8a946ff586b1a86eb76657d04c3e3d48d339a5b983663cbf165326f9d52b5a41ef0462d
|
7
|
+
data.tar.gz: d873bb4267ef68573de2d71abfb5cf247ecf12b5d8567584b1461461e8f109ff9aa327458c486319c5f24654b66abcbb5ef182a40b269e107a570cc069157ac7
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Gemfile.lock
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Restorando
|
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,74 @@
|
|
1
|
+
# RbRules
|
2
|
+
|
3
|
+
This library simplifies a rule set definition that can later be used to check if they
|
4
|
+
are satisfied for a given object or to find the rule that a given object doesn't satisfy.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'rbrules'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install rbrules
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Define your ruleset:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
MY_HOUSE_MY_RULES = RbRules.new do |rules|
|
26
|
+
rules.rule(:smoke) { |age| age > 21 }
|
27
|
+
rules.rule(:sleep) { |age| age.even? }
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
Test your object
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
MY_HOUSE_MY_RULES.all? 22 # => true
|
35
|
+
MY_HOUSE_MY_RULES.none? 12 # => false
|
36
|
+
```
|
37
|
+
|
38
|
+
If you don't want to pollute your global namespace to define global rules, you can give
|
39
|
+
your a name to your rule set like this:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
RbRules[:salute].rule(:hawaiian) {|string| string =~ /aloha/i }
|
43
|
+
RbRules[:salute].rule(:english) {|string| string =~ /hello|good bye/i }
|
44
|
+
|
45
|
+
RbRules[:salute].any? "Aloha world!"
|
46
|
+
```
|
47
|
+
|
48
|
+
You can also define your custom rules (which should respond to `#call(obj)`) in case
|
49
|
+
you need to take different actions when different rules fail
|
50
|
+
|
51
|
+
For example:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class MagicNumber < Struct.new(:magic_number)
|
55
|
+
|
56
|
+
def call(obj)
|
57
|
+
magic_number == obj
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
RbRules[:random_rules].rule MagicNumber.new(3)
|
63
|
+
|
64
|
+
matching_rule = RbRules[:random_rules].any?(3)
|
65
|
+
matching_rule.magic_number # => 3
|
66
|
+
```
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
1. Fork it
|
71
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
72
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
73
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
74
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rbrules.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
class RbRules
|
2
|
+
|
3
|
+
attr_reader :rules
|
4
|
+
|
5
|
+
@rule_sets = Hash.new { |hash, key| hash[key] = new }
|
6
|
+
|
7
|
+
def self.[](key)
|
8
|
+
@rule_sets[key]
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(&block)
|
12
|
+
@rules = []
|
13
|
+
yield(self) if block_given?
|
14
|
+
end
|
15
|
+
|
16
|
+
def rule(name_or_rule, &block)
|
17
|
+
if name_or_rule.respond_to? :call
|
18
|
+
rules << name_or_rule
|
19
|
+
else
|
20
|
+
rules << Rule.new(name_or_rule, block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
%w[all? none?].each do |method_name|
|
25
|
+
define_method method_name do |*args|
|
26
|
+
rules.public_send(method_name) { |rule| rule.call(*args) }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def any?(*args)
|
31
|
+
rules.find do |rule|
|
32
|
+
rule.call(*args)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Rule < Struct.new(:name, :block)
|
37
|
+
|
38
|
+
def call(*args); block.call(*args) end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
data/rbrules.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rbrules"
|
7
|
+
spec.version = "0.1.0"
|
8
|
+
spec.authors = ["Gabriel Schammah", "Juan Barreneche"]
|
9
|
+
spec.email = ["devs@restorando.com"]
|
10
|
+
spec.homepage = "http://engineering.restorando.com"
|
11
|
+
spec.description = %q{Declare rule sets to check your objects against them later}
|
12
|
+
spec.summary = <<-SUMMARY
|
13
|
+
This library simplifies a rule set definition that can later be used to check if they
|
14
|
+
are satisfied for a given object and or find the rule that a given object doesn't
|
15
|
+
satisfy.
|
16
|
+
SUMMARY
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files`.split($/)
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "minitest"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "pry"
|
27
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require './lib/rbrules'
|
3
|
+
|
4
|
+
class TestRbRules < MiniTest::Test
|
5
|
+
|
6
|
+
attr_reader :rb_rules
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@rb_rules = RbRules.new do |rules|
|
10
|
+
rules.rule(:hello) { |param| param =~ /hello/ }
|
11
|
+
rules.rule(:world) { |param| param =~ /world/ }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_any?
|
16
|
+
assert rb_rules.any?('hello')
|
17
|
+
assert rb_rules.any?('world')
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_any_returns_rule
|
21
|
+
assert rb_rules.any?('hello').name == :hello
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_all?
|
25
|
+
refute rb_rules.all?('hello')
|
26
|
+
refute rb_rules.all?('world')
|
27
|
+
assert rb_rules.all?('hello world')
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_none?
|
31
|
+
assert rb_rules.none?('none')
|
32
|
+
refute rb_rules.none?('hello')
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
class TestSingleton < TestRbRules
|
38
|
+
|
39
|
+
def setup
|
40
|
+
rb_rules.rule(:hello) { |param| param =~ /hello/ }
|
41
|
+
rb_rules.rule ->(param) { param =~ /world/ }
|
42
|
+
end
|
43
|
+
|
44
|
+
def rb_rules
|
45
|
+
RbRules[:test]
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rbrules
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gabriel Schammah
|
8
|
+
- Juan Barreneche
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-12-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: pry
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
description: Declare rule sets to check your objects against them later
|
57
|
+
email:
|
58
|
+
- devs@restorando.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
|
+
- lib/rbrules.rb
|
70
|
+
- rbrules.gemspec
|
71
|
+
- spec/rbrules_spec.rb
|
72
|
+
homepage: http://engineering.restorando.com
|
73
|
+
licenses:
|
74
|
+
- MIT
|
75
|
+
metadata: {}
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 2.2.2
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: This library simplifies a rule set definition that can later be used to check
|
96
|
+
if they are satisfied for a given object and or find the rule that a given object
|
97
|
+
doesn't satisfy.
|
98
|
+
test_files:
|
99
|
+
- spec/rbrules_spec.rb
|