strategize 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/strategize/operation.rb +18 -0
- data/lib/strategize/policy.rb +22 -0
- data/lib/strategize/policy_group.rb +28 -0
- data/lib/strategize/rule.rb +11 -0
- data/lib/strategize/rule_group.rb +16 -0
- data/lib/strategize.rb +7 -0
- data/test/policy_group_test.rb +21 -0
- data/test/policy_test.rb +13 -0
- data/test/rule_test.rb +18 -0
- data/test/test_helper.rb +11 -0
- metadata +72 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 87efd0ffdecd640f1522ccbd34bb1ea15c733d99
|
4
|
+
data.tar.gz: 417c5296430434f94ed464c2606f8dfa45130f60
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f1f3b54f43c019a1a4e1810bcff8d1d4ad049dfa2ff5fc4c599973eafebef4b0a5843b2d2f83de67026db2ccdebc6943d11aa54b4e0ac73e73615dcd3a631f21
|
7
|
+
data.tar.gz: ea04d4dc1a8ad2e00cbf7c9fc76c7620932b99c364863b1f41f3784ac3c1f55c43f8da16d9d10b282a006b0f8b16b96f339e8cf70da538a93295f0818d2c34b9
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Strategize
|
2
|
+
module Operation
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send(:attr_reader, :object)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def operation(&block)
|
10
|
+
@operation = block
|
11
|
+
end
|
12
|
+
|
13
|
+
def process(subject)
|
14
|
+
subject.instance_exec &@operation
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Strategize
|
2
|
+
module Policy
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send(:attr_reader, :object)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def rules
|
10
|
+
@rules ||= RuleGroup.new
|
11
|
+
end
|
12
|
+
|
13
|
+
def rule(predicate)
|
14
|
+
rules.add(predicate)
|
15
|
+
end
|
16
|
+
|
17
|
+
def evaluate(subject)
|
18
|
+
rules.evaluate(subject)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Strategize
|
2
|
+
class PolicyGroup
|
3
|
+
NotPolicyError = Class.new(RuntimeError)
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@policies = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(policy)
|
10
|
+
raise NotPolicyError unless is_policy(policy)
|
11
|
+
@policies.push(policy)
|
12
|
+
end
|
13
|
+
|
14
|
+
def select(subject)
|
15
|
+
@policies.select { |p| p.evaluate(subject) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def find(subject)
|
19
|
+
self.select(subject).first
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def is_policy(policy)
|
25
|
+
policy.included_modules.include?(Policy)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Strategize
|
2
|
+
class RuleGroup
|
3
|
+
def initialize
|
4
|
+
@rules = []
|
5
|
+
end
|
6
|
+
|
7
|
+
def add(predicate)
|
8
|
+
predicate = Rule.new(predicate) if predicate.is_a?(Proc)
|
9
|
+
@rules.push(predicate)
|
10
|
+
end
|
11
|
+
|
12
|
+
def evaluate(subject)
|
13
|
+
@rules.all? { |r| r.evaluate(subject) }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/strategize.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PolicyGroupTest < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@group = Strategize::PolicyGroup.new
|
6
|
+
@group.add(CanDrinkPolicy)
|
7
|
+
@group.add(CanSmokePolicy)
|
8
|
+
@group.add(CanVotePolicy)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_collection_operations_work
|
12
|
+
subject = OpenStruct.new(age: 19)
|
13
|
+
data = OpenStruct.new(permissions: {})
|
14
|
+
|
15
|
+
policies = @group.select(subject)
|
16
|
+
policies.each { |p| p.process(data) }
|
17
|
+
|
18
|
+
assert_equal true, data.permissions[:can_vote]
|
19
|
+
assert_equal true, data.permissions[:can_smoke]
|
20
|
+
end
|
21
|
+
end
|
data/test/policy_test.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PolicyTest < Minitest::Test
|
4
|
+
def test_can_drink_is_valid
|
5
|
+
subject = OpenStruct.new(age: 22)
|
6
|
+
CanDrinkPolicy.evaluate(subject)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_cannot_drink
|
10
|
+
subject = OpenStruct.new(age: 20)
|
11
|
+
CanDrinkPolicy.evaluate(subject)
|
12
|
+
end
|
13
|
+
end
|
data/test/rule_test.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class RuleTest < Minitest::Test
|
4
|
+
def test_can_create_rule
|
5
|
+
rule = Strategize::Rule.new(-> { true })
|
6
|
+
assert rule.is_a?(Strategize::Rule)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_rule_is_valid
|
10
|
+
rule = Strategize::Rule.new(-> { true })
|
11
|
+
assert rule.evaluate(nil)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_rule_is_not_valid
|
15
|
+
rule = Strategize::Rule.new(-> { false })
|
16
|
+
refute rule.evaluate(nil)
|
17
|
+
end
|
18
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'strategize'
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'minitest/reporters'
|
5
|
+
|
6
|
+
test_root = File.dirname(__FILE__)
|
7
|
+
Dir["#{test_root}/support/*.rb"]. each { |file| require file }
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new(color: true)
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: strategize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jdmorlan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest-reporters
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Strategize
|
28
|
+
email:
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/strategize.rb
|
34
|
+
- lib/strategize/operation.rb
|
35
|
+
- lib/strategize/policy.rb
|
36
|
+
- lib/strategize/policy_group.rb
|
37
|
+
- lib/strategize/rule.rb
|
38
|
+
- lib/strategize/rule_group.rb
|
39
|
+
- test/policy_group_test.rb
|
40
|
+
- test/policy_test.rb
|
41
|
+
- test/rule_test.rb
|
42
|
+
- test/test_helper.rb
|
43
|
+
homepage:
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata: {}
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 2.4.5.1
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: Validate rules and perform operations
|
67
|
+
test_files:
|
68
|
+
- test/policy_group_test.rb
|
69
|
+
- test/policy_test.rb
|
70
|
+
- test/rule_test.rb
|
71
|
+
- test/test_helper.rb
|
72
|
+
has_rdoc:
|