metacosm 0.2.3 → 0.2.4

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: a60e66baf1458103a4f71cd3b30cb05c96a003bb
4
- data.tar.gz: 5abac4b526875ec5269997ee9400791148707f3d
3
+ metadata.gz: 97d3af7dc335e54de93016c0dbe48a1fd26ba0a9
4
+ data.tar.gz: 7b18a06be6981863ff0a0e0ae50b03af83e8f4cc
5
5
  SHA512:
6
- metadata.gz: a34c6b7ab40dafd80fbfc261e788fed0db794f04f19b529c4368e7ae12d4c669bed5b64609f8b628487607f7f9dc685fa2c143be70c2ae2ffb8d08f4afd313d0
7
- data.tar.gz: e774445c009e84bfc222715a4aed40d546d398677251c2602326391cb8e1409797d044641dbb21dce52efd6eefc4cf3bab6f6b5b20e52f465dc8d08e5fdaeef2
6
+ metadata.gz: 61a7bfba459ea14a2b501f5e1f7b722f04291863c69cc4c3ddda8b5f898bb214b9b4d06d2af36b7d0fd9afb84edeccaaeb09b6a5bc6510708d2446806cf463fb
7
+ data.tar.gz: e95aa9198ab97a660b89c4e72168148cf5242fd74e13b26d0af80172568f3b6e565faa39fabe9bce549be44c832b125b82fdd17eea85ed2bd6a916508dadea6b
@@ -66,7 +66,17 @@ module Metacosm
66
66
  protected
67
67
  def handler_for(command)
68
68
  @handlers ||= {}
69
- @handlers[command.class] ||= Object.const_get(command.class.name.split('::').last + "Handler").new
69
+ @handlers[command.class] ||= construct_handler_for(command)
70
+ end
71
+
72
+ def construct_handler_for(command)
73
+ #binding.pry
74
+ module_name = command.class.name.deconstantize # || "Object"
75
+ module_name = "Object" if module_name.empty?
76
+ (module_name.constantize).
77
+ #Object.
78
+ const_get(command.class.name.demodulize + "Handler").new
79
+ #Object
70
80
  end
71
81
 
72
82
  def listener_for(event)
@@ -75,7 +85,9 @@ module Metacosm
75
85
  end
76
86
 
77
87
  def construct_listener_for(event)
78
- listener = Object.const_get(event.class.name.split('::').last + "Listener").new(self)
88
+ module_name = event.class.name.deconstantize # || "Object"
89
+ module_name = "Object" if module_name.empty?
90
+ listener = (module_name.constantize).const_get(event.class.name.demodulize + "Listener").new(self)
79
91
  listener
80
92
  end
81
93
  end
@@ -1,4 +1,4 @@
1
1
  module Metacosm
2
2
  # metacosm version
3
- VERSION = "0.2.3"
3
+ VERSION = "0.2.4"
4
4
  end
@@ -141,7 +141,7 @@ end
141
141
 
142
142
  describe "a more complex simulation (village)" do
143
143
  subject(:simulation) { Simulation.current }
144
- let!(:world) { World.create(id: world_id) }
144
+ let!(:world) { Village::World.create(id: world_id) }
145
145
  let(:world_id) { 'world_id' }
146
146
 
147
147
  describe "#apply" do
@@ -153,7 +153,7 @@ describe "a more complex simulation (village)" do
153
153
  let(:people_per_village) { 10 }
154
154
 
155
155
  let(:create_village_command) do
156
- CreateVillageCommand.create(
156
+ Village::CreateVillageCommand.create(
157
157
  world_id: world_id,
158
158
  village_id: village_id,
159
159
  village_name: village_name
@@ -161,14 +161,14 @@ describe "a more complex simulation (village)" do
161
161
  end
162
162
 
163
163
  let(:rename_village_command) do
164
- RenameVillageCommand.create(
164
+ Village::RenameVillageCommand.create(
165
165
  village_id: village_id,
166
166
  new_village_name: "Newcity"
167
167
  )
168
168
  end
169
169
 
170
170
  let(:village_created_event) do
171
- VillageCreatedEvent.create(
171
+ Village::VillageCreatedEvent.create(
172
172
  world_id: world_id,
173
173
  village_id: village_id,
174
174
  name: village_name
@@ -176,7 +176,7 @@ describe "a more complex simulation (village)" do
176
176
  end
177
177
 
178
178
  let(:populate_world_command) do
179
- PopulateWorldCommand.create(
179
+ Village::PopulateWorldCommand.create(
180
180
  world_id: world_id,
181
181
  name_dictionary: %w[ Alice ],
182
182
  per_village: people_per_village
@@ -184,7 +184,7 @@ describe "a more complex simulation (village)" do
184
184
  end
185
185
  #
186
186
  let(:create_person_command) do
187
- CreatePersonCommand.create(
187
+ Village::CreatePersonCommand.create(
188
188
  world_id: world_id,
189
189
  village_id: village_id,
190
190
  person_id: person_id,
@@ -193,15 +193,15 @@ describe "a more complex simulation (village)" do
193
193
  end
194
194
 
195
195
  let(:person_created_event) do
196
- PersonCreatedEvent.create(village_id: village_id, person_id: person_id, name: "Alice")
196
+ Village::PersonCreatedEvent.create(village_id: village_id, person_id: person_id, name: "Alice")
197
197
  end
198
198
 
199
199
  let(:village_names_query) do
200
- VillageNamesQuery.new(world_id)
200
+ Village::VillageNamesQuery.new(world_id)
201
201
  end
202
202
 
203
203
  let(:people_names_query) do
204
- PeopleNamesQuery.new(world_id)
204
+ Village::PeopleNamesQuery.new(world_id)
205
205
  end
206
206
 
207
207
  describe "handling a create village command" do
@@ -1,154 +1,157 @@
1
- class Person < Model
2
- belongs_to :village
3
- attr_accessor :name
4
- end
1
+ module Village
2
+ class Person < Model
3
+ belongs_to :village
4
+ attr_accessor :name
5
+ end
5
6
 
6
- class PersonView < View
7
- belongs_to :village_view
8
- attr_accessor :person_name, :person_id, :village_id
9
- end
7
+ class PersonView < View
8
+ belongs_to :village_view
9
+ attr_accessor :person_name, :person_id, :village_id
10
+ end
10
11
 
11
- class Village < Model
12
- belongs_to :world
13
- has_many :people
14
- attr_accessor :name
15
- end
12
+ class Village < Model
13
+ belongs_to :world
14
+ has_many :people
15
+ attr_accessor :name
16
+ end
16
17
 
17
- class VillageView < View
18
- belongs_to :world_view
19
- has_many :person_views
20
- attr_accessor :name, :village_id, :world_id
21
- end
18
+ class VillageView < View
19
+ belongs_to :world_view
20
+ has_many :person_views
21
+ attr_accessor :name, :village_id, :world_id
22
+ end
22
23
 
23
- class World < Model
24
- has_many :villages
25
- has_many :people, :through => :villages
26
- end
24
+ class World < Model
25
+ has_many :villages
26
+ has_many :people, :through => :villages
27
+ end
27
28
 
28
- class WorldView < View
29
- attr_accessor :world_id
30
- has_many :village_views
31
- has_many :person_views, :through => :village_views
32
- end
29
+ class WorldView < View
30
+ attr_accessor :world_id
31
+ has_many :village_views
32
+ has_many :person_views, :through => :village_views
33
+ end
33
34
 
34
- class CreateVillageCommand < Command
35
- attr_accessor :world_id, :village_id, :village_name
36
- end
35
+ class CreateVillageCommand < Command
36
+ attr_accessor :world_id, :village_id, :village_name
37
+ end
37
38
 
38
- class CreateVillageCommandHandler
39
- def handle(world_id:,village_id:,village_name:)
40
- world = World.where(id: world_id).first_or_create
41
- world.create_village(name: village_name, id: village_id)
42
- self
39
+ class CreateVillageCommandHandler
40
+ def handle(world_id:,village_id:,village_name:)
41
+ world = World.where(id: world_id).first_or_create
42
+ world.create_village(name: village_name, id: village_id)
43
+ self
44
+ end
43
45
  end
44
- end
45
46
 
46
- class VillageCreatedEvent < Event
47
- attr_accessor :village_id, :name, :world_id
48
- end
47
+ class VillageCreatedEvent < Event
48
+ attr_accessor :village_id, :name, :world_id
49
+ end
49
50
 
50
- class VillageCreatedEventListener < EventListener
51
- def receive(world_id:, village_id:, name:)
52
- world = WorldView.where(world_id: world_id).first_or_create
53
- world.create_village_view(
54
- world_id: world_id,
55
- village_id: village_id,
56
- name: name
57
- )
51
+ class VillageCreatedEventListener < EventListener
52
+ def receive(world_id:, village_id:, name:)
53
+ world = WorldView.where(world_id: world_id).first_or_create
54
+ world.create_village_view(
55
+ world_id: world_id,
56
+ village_id: village_id,
57
+ name: name
58
+ )
59
+ end
58
60
  end
59
- end
60
61
 
61
- class RenameVillageCommand < Command
62
- attr_accessor :village_id, :new_village_name
63
- end
62
+ class RenameVillageCommand < Command
63
+ attr_accessor :village_id, :new_village_name
64
+ end
64
65
 
65
- class RenameVillageCommandHandler
66
- def handle(village_id:, new_village_name:)
67
- village = Village.find(village_id)
68
- village.update name: new_village_name
66
+ class RenameVillageCommandHandler
67
+ def handle(village_id:, new_village_name:)
68
+ village = Village.find(village_id)
69
+ village.update name: new_village_name
70
+ end
69
71
  end
70
- end
71
72
 
72
- class VillageUpdatedEvent < Event
73
- attr_accessor :village_id, :name
74
- end
73
+ class VillageUpdatedEvent < Event
74
+ attr_accessor :village_id, :name
75
+ end
75
76
 
76
- class VillageUpdatedEventListener < EventListener
77
- def receive(village_id:, name:)
78
- village_view = VillageView.find_by(village_id: village_id)
79
- village_view.name = name
77
+ class VillageUpdatedEventListener < EventListener
78
+ def receive(village_id:, name:)
79
+ village_view = VillageView.find_by(village_id: village_id)
80
+ village_view.name = name
81
+ end
80
82
  end
81
- end
82
83
 
83
- class CreatePersonCommand < Command
84
- attr_accessor :world_id, :village_id, :person_id, :person_name
85
- end
84
+ class CreatePersonCommand < Command
85
+ attr_accessor :world_id, :village_id, :person_id, :person_name
86
+ end
86
87
 
87
- class CreatePersonCommandHandler
88
- def handle(world_id:, village_id:, person_id:, person_name:)
89
- world = World.where(id: world_id).first_or_create
90
- world.create_person(id: person_id, village_id: village_id, name: person_name)
88
+ class CreatePersonCommandHandler
89
+ def handle(world_id:, village_id:, person_id:, person_name:)
90
+ world = World.where(id: world_id).first_or_create
91
+ # binding.pry
92
+ world.create_person(id: person_id, village_id: village_id, name: person_name)
93
+ end
91
94
  end
92
- end
93
95
 
94
- class PersonCreatedEvent < Event
95
- attr_accessor :name, :person_id, :village_id
96
- end
96
+ class PersonCreatedEvent < Event
97
+ attr_accessor :name, :person_id, :village_id
98
+ end
97
99
 
98
- class PersonCreatedEventListener < EventListener
99
- def receive(name:, person_id:, village_id:)
100
- village_view = VillageView.where(village_id: village_id).first
101
- village_view.create_person_view(
102
- person_name: name,
103
- person_id: person_id,
104
- village_id: village_id
105
- )
100
+ class PersonCreatedEventListener < EventListener
101
+ def receive(name:, person_id:, village_id:)
102
+ village_view = VillageView.where(village_id: village_id).first
103
+ village_view.create_person_view(
104
+ person_name: name,
105
+ person_id: person_id,
106
+ village_id: village_id
107
+ )
108
+ end
106
109
  end
107
- end
108
110
 
109
- class PopulateWorldCommand < Command
110
- attr_accessor :world_id, :name_dictionary, :per_village
111
- end
111
+ class PopulateWorldCommand < Command
112
+ attr_accessor :world_id, :name_dictionary, :per_village
113
+ end
112
114
 
113
- class PopulateWorldCommandHandler
114
- def handle(world_id:, name_dictionary:, per_village:)
115
- world_id = world_id
116
- dictionary = name_dictionary
115
+ class PopulateWorldCommandHandler
116
+ def handle(world_id:, name_dictionary:, per_village:)
117
+ world_id = world_id
118
+ dictionary = name_dictionary
117
119
 
118
- world = World.where(id: world_id).first_or_create
120
+ world = World.where(id: world_id).first_or_create
119
121
 
120
- world.villages.each do |village|
121
- name = dictionary.sample
122
- per_village.times do
123
- village.create_person(name: name)
122
+ world.villages.each do |village|
123
+ name = dictionary.sample
124
+ per_village.times do
125
+ village.create_person(name: name)
126
+ end
124
127
  end
125
128
  end
126
129
  end
127
- end
128
130
 
129
- class CreateWorldCommand < Command
130
- attr_accessor :world_id
131
- end
131
+ class CreateWorldCommand < Command
132
+ attr_accessor :world_id
133
+ end
132
134
 
133
- class CreateWorldCommandHandler
134
- def handle(cmd)
135
- world_id = cmd.world_id
136
- World.create(id: world_id)
135
+ class CreateWorldCommandHandler
136
+ def handle(cmd)
137
+ world_id = cmd.world_id
138
+ World.create(id: world_id)
139
+ end
137
140
  end
138
- end
139
141
 
140
- ## queries
142
+ ## queries
141
143
 
142
- class VillageNamesQuery < Struct.new(:world_id)
143
- def execute
144
- world = WorldView.where(world_id: world_id).first_or_create
145
- world.village_views.map(&:name)
144
+ class VillageNamesQuery < Struct.new(:world_id)
145
+ def execute
146
+ world = WorldView.where(world_id: world_id).first_or_create
147
+ world.village_views.map(&:name)
148
+ end
146
149
  end
147
- end
148
150
 
149
- class PeopleNamesQuery < Struct.new(:world_id)
150
- def execute
151
- world_view = WorldView.where(world_id: world_id).first_or_create
152
- world_view.person_views.flat_map(&:person_name)
151
+ class PeopleNamesQuery < Struct.new(:world_id)
152
+ def execute
153
+ world_view = WorldView.where(world_id: world_id).first_or_create
154
+ world_view.person_views.flat_map(&:person_name)
155
+ end
153
156
  end
154
157
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metacosm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Weissman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-29 00:00:00.000000000 Z
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: passive_record