evt-entity_projection 0.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39536db3a78c2916a9811527e360a5f148367bb5
4
+ data.tar.gz: 8cb4baf8f48e0f2cc53ac1ed00ce87611d1e89c6
5
+ SHA512:
6
+ metadata.gz: 9a075b12f7826133d817a6065d06fdb4fd0e16dc67c8372494fadd975aaea29a5c04f7425fd351e27d03133ba80a99c4ad41029637e6f8cf1926c9e4956dfebc
7
+ data.tar.gz: ca7048d95bed2e41b6296287372777d67846a234c15b0b1f4eb0c756f15d03b020607b76ec07ac3fd128e30c0baaf9d83bafd9d8850c3bdb60c07ae86a732998
@@ -0,0 +1,4 @@
1
+ require 'messaging'
2
+
3
+ require 'entity_projection/log'
4
+ require 'entity_projection/entity_projection'
@@ -0,0 +1,6 @@
1
+ require 'messaging/controls'
2
+
3
+ require 'entity_projection/controls/event_data'
4
+ require 'entity_projection/controls/message'
5
+ require 'entity_projection/controls/entity'
6
+ require 'entity_projection/controls/projection'
@@ -0,0 +1,22 @@
1
+ module EntityProjection
2
+ module Controls
3
+ module Entity
4
+ def self.example
5
+ SomeEntity.build
6
+ end
7
+
8
+ class SomeEntity
9
+ include Schema::DataStructure
10
+
11
+ attribute :some_attribute
12
+ attribute :other_attribute
13
+ end
14
+
15
+ module New
16
+ def self.example
17
+ Entity::SomeEntity.new
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module EntityProjection
2
+ module Controls
3
+ EventData = Messaging::Controls::EventData
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module EntityProjection
2
+ module Controls
3
+ Message = Messaging::Controls::Message
4
+ end
5
+ end
@@ -0,0 +1,49 @@
1
+ module EntityProjection
2
+ module Controls
3
+ module Projection
4
+ class Example
5
+ include EntityProjection
6
+ include Controls::Message
7
+
8
+ entity_name :some_entity
9
+
10
+ apply SomeMessage do |some_message|
11
+ some_entity.some_attribute = some_message.some_attribute
12
+ end
13
+ end
14
+
15
+ module ApplyMethod
16
+ class Example
17
+ include EntityProjection
18
+
19
+ def apply(event_data)
20
+ entity.some_attribute = event_data.data[:attribute]
21
+ end
22
+ end
23
+ end
24
+
25
+ module BlockAndApplyMethod
26
+ class Example
27
+ include EntityProjection
28
+ include Controls::Message
29
+
30
+ apply SomeMessage do |some_message|
31
+ entity.some_attribute = 'some attribute value set by apply block'
32
+ end
33
+
34
+ def apply(event_data)
35
+ event_data.data = 'some data value set by apply method'
36
+ end
37
+ end
38
+ end
39
+
40
+ module Anomaly
41
+ module NoApply
42
+ class Example
43
+ include EntityProjection
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,163 @@
1
+ module EntityProjection
2
+ def self.included(cls)
3
+ cls.class_exec do
4
+ include Log::Dependency
5
+
6
+ cls.extend Build
7
+ cls.extend Call
8
+ cls.extend Info
9
+ cls.extend ApplyMacro
10
+ cls.extend MessageRegistry
11
+ cls.extend EntityNameMacro
12
+
13
+ virtual :configure
14
+
15
+ initializer :entity
16
+ end
17
+ end
18
+
19
+ module Build
20
+ def build(entity)
21
+ instance = new(entity)
22
+ instance.configure
23
+ instance
24
+ end
25
+ end
26
+
27
+ module Info
28
+ extend self
29
+
30
+ def handler(message_or_event_data)
31
+ name = handler_name(message_or_event_data)
32
+
33
+ if method_defined?(name)
34
+ return name
35
+ else
36
+ return nil
37
+ end
38
+ end
39
+
40
+ def handles?(message_or_event_data)
41
+ method_defined? handler_name(message_or_event_data)
42
+ end
43
+
44
+ def handler_name(message_or_event_data)
45
+ name = nil
46
+
47
+ if message_or_event_data.is_a? EventSource::EventData::Read
48
+ name = Messaging::Message::Info.canonize_name(message_or_event_data.type)
49
+ else
50
+ name = message_or_event_data.message_name
51
+ end
52
+
53
+ "apply_#{name}"
54
+ end
55
+ end
56
+
57
+ module ApplyMacro
58
+ class Error < RuntimeError; end
59
+
60
+ def logger
61
+ @logger ||= Log.get(self)
62
+ end
63
+
64
+ def apply_macro(message_class, &blk)
65
+ define_apply_method(message_class, &blk)
66
+ message_registry.register(message_class)
67
+ end
68
+ alias :apply :apply_macro
69
+
70
+ def define_apply_method(message_class, &blk)
71
+ apply_method_name = handler_name(message_class)
72
+
73
+ if blk.nil?
74
+ error_msg = "Handler for #{message_class.name} is not correctly defined. It must have a block."
75
+ logger.error { error_msg }
76
+ raise Error, error_msg
77
+ end
78
+
79
+ send(:define_method, apply_method_name, &blk)
80
+
81
+ apply_method = instance_method(apply_method_name)
82
+
83
+ unless apply_method.arity == 1
84
+ error_msg = "Handler for #{message_class.name} is not correctly defined. It can only have a single parameter."
85
+ logger.error { error_msg }
86
+ raise Error, error_msg
87
+ end
88
+
89
+ apply_method_name
90
+ end
91
+ end
92
+
93
+ module Call
94
+ def call(entity, message_or_event_data)
95
+ instance = build(entity)
96
+ instance.(message_or_event_data)
97
+ end
98
+ end
99
+
100
+ module MessageRegistry
101
+ def message_registry
102
+ @message_registry ||= Messaging::MessageRegistry.new
103
+ end
104
+ end
105
+
106
+ module EntityNameMacro
107
+ def entity_name_macro(entity_name)
108
+ send(:define_method, entity_name) do
109
+ entity
110
+ end
111
+ end
112
+ alias :entity_name :entity_name_macro
113
+ end
114
+
115
+ def call(message_or_event_data)
116
+ if message_or_event_data.is_a? Messaging::Message
117
+ handle_message(message_or_event_data)
118
+ else
119
+ handle_event_data(message_or_event_data)
120
+ end
121
+ end
122
+
123
+ def handle_message(message)
124
+ logger.trace(tags: [:handle, :message]) { "Applying message (Message class: #{message.class.name})" }
125
+ logger.trace(tags: [:data, :message, :handle]) { message.pretty_inspect }
126
+
127
+ handler = self.class.handler(message)
128
+
129
+ unless handler.nil?
130
+ public_send(handler, message)
131
+ end
132
+
133
+ logger.info(tags: [:handle, :message]) { "Applied message (Message class: #{message.class.name})" }
134
+ logger.trace(tags: [:data, :message, :handle]) { message.pretty_inspect }
135
+
136
+ message
137
+ end
138
+
139
+ def handle_event_data(event_data)
140
+ logger.trace(tags: [:handle, :event_data]) { "Applying event data (Type: #{event_data.type})" }
141
+ logger.trace(tags: [:data, :event_data, :handle]) { event_data.pretty_inspect }
142
+
143
+ res = nil
144
+
145
+ handler = self.class.handler(event_data)
146
+
147
+ unless handler.nil?
148
+ message_name = Messaging::Message::Info.canonize_name(event_data.type)
149
+ message_class = self.class.message_registry.get(message_name)
150
+ res = Messaging::Message::Import.(event_data, message_class)
151
+ public_send(handler, res)
152
+ else
153
+ if respond_to?(:apply)
154
+ res = apply(event_data)
155
+ end
156
+ end
157
+
158
+ logger.info(tags: [:handle, :event_data]) { "Applied event data (Type: #{event_data.type})" }
159
+ logger.info(tags: [:data, :event_data, :handle]) { event_data.pretty_inspect }
160
+
161
+ res
162
+ end
163
+ end
@@ -0,0 +1,9 @@
1
+ module EntityProjection
2
+ class Log < ::Log
3
+ def tag!(tags)
4
+ tags << :entity_projection
5
+ tags << :library
6
+ tags << :verbose
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evt-entity_projection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - The Eventide Project
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: evt-messaging
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: ntl-test_bench
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: " "
42
+ email: opensource@eventide-project.org
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/entity_projection.rb
48
+ - lib/entity_projection/controls.rb
49
+ - lib/entity_projection/controls/entity.rb
50
+ - lib/entity_projection/controls/event_data.rb
51
+ - lib/entity_projection/controls/message.rb
52
+ - lib/entity_projection/controls/projection.rb
53
+ - lib/entity_projection/entity_projection.rb
54
+ - lib/entity_projection/log.rb
55
+ homepage: https://github.com/eventide-project/entity-projection
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 2.3.3
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.5.2
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Projects event data into an entity
79
+ test_files: []