motion_flux 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d507b7c085d4fd6879c0fa57a2441a139decac6e
4
- data.tar.gz: ac6b3a180bee707c5df7070cb8ce859015859d5f
3
+ metadata.gz: f127b39640ef0cdf1e2d154c5d032fdfb0ea422d
4
+ data.tar.gz: ae5684932f0553e723c4cb75d5c6d3986b023175
5
5
  SHA512:
6
- metadata.gz: 9e354608a93ba7d91043d29cffff4fd07f2c847a8e4ff3e0c9cfd34d3c5da63db77cabc54ab04351dd34071e596656ed646f6d25c7352798cc34fd613f40aa15
7
- data.tar.gz: d5bfa3d67fb067a2313608d5eeb1affb6becf1e4b192d770fa4b9a3d156405996e749893461ec4f8068ee364423a203ebf74f766ee05f11d037da9a39c1d9db9
6
+ metadata.gz: 3e2c6eda607bddd68d842801beac3577230cdaa36935dcda68a4b544e34389db2c16f021430cc6623abcbc19908ccf84d6e98f3c98e83a546036756053208741
7
+ data.tar.gz: cf34fdf25a442a32ff8ba5dcfbdd02b616ff3582a91ff34b5986ac877ae95b7106b3106fdb389e7bd6d0d9847096c6a98b3340a53db89c38a524514c88a0ee83
@@ -0,0 +1,13 @@
1
+ require 'tsort'
2
+
3
+ module MotionFlux
4
+ class DependencyGraph < Hash
5
+ include TSort
6
+
7
+ alias_method :tsort_each_node, :each_key
8
+
9
+ def tsort_each_child node, &block
10
+ (self[node] || []).each(&block)
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,5 @@
1
+ require 'motion_flux/dependency_graph'
2
+
1
3
  module MotionFlux
2
4
  module Dispatcher
3
5
  module_function
@@ -6,18 +8,28 @@ module MotionFlux
6
8
  @subscribers ||= Hash.new { |hash, key| hash[key] = [] }
7
9
  end
8
10
 
11
+ def dependencies
12
+ @dependencies ||= DependencyGraph.new
13
+ end
14
+
9
15
  def register store, action
10
- puts "#{store} is registered on #{action}"
11
16
  subscribers[action] << store
12
17
  end
13
18
 
19
+ def add_dependency store, depended_stores
20
+ @order = nil
21
+ dependencies[store] ||= []
22
+ dependencies[store].concat Array.wrap(depended_stores)
23
+ end
24
+
14
25
  def clear
15
26
  subscribers.clear
27
+ dependencies.clear
16
28
  end
17
29
 
18
30
  def dispatch action
19
31
  exclusive_run action do
20
- subscribers[action.class].each do |store|
32
+ ordered_subscribers(action).each do |store|
21
33
  if store.respond_to? action.message
22
34
  store.send action.message, action
23
35
  else
@@ -27,6 +39,11 @@ module MotionFlux
27
39
  end
28
40
  end
29
41
 
42
+ def ordered_subscribers action
43
+ @order ||= dependencies.tsort.each_with_index.to_h
44
+ subscribers[action.class].sort_by { |x| @order[x] || @order.length }
45
+ end
46
+
30
47
  def exclusive_run action, &proc
31
48
  if @current_action
32
49
  puts 'cascading action dispatch is not allowed.'
@@ -9,7 +9,12 @@ module MotionFlux
9
9
  MotionFlux::Dispatcher.register instance, action
10
10
  end
11
11
 
12
+ def wait_for *stores
13
+ MotionFlux::Dispatcher.add_dependency instance, stores.map(&:instance)
14
+ end
15
+
12
16
  def store_attribute *attrs
17
+ options = attrs.extract_options!
13
18
  attrs.each do |attr|
14
19
  attributes << attr
15
20
 
@@ -17,6 +22,10 @@ module MotionFlux
17
22
  define_singleton_method attr do
18
23
  instance.send attr
19
24
  end
25
+
26
+ if options[:default]
27
+ instance.instance_variable_set "@#{attr}", options[:default]
28
+ end
20
29
  end
21
30
  end
22
31
 
@@ -1,3 +1,3 @@
1
1
  module MotionFlux
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
data/motion_flux.gemspec CHANGED
@@ -26,6 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'rspec'
27
27
  spec.add_development_dependency 'fuubar'
28
28
  spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'pry-doc'
29
30
  spec.add_development_dependency 'rubocop'
30
31
  spec.add_development_dependency 'guard'
31
32
  spec.add_development_dependency 'guard-rspec'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion_flux
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - kayhide
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-24 00:00:00.000000000 Z
11
+ date: 2015-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: motion_blender
@@ -108,6 +108,20 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry-doc
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  - !ruby/object:Gem::Dependency
112
126
  name: rubocop
113
127
  requirement: !ruby/object:Gem::Requirement
@@ -212,6 +226,7 @@ files:
212
226
  - bin/setup
213
227
  - lib/motion_flux.rb
214
228
  - lib/motion_flux/action.rb
229
+ - lib/motion_flux/dependency_graph.rb
215
230
  - lib/motion_flux/dispatcher.rb
216
231
  - lib/motion_flux/store.rb
217
232
  - lib/motion_flux/version.rb