mushin 0.0.0.pre4 → 0.0.0.pre5

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: 1e5db142fcbe592ac14841d36cf5ac54c3c04d43
4
- data.tar.gz: 1b0684e30975a5c690b45648d45dce8daefd2d34
3
+ metadata.gz: 57d69b4ddf43f86cb77ad4605f6b56c9390f557e
4
+ data.tar.gz: cbcb2987546f85ba505dc869ad704df8431258d3
5
5
  SHA512:
6
- metadata.gz: 247097d0563696a53f8bee7202fbf99b0ee4c2c0526b2342b0acd5de389cfe5de530a15d8125b251559eb2d033d49e930e44575b0b743262531f68571016fe80
7
- data.tar.gz: 3814c50c06ceabc26affcc17f2357e7420eb3a9f9f3bfb7127cb3c72fff1f6da19ca21ae93d59986f3b7967331911d17052acfbc38ff7b67f2d282e1a9f44711
6
+ metadata.gz: f03d782e256b51ec7f1a717c2fbfacdae5e7c9dff087eacd3ba0ac34c5719674c1710c8fe408cac8ec4b7551844d8032078711f0f279fd424eaaecba2c0dc4e5
7
+ data.tar.gz: 540f7ffde560fae05ab43b3df6616ee1276271e87f0dc8011fd2ad727876b8434728df537537acc0dd841d43af30f45b3af6d0cd8e3ff32260d49277cb3b5f62
@@ -0,0 +1,125 @@
1
+ require_relative './middleware/builder'
2
+ require_relative './middleware/runner'
3
+
4
+ module Mushin
5
+ module DSL
6
+ #Usage:
7
+ # module GameOn
8
+ # module Book
9
+ # include Mushin::DSL::Notebook
10
+ # def mybook
11
+ # p 'book works'
12
+ # end
13
+ # end
14
+ # end
15
+ #
16
+ # module MyDomainBook
17
+ # extend GameOn::Book
18
+ # mybook
19
+ # mynotebook
20
+ # end
21
+ module Notebook
22
+ def self.extended(mod)
23
+ puts "#{self} extended in #{mod}"
24
+ #puts "#{self} included in #{mod}"
25
+ self.send(:include, mod)
26
+ #mod.send(:extend, self)
27
+ #TODO Inject somthing useful logging,exceptions, etc.
28
+ #prepend mod
29
+ end
30
+
31
+ def self.find activity_construct, activity_rule
32
+ @@middlewares = []
33
+ @@constructs.each do |construct|
34
+ if activity_construct == construct.title
35
+ construct.rules.each do |rule|
36
+ if activity_rule == rule.title
37
+ rule.all_dynamix.each do |middleware|
38
+ @@middlewares << middleware
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ @@middlewares
45
+ end
46
+ end
47
+
48
+ class Activities
49
+ def self.on domain_context, &block
50
+ #p construct
51
+ @@domain_context = domain_context
52
+ @@activities = []
53
+
54
+ def add activity=[]
55
+ @@activities += [activity]
56
+ end
57
+
58
+ yield Activities.new
59
+
60
+ @@activities.each do |activity|
61
+ p @@domain_context
62
+ #p self
63
+ self.construction @@domain_context, activity
64
+ #Mushin::Engine.run @@construct, activity
65
+ end
66
+ end
67
+
68
+ def self.domain_context
69
+ @@domain_context
70
+ end
71
+
72
+ def self.all
73
+ @@activities
74
+ end
75
+
76
+ def self.count
77
+ @@activities.count
78
+ end
79
+ end
80
+ end
81
+
82
+ class Env
83
+ class << self
84
+ attr_accessor :id
85
+ end
86
+
87
+ def Env.register &block
88
+ #instance_eval &block
89
+ class_eval &block
90
+ end
91
+
92
+ def Env.activate id, &block
93
+ @id = id
94
+ yield
95
+ end
96
+ end
97
+
98
+ module Engine
99
+ def Engine.setup before_stack = []
100
+ @@setup_middlewares = before_stack
101
+ end
102
+
103
+ def Engine.run domain_context, activity
104
+ p 'Mushin::Engine is running'
105
+ @@domain_context = domain_context
106
+ @@activity = activity
107
+ #TODO write metaprog code in Mushin::DSL::Gamebooks to figure out the modules that included it via the included(self) and then trigger the find function on it to get its own domain
108
+ @@middlewares = Mushin::DSL::Notebook.find @@domain_context, @@activity
109
+ #p @@middlewares
110
+ @@stack = Middleware::Builder.new do
111
+ #use GameOn::Persistence::DS
112
+ @@middlewares.each do |middleware|
113
+ #p "use #{middleware.name}, #{middleware.opts}, #{middleware.params}"
114
+ use middleware.name, middleware.opts, middleware.params
115
+ end
116
+ end
117
+ @@setup_middlewares.each do |setup_middleware|
118
+ #@@stack.insert_before 1, setup_middleware #TODO should be one?
119
+ @@stack.insert_before 0, setup_middleware
120
+ end
121
+ @@stack.call
122
+ #p @@stack.methods
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,137 @@
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.
22
+ #
23
+ # Example:
24
+ #
25
+ # Builder.new do
26
+ # use A
27
+ # use B
28
+ # end
29
+ #
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
41
+
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)
48
+ end
49
+ end
50
+
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]
62
+ end
63
+
64
+ self
65
+ end
66
+
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
74
+
75
+ alias_method :insert_before, :insert
76
+
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
83
+
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)
93
+ end
94
+ end
95
+
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
101
+
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
107
+
108
+ protected
109
+
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
117
+ end
118
+
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
129
+
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)
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,69 @@
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| }
8
+
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
22
+
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
33
+
34
+ protected
35
+
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
46
+
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 ||= []
51
+
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
66
+ end
67
+ end
68
+ end
69
+ 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.pre4
4
+ version: 0.0.0.pre5
5
5
  platform: ruby
6
6
  authors:
7
7
  - theotherstupidguy
@@ -18,6 +18,9 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/mushin.rb
21
+ - lib/mushin/base.rb
22
+ - lib/mushin/middleware/builder.rb
23
+ - lib/mushin/middleware/runner.rb
21
24
  homepage: https://github.com/mushin-rb/mushin
22
25
  licenses:
23
26
  - MIT