action_director 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +10 -0
- data/action_director.gemspec +29 -0
- data/lib/action_director/directable.rb +71 -0
- data/lib/action_director/directing.rb +21 -0
- data/lib/action_director/directive.rb +35 -0
- data/lib/action_director/version.rb +3 -0
- data/lib/action_director.rb +7 -0
- data/test/directive_test.rb +34 -0
- data/test/test_directive_from.rb +25 -0
- data/test/test_helper.rb +8 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YmNjOGMxNmEwMTEwMTNjY2MxZDU5MjU1ZjliMDVjZmRhNzU3Zjk3NQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OTM2OGVmOWM5YmEzOTQ2ODVkOGE2OTViOTkyNmNiYWFiMTEwYzUzMA==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZWY5MzU3YTFmM2ZmYjBkNDhlNDMxMDdiMjRhZjFmNzhmNGUxNzdkNGNjYjQ1
|
10
|
+
ZTExYjlhMTUzM2U3OTJkYzk0N2Q1ZTQ4NDA1YmZiNGJiNmI3Yjc3ZWFmMDAy
|
11
|
+
ZWExMmZkNTc0MzEyZDcxMTVlMjVlZmY1ZTIyZTI0OGQwY2Y1YzU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZTkwOWRmNzdiMDllNjQ4Y2NhN2ZiODNjN2FjNjJiMWM0NjVjMjE3NjYwZmFk
|
14
|
+
Mzc2YjAzZTAzNmY5MzdjMDIyNzE4ZmI5NDk1YTcwMjFlNmYyMTkzMTNjMjM2
|
15
|
+
MjI3NGRjODBhMmQyZDJmY2JkY2RmYWM1NTc3YTAxMTIzMDM4ZjQ=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ritchie Paul Buitre
|
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,64 @@
|
|
1
|
+
# ActionDirector
|
2
|
+
|
3
|
+
Directs objects' behaviors
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'action_director'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install action_director
|
18
|
+
|
19
|
+
## Sample Usage
|
20
|
+
|
21
|
+
Range Conditions:
|
22
|
+
|
23
|
+
class AgeLabeler
|
24
|
+
include ActionDirector
|
25
|
+
|
26
|
+
def initialize
|
27
|
+
@labeling = directive('Labeling age').setup do
|
28
|
+
with 1..17, 18..27, 28..37, 38..47, 48..57 do |age, range| "#{range.begin} - #{range.end}" end
|
29
|
+
with 58..100000000000000000000000000000000 do |age, range| "#{range.begin} and above" end
|
30
|
+
otherwise do |age, range| "Unknown" end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def label(age)
|
35
|
+
@labeling.from age
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
Regexp Conditions:
|
40
|
+
|
41
|
+
class JsonResponseDirector < Struct.new(:view_context)
|
42
|
+
include ActionDirector
|
43
|
+
|
44
|
+
def responding
|
45
|
+
@responding ||= direct view_context do
|
46
|
+
with /^succeeded_creating/ do |resource| render action: 'show', status: :created, location: resource end
|
47
|
+
with /^failed/ do |resource| render json: resource.errors, status: :unprocessable_entity end
|
48
|
+
otherwise do |resource| head :no_content end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def method_missing(method_name, *args, &block)
|
53
|
+
resource = args.first
|
54
|
+
responding.for resource, method_name
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
1. Fork it
|
61
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
62
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
63
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
64
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'action_director/version'
|
5
|
+
require "action_director/directable"
|
6
|
+
require "action_director/directive"
|
7
|
+
require "action_director/directing"
|
8
|
+
|
9
|
+
Gem::Specification.new do |spec|
|
10
|
+
spec.name = "action_director"
|
11
|
+
spec.version = ActionDirector::VERSION
|
12
|
+
spec.authors = ["Ritchie Paul Buitre"]
|
13
|
+
spec.email = ["ritchie@richorelse.com"]
|
14
|
+
spec.description = %q{Directs objects' behaviors}
|
15
|
+
spec.summary = %q{You can think of it as either a router for your objects, or as a rapid prototyping toolset.}
|
16
|
+
spec.homepage = "https://github.com/RichOrElse/action-director"
|
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 "bundler", "~> 1.3"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "minitest", "~> 4.7"
|
27
|
+
spec.add_development_dependency "minitest-reporters"
|
28
|
+
spec.add_development_dependency "pry"
|
29
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# directable.rb: Written by Ritchie Paul Buitre (2014)
|
2
|
+
|
3
|
+
module ActionDirector
|
4
|
+
module Directable
|
5
|
+
def with *_conditions, &block # assign conditions (keys) on the same callback (block)
|
6
|
+
_conditions.each do |key| actions[key] = block end
|
7
|
+
self
|
8
|
+
end
|
9
|
+
|
10
|
+
def without *_conditions # removes actions
|
11
|
+
_conditions.each do |key| actions.delete key end
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
def otherwise &block # assigns a default callback for when none of the conditions are met
|
16
|
+
actions.default = block
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def as key, *args # of the eight(8) calling methods, is the most straight forward (direct) way of calling a stored action block
|
21
|
+
stored = actions[key] # use one (1) calling method for different directables instead of many on the same directable (Directive)
|
22
|
+
if stored
|
23
|
+
stored.call *args # here only the succeeding arguments (args) are passed (excluding key)
|
24
|
+
else
|
25
|
+
raise ArgumentError, "#{key.inspect}:#{key.class} did not match any condition"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def by key, *args # passes a key plus (optional) arguments
|
30
|
+
as key, key, *args
|
31
|
+
end
|
32
|
+
|
33
|
+
def of key, subject # passes the subject with the key
|
34
|
+
as key, subject, key # accepts only two (2) arguments
|
35
|
+
end
|
36
|
+
|
37
|
+
def from source # matches (keys) and passes the subject (with matching key)
|
38
|
+
of key_like(source), source # accepts only one (1) argument (source), passes two (2) to the block
|
39
|
+
end
|
40
|
+
|
41
|
+
def to destination, *args # matches (keys) and passes the subject (without a key) plus (optional) arguments
|
42
|
+
alike destination, destination, *args
|
43
|
+
end
|
44
|
+
|
45
|
+
def for subject, condition # evaluates second argument (condition) instead of the first (subject), ideal for hash conditions
|
46
|
+
alike condition, subject, condition # accepts only two (2) arguments and passes both
|
47
|
+
end
|
48
|
+
|
49
|
+
def like so, &block # matches and passes the subject (so)
|
50
|
+
alike so, so, block # accepts only one (1) argument (so)
|
51
|
+
end
|
52
|
+
|
53
|
+
def alike so, *args # derives a key from the subject (so), only passes succeeding arguments (args)
|
54
|
+
as key_like(so), *args # similar to as() method, passing no key they are alike :P
|
55
|
+
end
|
56
|
+
|
57
|
+
def conditions # list block keys
|
58
|
+
actions.keys
|
59
|
+
end
|
60
|
+
|
61
|
+
protected
|
62
|
+
|
63
|
+
def key_like condition # returns a block key matching the argument
|
64
|
+
conditions.find { |key| key === condition }
|
65
|
+
end
|
66
|
+
|
67
|
+
def actions # blocks store
|
68
|
+
@actions ||= {}
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActionDirector
|
2
|
+
module Directing
|
3
|
+
private
|
4
|
+
|
5
|
+
def direct actor = nil, &block
|
6
|
+
directive_for(actor).setup &block
|
7
|
+
end
|
8
|
+
|
9
|
+
def directive name, actor = nil
|
10
|
+
directives[name] = directive_for(actor, name)
|
11
|
+
end
|
12
|
+
|
13
|
+
def directive_for actor, name = nil
|
14
|
+
actor.kind_of?(Directive) ? actor : Directive.new(actor, name)
|
15
|
+
end
|
16
|
+
|
17
|
+
def directives
|
18
|
+
@directives ||= {}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module ActionDirector
|
4
|
+
class Directive < SimpleDelegator
|
5
|
+
include Directable
|
6
|
+
|
7
|
+
def initialize directed = nil, name = nil
|
8
|
+
super @directed = directed
|
9
|
+
@name = name
|
10
|
+
@actions = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
def inspect
|
14
|
+
[@name, 'Directive'].join ' '
|
15
|
+
end
|
16
|
+
|
17
|
+
def setup &block
|
18
|
+
block.arity < 1 ? instance_eval(&block) : block.call(self) unless block.nil?
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def otherwise?
|
23
|
+
!@actions.default.nil?
|
24
|
+
end
|
25
|
+
|
26
|
+
def accept? condition
|
27
|
+
otherwise? || recognize?(condition)
|
28
|
+
end
|
29
|
+
|
30
|
+
def recognize? condition
|
31
|
+
key_like(condition) != nil
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class DirectiveTest < MiniTest::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@directive = ActionDirector::Directive.new().setup do
|
6
|
+
with 1..10 do |input, key| [input, key] end
|
7
|
+
otherwise do [] end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_as_range
|
12
|
+
assert_equal @directive.as(1..10), [nil, nil]
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_as_another_range
|
16
|
+
assert_equal @directive.as(1..13), []
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_of_range
|
20
|
+
assert_equal @directive.of(1..10, 5), [5, 1..10]
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_of_another_range
|
24
|
+
assert_equal @directive.of(1..13, 5), []
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_from_within_range
|
28
|
+
assert_equal @directive.from(5) ,[5, 1..10]
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_from_out_of_range
|
32
|
+
assert_equal @directive.from(0), []
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TestDirectiveFrom < MiniTest::Unit::TestCase
|
4
|
+
include ActionDirector
|
5
|
+
|
6
|
+
def setup
|
7
|
+
@labeling_age = directive('labeling age').setup do
|
8
|
+
with 1..17, 18..27, 28..37, 38..47, 48..57 do |age, range| "#{range.begin} - #{range.end}" end
|
9
|
+
with 58..100000000000000000000000000000000 do |age, range| "#{range.begin} and above" end
|
10
|
+
otherwise do |age, range| "Unknown" end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_from_regular_age
|
15
|
+
assert_equal @labeling_age.from(33), "28 - 37"
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_from_above_regular_age
|
19
|
+
assert_equal @labeling_age.from(99), "58 and above"
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_from_no_age
|
23
|
+
assert_equal @labeling_age.from(nil), "Unknown"
|
24
|
+
end
|
25
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: action_director
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ritchie Paul Buitre
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-28 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '4.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest-reporters
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Directs objects' behaviors
|
84
|
+
email:
|
85
|
+
- ritchie@richorelse.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- LICENSE.txt
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- action_director.gemspec
|
96
|
+
- lib/action_director.rb
|
97
|
+
- lib/action_director/directable.rb
|
98
|
+
- lib/action_director/directing.rb
|
99
|
+
- lib/action_director/directive.rb
|
100
|
+
- lib/action_director/version.rb
|
101
|
+
- test/directive_test.rb
|
102
|
+
- test/test_directive_from.rb
|
103
|
+
- test/test_helper.rb
|
104
|
+
homepage: https://github.com/RichOrElse/action-director
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ! '>='
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.1
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: You can think of it as either a router for your objects, or as a rapid prototyping
|
128
|
+
toolset.
|
129
|
+
test_files:
|
130
|
+
- test/directive_test.rb
|
131
|
+
- test/test_directive_from.rb
|
132
|
+
- test/test_helper.rb
|
133
|
+
has_rdoc:
|