mushin 0.0.0.pre94 → 0.1.0

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.
data/lib/mushin/base.rb DELETED
@@ -1,107 +0,0 @@
1
- module Mushin # Domain Frameworks Generator
2
- module Errors
3
- exceptions = %w[ UnkownDSLConstruct UnkownUse UnknownOption ]
4
- exceptions.each { |e| const_set(e, Class.new(StandardError)) }
5
- end
6
-
7
- module Env
8
- def register &block
9
- module_eval &block
10
- end
11
-
12
- def set id, &block
13
- raise "Domain Framework must impelment set"
14
- end
15
- def get id
16
- raise "Domain Framework must impelment get"
17
- end
18
- end
19
- module Domain
20
- module Mushin::Domain::Middleware
21
- class << self
22
- attr_accessor :opts, :params
23
- def opts
24
- #@opts ||= {}
25
- @opts = Hash.new
26
- end
27
- def params
28
- #@params ||= {}
29
- @params = Hash.new
30
- end
31
- end
32
- module Opts
33
- def self.[] key
34
- Mushin::Domain::Middleware.opts[key]
35
- end
36
- def self.[]= key, value
37
- Mushin::Domain::Middleware.opts.merge! Hash[key, value]
38
- end
39
- end
40
- module Params
41
- def self.[] key
42
- Mushin::Domain::Middleware.params[key]
43
- end
44
- def self.[]= key, value
45
- Mushin::Domain::Middleware.params.merge! Hash[key, value]
46
- end
47
- end
48
- end
49
- end
50
-
51
- module DSL
52
- class Context
53
- attr_accessor :title, :activities
54
- def initialize title
55
- @title = title
56
- @activities = []
57
- end
58
- end
59
- class Activity
60
- attr_accessor :title, :uses
61
- def initialize title
62
- @title = title
63
- @uses = []
64
- end
65
- end
66
- class Use
67
- attr_accessor :name, :opts, :params
68
- def initialize name, opts={}, params={}
69
- @name = name
70
- @opts = opts
71
- @params = params
72
- end
73
- end
74
-
75
- class << self
76
- attr_accessor :contexts, :middlewares
77
- end
78
- Mushin::DSL.contexts = []
79
- def context title, &block
80
- @context = Mushin::DSL::Context.new title
81
- def activity activity=[], &block
82
- @activity = Mushin::DSL::Activity.new activity
83
- def use name, opts={}, params={}
84
- if !@activity.uses.bsearch {|x| x == [name,opts,params]} #TODO I think bsearch should be commented out as there are certain sequential logics that requires the use of a certain middleware more than once in a single activity
85
-
86
- @use = Mushin::DSL::Use.new name, opts, params
87
- @activity.uses << @use
88
- end
89
- end
90
- yield
91
- @context.activities << @activity
92
- end
93
- yield
94
- Mushin::DSL.contexts << @context #TODO can't deal with class variables, needs to initialize this? or the DSL will be the same for all users? I think the DSL will be the same for all users so no need to worry about this being class variable on the application level.
95
- end
96
- end
97
-
98
- module Engine
99
- require_relative './middleware/builder'
100
- require_relative './middleware/runner'
101
-
102
- attr_reader :setup_middlewares # set it through the setup method and then read it no need for attr_accessor :setup_middlewares
103
- def setup before_stack = []
104
- @setup_middlewares = before_stack
105
- end
106
- end
107
- end
@@ -1,139 +0,0 @@
1
- module Mushin
2
- module Middleware
3
- # This provides a DSL for building up a stack of middlewares.
4
- #
5
- # This code is based heavily off of `Rack::Builder` and
6
- # `ActionDispatch::MiddlewareStack` in Rack and Rails, respectively.
7
- #
8
- # # Usage
9
- #
10
- # Building a middleware stack is very easy:
11
- #
12
- # app = Middleware::Builder.new do
13
- # use A
14
- # use B
15
- # end
16
- #
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
42
-
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
50
- end
51
-
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
66
- end
67
-
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
75
-
76
- alias_method :insert_before, :insert
77
-
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
84
-
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
96
-
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)
101
- end
102
-
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
108
-
109
- protected
110
-
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
119
-
120
- nil
121
- end
122
-
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
130
-
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
137
- end
138
- end
139
- end
@@ -1,71 +0,0 @@
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| }
9
-
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
23
-
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
34
-
35
- protected
36
-
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
47
-
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 ||= []
52
-
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
68
- end
69
- end
70
- end
71
- end