shamu 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (207) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +26 -0
  3. data/.gitignore +2 -1
  4. data/.rubocop.yml +89 -30
  5. data/.yardopts +4 -5
  6. data/Gemfile +24 -12
  7. data/Guardfile +5 -0
  8. data/LABELS.md +22 -0
  9. data/README.md +41 -0
  10. data/Rakefile +12 -0
  11. data/circle.yml +7 -3
  12. data/config.ru +7 -0
  13. data/lib/shamu/active_record.rb +7 -0
  14. data/lib/shamu/attributes/assignment.rb +114 -0
  15. data/lib/shamu/attributes/equality.rb +40 -0
  16. data/lib/shamu/attributes/fluid_assignment.rb +49 -0
  17. data/lib/shamu/attributes/validation.rb +74 -0
  18. data/lib/shamu/attributes.rb +255 -0
  19. data/lib/shamu/auditing/README.md +0 -0
  20. data/lib/shamu/auditing/audit_record.rb +32 -0
  21. data/lib/shamu/auditing/auditing_service.rb +32 -0
  22. data/lib/shamu/auditing/list_scope.rb +22 -0
  23. data/lib/shamu/auditing/logging_auditing_service.rb +16 -0
  24. data/lib/shamu/auditing/support.rb +75 -0
  25. data/lib/shamu/auditing/transaction.rb +58 -0
  26. data/lib/shamu/auditing.rb +12 -0
  27. data/lib/shamu/entities/README.md +1 -0
  28. data/lib/shamu/entities/active_record.rb +123 -0
  29. data/lib/shamu/entities/active_record_soft_destroy.rb +91 -0
  30. data/lib/shamu/entities/entity.rb +196 -0
  31. data/lib/shamu/entities/entity_path.rb +87 -0
  32. data/lib/shamu/entities/identity_cache.rb +64 -0
  33. data/lib/shamu/entities/list.rb +54 -0
  34. data/lib/shamu/entities/list_scope/dates.rb +57 -0
  35. data/lib/shamu/entities/list_scope/paging.rb +51 -0
  36. data/lib/shamu/entities/list_scope/scoped_paging.rb +65 -0
  37. data/lib/shamu/entities/list_scope/sorting.rb +76 -0
  38. data/lib/shamu/entities/list_scope.rb +105 -0
  39. data/lib/shamu/entities/null_entity.rb +88 -0
  40. data/lib/shamu/entities.rb +11 -0
  41. data/lib/shamu/error.rb +23 -5
  42. data/lib/shamu/events/README.md +0 -0
  43. data/lib/shamu/events/active_record/channel.rb +36 -0
  44. data/lib/shamu/events/active_record/message.rb +52 -0
  45. data/lib/shamu/events/active_record/migration.rb +49 -0
  46. data/lib/shamu/events/active_record/runner.rb +28 -0
  47. data/lib/shamu/events/active_record/service.rb +174 -0
  48. data/lib/shamu/events/active_record.rb +13 -0
  49. data/lib/shamu/events/channel_stats.rb +23 -0
  50. data/lib/shamu/events/error.rb +24 -0
  51. data/lib/shamu/events/events_service.rb +136 -0
  52. data/lib/shamu/events/in_memory/async_service.rb +48 -0
  53. data/lib/shamu/events/in_memory/service.rb +97 -0
  54. data/lib/shamu/events/in_memory.rb +10 -0
  55. data/lib/shamu/events/message.rb +38 -0
  56. data/lib/shamu/events/support.rb +60 -0
  57. data/lib/shamu/events.rb +12 -0
  58. data/lib/shamu/features/README.md +0 -0
  59. data/lib/shamu/features/conditions/condition.rb +39 -0
  60. data/lib/shamu/features/conditions/env.rb +37 -0
  61. data/lib/shamu/features/conditions/hosts.rb +25 -0
  62. data/lib/shamu/features/conditions/matching.rb +16 -0
  63. data/lib/shamu/features/conditions/not_matching.rb +16 -0
  64. data/lib/shamu/features/conditions/percentage.rb +44 -0
  65. data/lib/shamu/features/conditions/proc.rb +54 -0
  66. data/lib/shamu/features/conditions/roles.rb +23 -0
  67. data/lib/shamu/features/conditions/schedule_at.rb +27 -0
  68. data/lib/shamu/features/conditions.rb +18 -0
  69. data/lib/shamu/features/config_service.rb +10 -0
  70. data/lib/shamu/features/context.rb +80 -0
  71. data/lib/shamu/features/env_store.rb +88 -0
  72. data/lib/shamu/features/errors.rb +29 -0
  73. data/lib/shamu/features/features_service.rb +168 -0
  74. data/lib/shamu/features/list_scope.rb +30 -0
  75. data/lib/shamu/features/selector.rb +50 -0
  76. data/lib/shamu/features/support.rb +51 -0
  77. data/lib/shamu/features/toggle.rb +149 -0
  78. data/lib/shamu/features/toggle_codec.rb +69 -0
  79. data/lib/shamu/features.rb +16 -0
  80. data/lib/shamu/locale/en.yml +22 -2
  81. data/lib/shamu/logger.rb +13 -0
  82. data/lib/shamu/rack/README.md +0 -0
  83. data/lib/shamu/rack/cookies.rb +115 -0
  84. data/lib/shamu/rack/cookies_middleware.rb +26 -0
  85. data/lib/shamu/rack/query_params.rb +41 -0
  86. data/lib/shamu/rack/query_params_middleware.rb +24 -0
  87. data/lib/shamu/rack.rb +12 -0
  88. data/lib/shamu/rails/controller.rb +131 -0
  89. data/lib/shamu/rails/entity.rb +168 -0
  90. data/lib/shamu/rails/features.rb +13 -0
  91. data/lib/shamu/rails/railtie.rb +30 -0
  92. data/lib/shamu/rails.rb +10 -0
  93. data/lib/shamu/rspec/matchers.rb +44 -0
  94. data/lib/shamu/rspec.rb +1 -0
  95. data/lib/shamu/security/README.md +0 -0
  96. data/lib/shamu/security/active_record_policy.rb +106 -0
  97. data/lib/shamu/security/error.rb +65 -0
  98. data/lib/shamu/security/hashed_value.rb +71 -0
  99. data/lib/shamu/security/no_policy.rb +15 -0
  100. data/lib/shamu/security/policy.rb +289 -0
  101. data/lib/shamu/security/policy_refinement.rb +50 -0
  102. data/lib/shamu/security/policy_rule.rb +59 -0
  103. data/lib/shamu/security/principal.rb +72 -0
  104. data/lib/shamu/security/roles.rb +62 -0
  105. data/lib/shamu/security/roles_service.rb +30 -0
  106. data/lib/shamu/security/support.rb +83 -0
  107. data/lib/shamu/security.rb +43 -0
  108. data/lib/shamu/services/README.md +2 -0
  109. data/lib/shamu/services/active_record.rb +58 -0
  110. data/lib/shamu/services/active_record_crud.rb +378 -0
  111. data/lib/shamu/services/error.rb +24 -0
  112. data/lib/shamu/services/lazy_association.rb +31 -0
  113. data/lib/shamu/services/lazy_transform.rb +97 -0
  114. data/lib/shamu/services/request.rb +122 -0
  115. data/lib/shamu/services/request_support.rb +124 -0
  116. data/lib/shamu/services/result.rb +75 -0
  117. data/lib/shamu/services/service.rb +355 -0
  118. data/lib/shamu/services.rb +12 -0
  119. data/lib/shamu/sessions/README.md +2 -0
  120. data/lib/shamu/sessions/cookie_store.rb +79 -0
  121. data/lib/shamu/sessions/session_store.rb +42 -0
  122. data/lib/shamu/sessions.rb +8 -0
  123. data/lib/shamu/to_bool_extension.rb +57 -0
  124. data/lib/shamu/to_model_id_extension.rb +50 -0
  125. data/lib/shamu/version.rb +10 -4
  126. data/lib/shamu.rb +18 -6
  127. data/shamu.gemspec +21 -10
  128. data/spec/internal/README.md +4 -0
  129. data/spec/internal/config/database.yml +3 -0
  130. data/spec/internal/config/routes.rb +3 -0
  131. data/spec/internal/db/schema.rb +3 -0
  132. data/spec/internal/log/.gitignore +1 -0
  133. data/spec/internal/public/favicon.ico +0 -0
  134. data/spec/lib/shamu/active_record_support.rb +32 -0
  135. data/spec/lib/shamu/attributes/assignment_spec.rb +129 -0
  136. data/spec/lib/shamu/attributes/equality_spec.rb +63 -0
  137. data/spec/lib/shamu/attributes/fluid_assignment_spec.rb +31 -0
  138. data/spec/lib/shamu/attributes/validation_spec.rb +53 -0
  139. data/spec/lib/shamu/attributes_spec.rb +331 -0
  140. data/spec/lib/shamu/auditing/logging_auditing_service_spec.rb +18 -0
  141. data/spec/lib/shamu/auditing/support_spec.rb +41 -0
  142. data/spec/lib/shamu/entities/active_record_soft_destroy_spec.rb +82 -0
  143. data/spec/lib/shamu/entities/active_record_spec.rb +66 -0
  144. data/spec/lib/shamu/entities/entity_path_spec.rb +40 -0
  145. data/spec/lib/shamu/entities/entity_spec.rb +56 -0
  146. data/spec/lib/shamu/entities/identity_cache_spec.rb +69 -0
  147. data/spec/lib/shamu/entities/list_scope/dates_spec.rb +47 -0
  148. data/spec/lib/shamu/entities/list_scope/paging_spec.rb +41 -0
  149. data/spec/lib/shamu/entities/list_scope/scoped_paging_spec.rb +40 -0
  150. data/spec/lib/shamu/entities/list_scope/sorting_spec.rb +59 -0
  151. data/spec/lib/shamu/entities/list_scope_spec.rb +127 -0
  152. data/spec/lib/shamu/entities/list_spec.rb +60 -0
  153. data/spec/lib/shamu/entities/null_entity_spec.rb +94 -0
  154. data/spec/lib/shamu/events/active_record/migration_spec.rb +11 -0
  155. data/spec/lib/shamu/events/active_record/service_spec.rb +139 -0
  156. data/spec/lib/shamu/events/events_service_spec.rb +57 -0
  157. data/spec/lib/shamu/events/in_memory/async_service_spec.rb +37 -0
  158. data/spec/lib/shamu/events/in_memory/service_spec.rb +36 -0
  159. data/spec/lib/shamu/events/message_spec.rb +7 -0
  160. data/spec/lib/shamu/events/support_spec.rb +44 -0
  161. data/spec/lib/shamu/features/conditions/condition_spec.rb +8 -0
  162. data/spec/lib/shamu/features/conditions/env_spec.rb +29 -0
  163. data/spec/lib/shamu/features/conditions/hosts_spec.rb +21 -0
  164. data/spec/lib/shamu/features/conditions/matching_spec.rb +23 -0
  165. data/spec/lib/shamu/features/conditions/percentage_spec.rb +71 -0
  166. data/spec/lib/shamu/features/conditions/proc_spec.rb +28 -0
  167. data/spec/lib/shamu/features/env_store_spec.rb +48 -0
  168. data/spec/lib/shamu/features/features.yml +34 -0
  169. data/spec/lib/shamu/features/features_service_spec.rb +109 -0
  170. data/spec/lib/shamu/features/secondary.yml +5 -0
  171. data/spec/lib/shamu/features/selector_spec.rb +17 -0
  172. data/spec/lib/shamu/features/support_spec.rb +45 -0
  173. data/spec/lib/shamu/features/toggle_codec_spec.rb +28 -0
  174. data/spec/lib/shamu/features/toggle_spec.rb +42 -0
  175. data/spec/lib/shamu/rack/cookies_middleware_spec.rb +33 -0
  176. data/spec/lib/shamu/rack/cookies_spec.rb +43 -0
  177. data/spec/lib/shamu/rack/query_params_middleware_spec.rb +33 -0
  178. data/spec/lib/shamu/rack/query_params_spec.rb +23 -0
  179. data/spec/lib/shamu/rails/controller_spec.rb +74 -0
  180. data/spec/lib/shamu/rails/entity_spec.rb +150 -0
  181. data/spec/lib/shamu/rails/features.yml +13 -0
  182. data/spec/lib/shamu/rails/features_spec.rb +45 -0
  183. data/spec/lib/shamu/security/active_record_policy_spec.rb +38 -0
  184. data/spec/lib/shamu/security/hashed_value_spec.rb +41 -0
  185. data/spec/lib/shamu/security/policy_refinement_spec.rb +61 -0
  186. data/spec/lib/shamu/security/policy_rule_spec.rb +60 -0
  187. data/spec/lib/shamu/security/policy_spec.rb +158 -0
  188. data/spec/lib/shamu/security/roles_spec.rb +46 -0
  189. data/spec/lib/shamu/services/active_record_crud_spec.rb +460 -0
  190. data/spec/lib/shamu/services/active_record_spec.rb +92 -0
  191. data/spec/lib/shamu/services/lazy_association_spec.rb +31 -0
  192. data/spec/lib/shamu/services/lazy_transform_spec.rb +96 -0
  193. data/spec/lib/shamu/services/request_spec.rb +58 -0
  194. data/spec/lib/shamu/services/request_support_spec.rb +129 -0
  195. data/spec/lib/shamu/services/result_spec.rb +37 -0
  196. data/spec/lib/shamu/services/service_spec.rb +307 -0
  197. data/spec/lib/shamu/sessions/cookie_store_spec.rb +44 -0
  198. data/spec/lib/shamu/to_bool_extension_spec.rb +67 -0
  199. data/spec/lib/shamu/to_model_id_extension_spec.rb +54 -0
  200. data/spec/rails_helper.rb +13 -0
  201. data/spec/spec_helper.rb +17 -12
  202. data/spec/support/active_record.rb +17 -0
  203. data/spec/support/database.rb +14 -0
  204. data/spec/support/logger.rb +0 -0
  205. metadata +383 -9
  206. data/spec/lib/shamu_spec.rb +0 -5
  207. /data/{spec/internal/log/test.log → lib/shamu/attributes/README.md} +0 -0
@@ -0,0 +1,24 @@
1
+ require "i18n"
2
+
3
+ module Shamu
4
+
5
+ module Events
6
+ # A generic error class for problems with shamu services.
7
+ class Error < Shamu::Error
8
+ private
9
+
10
+ def translation_scope
11
+ super.dup.insert( 1, :events )
12
+ end
13
+ end
14
+
15
+ # A an event runner did not provide a valid runner_id.
16
+ class UnknownRunnnerError < Error
17
+
18
+ def initialize( message = :unknown_runner )
19
+ super
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,136 @@
1
+ require "multi_json"
2
+
3
+ module Shamu
4
+ module Events
5
+
6
+ # The {EventsService} handles receiving messages ({#publish}) and
7
+ # dispatching them to all registered subscribers ({#subscriber}). The actual
8
+ # delivery and message transport is defined by the concrete implementations
9
+ # of the {EventService}. See "Included Event Systems" below.
10
+ #
11
+ # Use `.` or `/` to namespace and group channels. Channels are not related
12
+ # to each other but namespacing can help organize and group channels in
13
+ # reports and back-end tools.
14
+ #
15
+ # > Events are not guaranteed to be delivered and may be delivered more than
16
+ # > once. Event processing should be idempotent and resilient to message
17
+ # > loss.
18
+ #
19
+ # ## Included Event Systems
20
+ #
21
+ # - {InMemory In Memory} intended for decoupling services all running
22
+ # withing the same process. This is the default.
23
+ # - {ActiveRecord} for low volume high-latency communications in a smaller
24
+ # system.
25
+ #
26
+ # ## Selecting an Event System
27
+ #
28
+ # Shamu relies on {https://github.com/phallguy/scorpion Scorpion} to resolve
29
+ # dependencies. To select which event system to use, prepare the scorpion
30
+ # with specific hunting instructions.
31
+ #
32
+ # ```
33
+ # Scorpion.prepare do
34
+ # capture Shamu::Events::ActiveRecord::Service
35
+ # end
36
+ # ```
37
+ class EventsService < Services::Service
38
+
39
+ # Prepare the default event service implementation to use. The default
40
+ # event service can be overridden when setting up the scorpion.
41
+ #
42
+ # @example
43
+ # Scorpion.prepare do
44
+ # capture Shamu::Events::EventsService do |scorpion, *args|
45
+ # scorpion.fetch Shamu::Events::InMemory::AsyncService, *args
46
+ # end
47
+ # end
48
+ #
49
+ # @return [EventsService]
50
+ def self.create( scorpion, *args, &block )
51
+ @events_service ||= scorpion.fetch InMemory::Service
52
+ end
53
+
54
+ # Publish a well-defined {Message} to a known channel so that any client
55
+ # that has {#subscribe subscribed} will receive a copy of the message to
56
+ # process.
57
+ #
58
+ # Events are delivered asynchronously. There is no guarantee that a
59
+ # subscriber has received or processed a message.
60
+ #
61
+ # @param [String] channel to publish to.
62
+ # @param [Message] message to publish.
63
+ # @return [void]
64
+ def publish( channel, message )
65
+ fail NotImplementedError
66
+ end
67
+
68
+ # Subscribe to receive notifications of events on the named channel. Any
69
+ # time a publisher pushes a message `callback` will be invoked with a copy
70
+ # of the message.
71
+ #
72
+ # @param [String] channel to listen to.
73
+ # @yield (message)
74
+ # @yieldparam [Message] message
75
+ # @return [void]
76
+ def subscribe( channel, &callback )
77
+ fail NotImplementedError
78
+ end
79
+
80
+ # Subscribe to the given channels from one service and forward them to
81
+ # another service.
82
+ #
83
+ # @param [EventsService] from the service to subscribe to.
84
+ # @param [EventsService] to the service to forward to.
85
+ # @param [Array<String>] the channels to forwar.
86
+ # @return [void]
87
+ def self.bridge( from, to, *channels )
88
+ Array( channels ).each do |channel|
89
+ from.subscribe( channel ) do |message|
90
+ to.publish channel, message
91
+ end
92
+ end
93
+ end
94
+
95
+ private
96
+
97
+ # @!visibility public
98
+ #
99
+ # Serialize a message so that it can be transfered from publisher to
100
+ # subsriber.
101
+ #
102
+ # @param [Message] message to serializer.
103
+ # @return [String] the serialized message.
104
+ def serialize( message )
105
+ MultiJson.dump \
106
+ class: message.class.name,
107
+ attributes: message.to_attributes
108
+ end
109
+
110
+ # @!visibility public
111
+ #
112
+ # Deserialize a message back to a {Message} instance.
113
+ #
114
+ # @param [String] raw data.
115
+ # @return [Message] the deserialized message.
116
+ def deserialize( raw )
117
+ hash = MultiJson.load( raw )
118
+ message_class = hash["class"].constantize
119
+ scorpion.fetch message_class, hash["attributes"], {}
120
+ end
121
+
122
+ def fetch_channel( name )
123
+ channels[name] || begin
124
+ mutex.synchronize do
125
+ channels[ name ] ||= create_channel( name )
126
+ end
127
+ end
128
+ end
129
+
130
+ def create_channel( name )
131
+ fail NotImplementedError, "Implement `def create_channel( name )` in #{ self.class.name }"
132
+ end
133
+
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,48 @@
1
+ require "thread"
2
+ require "thwait"
3
+
4
+ module Shamu
5
+ module Events
6
+ module InMemory
7
+
8
+ # An asynchronous version of {Service}. Event subscribers should be able
9
+ # to handle events coming in on a separate thread.
10
+ class AsyncService < InMemory::Service
11
+
12
+ initialize do
13
+ ObjectSpace.define_finalizer self do
14
+ threads = mutex.synchronize do
15
+ channels.map do |_, state|
16
+ state[:queue].close
17
+ state[:thread]
18
+ end
19
+ end
20
+
21
+ ThreadsWait.all_waits( *threads )
22
+ end
23
+ end
24
+
25
+ # (see Service#dispatch)
26
+ def dispatch
27
+ # No-op since messages are immediately dispatched on background threads.
28
+ end
29
+
30
+ private
31
+
32
+ def create_channel( _ )
33
+ state = super
34
+ state[:thread] = channel_thread( state )
35
+ state[:queue] = Queue.new
36
+ state
37
+ end
38
+
39
+ def channel_thread( state )
40
+ Thread.new do
41
+ dispatch_channel( state )
42
+ end
43
+ end
44
+
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,97 @@
1
+ require "thread"
2
+ require "thwait"
3
+
4
+ module Shamu
5
+ module Events
6
+ module InMemory
7
+
8
+ # Provides an in-memory {EventsService} that dispatches {Message messages}
9
+ # to subscribers within the same process.
10
+ #
11
+ # Messages are volitale and will be lost if the process crashes.
12
+ class Service < EventsService
13
+ include ChannelStats
14
+
15
+ initialize do
16
+ @mutex = Thread::Mutex.new
17
+ @channels = {}
18
+ end
19
+
20
+ # (see EventsService#publish)
21
+ def publish( channel, message )
22
+ state = fetch_channel( channel )
23
+ queue = state[ :queue ]
24
+ queue.push serialize( message )
25
+ end
26
+
27
+ # (see EventsService#subscribe)
28
+ def subscribe( channel, &callback )
29
+ subscribers = fetch_channel( channel )[ :subscribers ]
30
+ mutex.synchronize do
31
+ subscribers << callback
32
+ end
33
+ end
34
+
35
+ # Dispatch all pending mssages in the given named channels.
36
+ # @param [Array<String>] names of the channels to dispatch. Dispatches
37
+ # to all queues if empty.
38
+ # @return [void]
39
+ def dispatch( *names )
40
+ names = channels.keys if names.empty?
41
+
42
+ names.each do |name|
43
+ dispatch_channel( fetch_channel( name ) )
44
+ end
45
+ end
46
+
47
+ # (see ChannelStats#chanel_stats)
48
+ def channel_stats( name )
49
+ channel = fetch_channel( name )
50
+
51
+ {
52
+ name: name,
53
+ subscribers_count: channel[ :subscribers ].count,
54
+ queue_size: channel[ :queue ].size,
55
+ dispatching: channel[ :dispatching ]
56
+ }
57
+ end
58
+
59
+ private
60
+
61
+ attr_reader :channels
62
+ attr_reader :mutex
63
+
64
+ def create_channel( _ )
65
+ {
66
+ queue: [],
67
+ subscribers: [],
68
+ }
69
+ end
70
+
71
+ def dispatch_channel( state )
72
+ mutex.synchronize do
73
+ return if state[:dispatching]
74
+ state[ :dispatching ] = true
75
+ end
76
+
77
+ dispatch_messages( state )
78
+
79
+ ensure
80
+ mutex.synchronize do
81
+ state[ :dispatching ] = false
82
+ end
83
+ end
84
+
85
+ def dispatch_messages( state )
86
+ while raw_message = state[:queue].shift
87
+ message = deserialize( raw_message )
88
+ state[ :subscribers ].each do |subscriber|
89
+ subscriber.call( message )
90
+ end
91
+ end
92
+ end
93
+
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,10 @@
1
+ module Shamu
2
+ module Events
3
+
4
+ # See {Service}.
5
+ module InMemory
6
+ require "shamu/events/in_memory/service"
7
+ require "shamu/events/in_memory/async_service"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,38 @@
1
+ module Shamu
2
+ module Events
3
+
4
+ # The event {Message} consists of a set of well-defined attributes
5
+ # describing a single discrete event in the system and associated meta-data
6
+ # needed by subscribers to process the message.
7
+ #
8
+ # Messages should attempt to forward the state available at the time the
9
+ # event was published so that subscribers can process the message without
10
+ # contacting additional services.
11
+ #
12
+ # Event messages are serialized for network or IPC transimission and must
13
+ # limit attributes to primitive types (Strings, Numbers, Arrays and Hashes)
14
+ # so they can be round tripped with `Message.new( message.to_attributes )`.
15
+ class Message
16
+ include Shamu::Attributes
17
+ include Shamu::Attributes::Equality
18
+
19
+ # ============================================================================
20
+ # @!group Attributes
21
+ #
22
+
23
+ # @!attribute
24
+ # @return [String] the ID for the message. Automatically generated UUID
25
+ attribute :id
26
+
27
+ #
28
+ # @!endgroup Attributes
29
+
30
+
31
+ def initialize( * )
32
+ super
33
+
34
+ @id ||= SecureRandom.uuid
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,60 @@
1
+ module Shamu
2
+ module Events
3
+
4
+ # Add event dispatching support to a {Services::Service}
5
+ module Support
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+
10
+ # ============================================================================
11
+ # @!group Dependencies
12
+ #
13
+
14
+ # @!attribute
15
+ # @return [Events::EventsService] the events service to publish messages
16
+ # to.
17
+ attr_dependency :events_service, Events::EventsService
18
+
19
+ #
20
+ # @!endgroup Dependencies
21
+
22
+ end
23
+
24
+ private
25
+
26
+ # @!visibility public
27
+ #
28
+ # Publish the given `message` to the {#events_service}.
29
+ #
30
+ # @param [Events::Message] message the custom event specific message to
31
+ # publish.
32
+ # @param [String] channel to publish to.
33
+ # @return [void]
34
+ def event!( message, channel: event_channel )
35
+ events_service.publish channel, message
36
+ end
37
+
38
+ # @!visibility public
39
+ #
40
+ # The channel to {#publish_event publish events} to. Defaults to the
41
+ # transformed name of the service class.
42
+ #
43
+ # Users::UsersService => users
44
+ # Users::ProfileService => users/profile
45
+ # Users::Profiles::ProfilesService => users/profiles
46
+ #
47
+ # @return [String] the name of the channel.
48
+ def event_channel
49
+ @event_channel ||= begin
50
+ base_name = self.class.name || "Events"
51
+ parts = base_name.split( "::" )
52
+ parts[-1].sub!( /Service$/, "" )
53
+ parts.pop if parts[-1] == parts[-2] || ( parts.length > 1 && parts[-1].blank? )
54
+ parts.join( "/" ).underscore
55
+ end
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,12 @@
1
+ module Shamu
2
+ # {include:file:lib/shamu/events/README.md}
3
+ module Security
4
+ require "shamu/events/error"
5
+ require "shamu/events/message"
6
+ require "shamu/events/events_service"
7
+ require "shamu/events/channel_stats"
8
+ require "shamu/events/support"
9
+
10
+ require "shamu/events/in_memory"
11
+ end
12
+ end
File without changes
@@ -0,0 +1,39 @@
1
+ module Shamu
2
+ module Features
3
+ module Conditions
4
+
5
+ # A condition that must be met for a {Selector} to match and enable a
6
+ # {Toggle}.
7
+ class Condition
8
+
9
+ # @param [String] name of the condition.
10
+ # @param [Object] config settings for the condition.
11
+ def self.create( name, config, toggle )
12
+ @condition_class ||= Hash.new do |hash, key|
13
+ hash[key] = "Shamu::Features::Conditions::#{ key.to_s.camelize }".constantize
14
+ end
15
+
16
+ @condition_class[name].new config, toggle
17
+ end
18
+
19
+ # @param [Object] config options selected for the condition.
20
+ def initialize( config, toggle )
21
+ @config = config
22
+ @toggle = toggle
23
+ end
24
+
25
+ # @param [Context] context the feature evaluation context.
26
+ # @return [Boolean] true if the condition matches the given environment.
27
+ def match?( context )
28
+ fail NotImplementedError
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :config
34
+ attr_reader :toggle
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,37 @@
1
+ module Shamu
2
+ module Features
3
+ module Conditions
4
+
5
+ # Match against an environment variable.
6
+ class Env < Conditions::Condition
7
+
8
+ # (see Condition#match?)
9
+ def match?( context )
10
+ variables.any? { |name, matcher| matcher.call( context.env( name ) ) }
11
+ end
12
+
13
+ private
14
+
15
+ def variables
16
+ @variables ||= hash_variables || array_variables
17
+ end
18
+
19
+ def hash_variables
20
+ return unless config.is_a?( Hash )
21
+
22
+ config.each_with_object( {} ) do |(name, value), hash|
23
+ hash[name] = ->(v) { v == value }
24
+ end
25
+ end
26
+
27
+ def array_variables
28
+ Array( config ).each_with_object( {} ) do |name, hash|
29
+ hash[name] = ->(v) { v.to_bool }
30
+ end
31
+ end
32
+
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ module Shamu
2
+ module Features
3
+ module Conditions
4
+
5
+ # Match against the current host machine's name.
6
+ class Hosts < Conditions::Condition
7
+
8
+ # (see Condition#match?)
9
+ def match?( context )
10
+ hosts.any? { |h| h.match( context.host ) }
11
+ end
12
+
13
+ private
14
+
15
+ def hosts
16
+ @hosts ||= Array( config ).map do |entry|
17
+ Regexp.new( entry, true )
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,16 @@
1
+ module Shamu
2
+ module Features
3
+ module Conditions
4
+
5
+ # Match if another feature is also enabled.
6
+ class Matching < Conditions::Condition
7
+
8
+ # (see Condition#match?)
9
+ def match?( context )
10
+ context.enabled?( config )
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module Shamu
2
+ module Features
3
+ module Conditions
4
+
5
+ # Match if another feature is also enabled.
6
+ class NotMatching < Conditions::Matching
7
+
8
+ # (see Condition#match?)
9
+ def match?( context )
10
+ !super
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ require "crc32"
2
+
3
+ module Shamu
4
+ module Features
5
+ module Conditions
6
+
7
+ # Match against a limited percentage of total users.
8
+ class Percentage < Conditions::Condition
9
+
10
+ # (see Condition#match?)
11
+ def match?( context )
12
+ if context.user_id
13
+ ( user_id_hash( context.user_id ) ^ toggle_crc ) % 100 < percentage
14
+ else
15
+ context.sticky!
16
+ Random.rand( 100 ) < percentage
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def percentage
23
+ @percentage ||= [ config.to_i, 100 ].min
24
+ end
25
+
26
+ def user_id_hash( user_id )
27
+ if user_id.is_a?( Numeric )
28
+ return user_id
29
+ else
30
+ return user_id.sub( "-", "" ).to_i( 16 )
31
+ end
32
+ end
33
+
34
+ def toggle_crc
35
+ # Use the name of the toggle to provide consistent semi-random noise
36
+ # into the user selection process.
37
+ @toggle_crc ||= Crc32.calculate( toggle.name, toggle.name.length, 0 )
38
+ end
39
+
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,54 @@
1
+ module Shamu
2
+ module Features
3
+ module Conditions
4
+
5
+ # Match against a custom method. Due to their dynamic nature, proc
6
+ # conditions are much slower and should be reserved for only a few
7
+ # features.
8
+ #
9
+ # The proc is specified in the configuration by class and method name like
10
+ #
11
+ # ```yaml
12
+ # # features.yml
13
+ # commerce:
14
+ # buy_now:
15
+ # select:
16
+ # - proc: Commerce::BuyNow#match?
17
+ # ```
18
+ #
19
+ # Shamu will instantiate a new instance of the `Commerce::BuyNow` class
20
+ # and invoke the `match?` method passing the current {Features::Context}.
21
+ #
22
+ # The custom proj will also have access to the current {Scorpion} if it
23
+ # includes the {Scorpion::Object} mixin.
24
+ class Proc < Conditions::Condition
25
+ include Scorpion::Object
26
+
27
+ # (see Condition#match?)
28
+ def match?( context )
29
+ instance( context ).send( proc_method, context, toggle )
30
+ end
31
+
32
+ private
33
+
34
+ def instance( context )
35
+ context.scorpion.fetch( proc_class )
36
+ end
37
+
38
+ def proc_class
39
+ @proc_class ||= proc_config.first.constantize
40
+ end
41
+
42
+ def proc_method
43
+ proc_config.last
44
+ end
45
+
46
+ def proc_config
47
+ @proc_config ||= config.split( "#" )
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,23 @@
1
+ module Shamu
2
+ module Features
3
+ module Conditions
4
+
5
+ # Match against the current user's roles.
6
+ class Roles < Conditions::Condition
7
+
8
+ # (see Condition#match?)
9
+ def match?( context )
10
+ ( context.roles && roles ).any?
11
+ end
12
+
13
+ private
14
+
15
+ def roles
16
+ @roles ||= Array( config )
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
23
+ end