mushin 0.0.0.pre49 → 0.0.0.pre50
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 +4 -4
- data/lib/mushin/base.rb +24 -23
- data/lib/mushin/middleware/builder.rb +113 -111
- data/lib/mushin/middleware/runner.rb +61 -59
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cabcee365ccfdc8afe6f1ed6f8a7481c15c577d5
|
4
|
+
data.tar.gz: fd105bd186c500e080b50aefcc29dc741ce83ebb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf159f32f2a2c960e69cb333fddbda306331e6569ada708914adc8dd861049820bb363506bbb9a19e150481c5f6d876c976a49540a75a57b03619e361d7bd7ff
|
7
|
+
data.tar.gz: 988a31cdc58a19e19f53e912f3861895917c53c42ab68cb5a15764b810af18d138802e2a12ce1f71557fa42493435011adb6edc767305f5dac4fbf943feecb4b
|
data/lib/mushin/base.rb
CHANGED
@@ -19,31 +19,32 @@ module Mushin # Domain Frameworks Generator
|
|
19
19
|
raise "Domain Framework must impelment get"
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
end
|
33
|
-
module Opts
|
34
|
-
def self.[] key
|
35
|
-
Mushin::Middleware.opts[key]
|
36
|
-
end
|
37
|
-
def self.[]= key, value
|
38
|
-
Mushin::Middleware.opts.merge! Hash[key, value]
|
22
|
+
module Domain
|
23
|
+
module Mushin::Domain::Middleware
|
24
|
+
class << self
|
25
|
+
attr_accessor :opts, :params
|
26
|
+
def opts
|
27
|
+
@opts ||= {}
|
28
|
+
end
|
29
|
+
def params
|
30
|
+
@params ||= {}
|
31
|
+
end
|
39
32
|
end
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
33
|
+
module Opts
|
34
|
+
def self.[] key
|
35
|
+
Mushin::Domain::Middleware.opts[key]
|
36
|
+
end
|
37
|
+
def self.[]= key, value
|
38
|
+
Mushin::Domain::Middleware.opts.merge! Hash[key, value]
|
39
|
+
end
|
44
40
|
end
|
45
|
-
|
46
|
-
|
41
|
+
module Params
|
42
|
+
def self.[] key
|
43
|
+
Mushin::Domain::Middleware.params[key]
|
44
|
+
end
|
45
|
+
def self.[]= key, value
|
46
|
+
Mushin::Domain::Middleware.params.merge! Hash[key, value]
|
47
|
+
end
|
47
48
|
end
|
48
49
|
end
|
49
50
|
end
|
@@ -1,137 +1,139 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
# This code is based heavily off of `Rack::Builder` and
|
5
|
-
# `ActionDispatch::MiddlewareStack` in Rack and Rails, respectively.
|
6
|
-
#
|
7
|
-
# # Usage
|
8
|
-
#
|
9
|
-
# Building a middleware stack is very easy:
|
10
|
-
#
|
11
|
-
# app = Middleware::Builder.new do
|
12
|
-
# use A
|
13
|
-
# use B
|
14
|
-
# end
|
15
|
-
#
|
16
|
-
# # Call the middleware
|
17
|
-
# app.call(7)
|
18
|
-
#
|
19
|
-
class Builder
|
20
|
-
# Initializes the builder. An optional block can be passed which
|
21
|
-
# will be evaluated in the context of the instance.
|
1
|
+
module Mushin
|
2
|
+
module Middleware
|
3
|
+
# This provides a DSL for building up a stack of middlewares.
|
22
4
|
#
|
23
|
-
#
|
5
|
+
# This code is based heavily off of `Rack::Builder` and
|
6
|
+
# `ActionDispatch::MiddlewareStack` in Rack and Rails, respectively.
|
24
7
|
#
|
25
|
-
#
|
8
|
+
# # Usage
|
9
|
+
#
|
10
|
+
# Building a middleware stack is very easy:
|
11
|
+
#
|
12
|
+
# app = Middleware::Builder.new do
|
26
13
|
# use A
|
27
14
|
# use B
|
28
15
|
# end
|
29
16
|
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
17
|
+
# # Call the middleware
|
18
|
+
# app.call(7)
|
19
|
+
#
|
20
|
+
class Builder
|
21
|
+
# Initializes the builder. An optional block can be passed which
|
22
|
+
# will be evaluated in the context of the instance.
|
23
|
+
#
|
24
|
+
# Example:
|
25
|
+
#
|
26
|
+
# Builder.new do
|
27
|
+
# use A
|
28
|
+
# use B
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# @param [Hash] opts Options hash
|
32
|
+
# @option opts [Class] :runner_class The class to wrap the middleware stack
|
33
|
+
# in which knows how to run them.
|
34
|
+
# @yield [] Evaluated in this instance which allows you to use methods
|
35
|
+
# like {#use} and such.
|
36
|
+
#def game name, *dyx, &block
|
37
|
+
def initialize(opts=nil, &block)
|
38
|
+
opts ||= {}
|
39
|
+
@runner_class = opts[:runner_class] || Runner
|
40
|
+
instance_eval(&block) if block_given?
|
41
|
+
end
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
# Returns a mergeable version of the builder. If `use` is called with
|
44
|
+
# the return value of this method, then the stack will merge, instead
|
45
|
+
# of being treated as a separate single middleware.
|
46
|
+
def flatten
|
47
|
+
lambda do |env|
|
48
|
+
self.call(env)
|
49
|
+
end
|
48
50
|
end
|
49
|
-
end
|
50
51
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
52
|
+
# Adds a middleware class to the middleware stack. Any additional
|
53
|
+
# args and a block, if given, are saved and passed to the initializer
|
54
|
+
# of the middleware.
|
55
|
+
#
|
56
|
+
# @param [Class] middleware The middleware class
|
57
|
+
def use(middleware, *args, &block)
|
58
|
+
if middleware.kind_of?(Builder)
|
59
|
+
# Merge in the other builder's stack into our own
|
60
|
+
self.stack.concat(middleware.stack)
|
61
|
+
else
|
62
|
+
self.stack << [middleware, args, block]
|
63
|
+
end
|
64
|
+
|
65
|
+
self
|
62
66
|
end
|
63
67
|
|
64
|
-
|
65
|
-
|
68
|
+
# Inserts a middleware at the given index or directly before the
|
69
|
+
# given middleware object.
|
70
|
+
def insert(index, middleware, *args, &block)
|
71
|
+
index = self.index(index) unless index.is_a?(Integer)
|
72
|
+
raise "no such middleware to insert before: #{index.inspect}" unless index
|
73
|
+
stack.insert(index, [middleware, args, block])
|
74
|
+
end
|
66
75
|
|
67
|
-
|
68
|
-
# given middleware object.
|
69
|
-
def insert(index, middleware, *args, &block)
|
70
|
-
index = self.index(index) unless index.is_a?(Integer)
|
71
|
-
raise "no such middleware to insert before: #{index.inspect}" unless index
|
72
|
-
stack.insert(index, [middleware, args, block])
|
73
|
-
end
|
76
|
+
alias_method :insert_before, :insert
|
74
77
|
|
75
|
-
|
78
|
+
# Inserts a middleware after the given index or middleware object.
|
79
|
+
def insert_after(index, middleware, *args, &block)
|
80
|
+
index = self.index(index) unless index.is_a?(Integer)
|
81
|
+
raise "no such middleware to insert after: #{index.inspect}" unless index
|
82
|
+
insert(index + 1, middleware, *args, &block)
|
83
|
+
end
|
76
84
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
85
|
+
# Replaces the given middleware object or index with the new
|
86
|
+
# middleware.
|
87
|
+
def replace(index, middleware, *args, &block)
|
88
|
+
if index.is_a?(Integer)
|
89
|
+
delete(index)
|
90
|
+
insert(index, middleware, *args, &block)
|
91
|
+
else
|
92
|
+
insert_before(index, middleware, *args, &block)
|
93
|
+
delete(index)
|
94
|
+
end
|
95
|
+
end
|
83
96
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
delete(index)
|
89
|
-
insert(index, middleware, *args, &block)
|
90
|
-
else
|
91
|
-
insert_before(index, middleware, *args, &block)
|
92
|
-
delete(index)
|
97
|
+
# Deletes the given middleware object or index
|
98
|
+
def delete(index)
|
99
|
+
index = self.index(index) unless index.is_a?(Integer)
|
100
|
+
stack.delete_at(index)
|
93
101
|
end
|
94
|
-
end
|
95
102
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
103
|
+
# Runs the builder stack with the given environment.
|
104
|
+
def call(env = Hash.new)
|
105
|
+
#def call(env=nil)
|
106
|
+
to_app.call(env)
|
107
|
+
end
|
101
108
|
|
102
|
-
|
103
|
-
def call(env = Hash.new)
|
104
|
-
#def call(env=nil)
|
105
|
-
to_app.call(env)
|
106
|
-
end
|
109
|
+
protected
|
107
110
|
|
108
|
-
|
111
|
+
# Returns the numeric index for the given middleware object.
|
112
|
+
#
|
113
|
+
# @param [Object] object The item to find the index for
|
114
|
+
# @return [Integer]
|
115
|
+
def index(object)
|
116
|
+
stack.each_with_index do |item, i|
|
117
|
+
return i if item[0] == object
|
118
|
+
end
|
109
119
|
|
110
|
-
|
111
|
-
#
|
112
|
-
# @param [Object] object The item to find the index for
|
113
|
-
# @return [Integer]
|
114
|
-
def index(object)
|
115
|
-
stack.each_with_index do |item, i|
|
116
|
-
return i if item[0] == object
|
120
|
+
nil
|
117
121
|
end
|
118
122
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
def stack
|
127
|
-
@stack ||= []
|
128
|
-
end
|
123
|
+
# Returns the current stack of middlewares. You probably won't
|
124
|
+
# need to use this directly, and it's recommended that you don't.
|
125
|
+
#
|
126
|
+
# @return [Array]
|
127
|
+
def stack
|
128
|
+
@stack ||= []
|
129
|
+
end
|
129
130
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
131
|
+
# Converts the builder stack to a runnable action sequence.
|
132
|
+
#
|
133
|
+
# @return [Object] A callable object
|
134
|
+
def to_app
|
135
|
+
@runner_class.new(stack.dup)
|
136
|
+
end
|
135
137
|
end
|
136
138
|
end
|
137
139
|
end
|
@@ -1,68 +1,70 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module Mushin
|
2
|
+
module Middleware
|
3
|
+
# This is a basic runner for middleware stacks. This runner does
|
4
|
+
# the default expected behavior of running the middleware stacks
|
5
|
+
# in order, then reversing the order.
|
6
|
+
class Runner
|
7
|
+
# A middleware which does nothing
|
8
|
+
EMPTY_MIDDLEWARE = lambda { |env| }
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
10
|
+
# Build a new middleware runner with the given middleware
|
11
|
+
# stack.
|
12
|
+
#
|
13
|
+
# Note: This class usually doesn't need to be used directly.
|
14
|
+
# Instead, take a look at using the {Builder} class, which is
|
15
|
+
# a much friendlier way to build up a middleware stack.
|
16
|
+
#
|
17
|
+
# @param [Array] stack An array of the middleware to run.
|
18
|
+
def initialize(stack)
|
19
|
+
# We need to take the stack of middleware and initialize them
|
20
|
+
# all so they call the proper next middleware.
|
21
|
+
@kickoff = build_call_chain(stack)
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
24
|
+
# Run the middleware stack with the given state bag.
|
25
|
+
#
|
26
|
+
# @param [Object] env The state to pass into as the initial
|
27
|
+
# environment data. This is usual a hash of some sort.
|
28
|
+
def call(env)
|
29
|
+
# We just call the kickoff middleware, which is responsible
|
30
|
+
# for properly calling the next middleware, and so on and so
|
31
|
+
# forth.
|
32
|
+
@kickoff.call(env)
|
33
|
+
end
|
33
34
|
|
34
|
-
|
35
|
+
protected
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
37
|
+
# This takes a stack of middlewares and initializes them in a way
|
38
|
+
# that each middleware properly calls the next middleware.
|
39
|
+
def build_call_chain(stack)
|
40
|
+
# We need to instantiate the middleware stack in reverse
|
41
|
+
# order so that each middleware can have a reference to
|
42
|
+
# the next middleware it has to call. The final middleware
|
43
|
+
# is always the empty middleware, which does nothing but return.
|
44
|
+
stack.reverse.inject(EMPTY_MIDDLEWARE) do |next_middleware, current_middleware|
|
45
|
+
# Unpack the actual item
|
46
|
+
klass, args, block = current_middleware
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
48
|
+
# Default the arguments to an empty array. Otherwise in Ruby 1.8
|
49
|
+
# a `nil` args will actually pass `nil` into the class. Not what
|
50
|
+
# we want!
|
51
|
+
args ||= []
|
51
52
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
53
|
+
if klass.is_a?(Class)
|
54
|
+
# If the klass actually is a class, then instantiate it with
|
55
|
+
# the app and any other arguments given.
|
56
|
+
klass.new(next_middleware, *args, &block)
|
57
|
+
elsif klass.respond_to?(:call)
|
58
|
+
# Make it a lambda which calls the item then forwards up
|
59
|
+
# the chain.
|
60
|
+
lambda do |env|
|
61
|
+
klass.call(env)
|
62
|
+
next_middleware.call(env)
|
63
|
+
end
|
64
|
+
else
|
65
|
+
raise "Invalid middleware, doesn't respond to `call`: #{action.inspect}"
|
66
|
+
end
|
67
|
+
end
|
66
68
|
end
|
67
69
|
end
|
68
70
|
end
|