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