stenotype 0.1.0 → 0.1.1

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.
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stenotype
4
- #
5
- # A namespace for holding library-level exceptions.
6
- #
7
- module Exceptions
8
- #
9
- # This exception is being raised in case an unsupported mode
10
- # for Google Cloud is specified.
11
- #
12
- class GoogleCloudUnsupportedMode < StandardError; end
13
-
14
- #
15
- # This exception is being raised upon unsuccessful publishing of an event.
16
- #
17
- class MessageNotPublished < StandardError; end
18
-
19
- #
20
- # This exception is being raised in case no targets are
21
- # specified {Stenotype::Configuration}.
22
- #
23
- class NoTargetsSpecified < StandardError; end
24
-
25
- #
26
- # This exception is being raised upon using a context handler which
27
- # has never been registered in known handlers in {Stenotype::ContextHandlers::Collection}.
28
- #
29
- class UnknownHandler < StandardError; end
30
- end
31
- end
@@ -1,145 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Stenotype
4
- #
5
- # A namespace containing extensions of various frameworks.
6
- # For example Rails components could be extended
7
- #
8
- module Frameworks
9
- #
10
- # An extension for a plain Ruby class in order to track invocation of
11
- # instance methods.
12
- #
13
- module ObjectExt
14
- #
15
- # Class methods for `Object` to be extended by
16
- #
17
- ClassMethodsExtension = Class.new(Module)
18
- #
19
- # Instance methods to be included into `Object` ancestors chain
20
- #
21
- InstanceMethodsExtension = Class.new(Module)
22
-
23
- attr_reader :instance_mod,
24
- :class_mod
25
-
26
- # @!visibility private
27
- def self.included(klass)
28
- @instance_mod = InstanceMethodsExtension.new
29
- @class_mod = ClassMethodsExtension.new
30
-
31
- build_instance_methods
32
- build_class_methods
33
-
34
- klass.const_set(:InstanceProxy, Module.new)
35
- klass.const_set(:ClassProxy, Module.new)
36
-
37
- klass.send(:include, instance_mod)
38
- klass.extend(class_mod)
39
-
40
- super
41
- end
42
-
43
- #
44
- # @!method emit_event(data = {}, method: caller_locations.first.label, eval_context: nil)
45
- # A method injected into all instances of Object
46
- # @!scope instance
47
- # @param data {Hash} Data to be sent to the targets
48
- # @param method {String} An optional method name
49
- # @param eval_context {Hash} A hash linking object to context handler
50
- # @return {Stenotype::Event} An instance of emitted event
51
- #
52
-
53
- #
54
- # @!method emit_event_before(*methods)
55
- # A method injected into all instances of Object
56
- # @!scope instance
57
- # @param methods {Array<Symbol>} A list of method before which an event will be emitted
58
- #
59
-
60
- #
61
- # @!method emit_klass_event_before(*class_methods)
62
- # A class method injected into all subclasses of [Object]
63
- # @!scope class
64
- # @param class_method {Array<Symbol>} A list of class method before which
65
- # an event will be emitted
66
- #
67
-
68
- #
69
- # rubocop:disable Metrics/MethodLength
70
- # Adds two methods: [#emit_event] and [#emit_event_before] to every object
71
- # inherited from [Object]
72
- #
73
- def build_instance_methods
74
- instance_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
75
- def emit_event(data = {}, method: caller_locations.first.label, eval_context: nil)
76
- Stenotype::Event.emit!(
77
- {
78
- type: 'class_instance',
79
- **data,
80
- },
81
- options: {
82
- class: self.class.name,
83
- method: method.to_sym
84
- },
85
- eval_context: (eval_context || { klass: self })
86
- )
87
- end
88
-
89
- def emit_event_before(*methods)
90
- proxy = const_get(:InstanceProxy)
91
-
92
- methods.each do |method|
93
- proxy.module_eval do
94
- define_method(method) do |*args, **rest_args, &block|
95
- Stenotype::Event.emit!(
96
- { type: 'class_instance' },
97
- options: {
98
- class: self.class.name,
99
- method: __method__
100
- },
101
- eval_context: { klass: self }
102
- )
103
- super(*args, **rest_args, &block)
104
- end
105
- end
106
- end
107
-
108
- send(:prepend, proxy)
109
- end
110
- RUBY
111
- end
112
-
113
- #
114
- # Adds class method [#emit_klass_event_before] to every class
115
- # inherited from [Object]
116
- #
117
- def build_class_methods
118
- class_mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
119
- def emit_klass_event_before(*class_methods)
120
- proxy = const_get(:ClassProxy)
121
-
122
- class_methods.each do |method|
123
- proxy.module_eval do
124
- define_method(method) do |*args, **rest_args, &block|
125
- Stenotype::Event.emit!(
126
- { type: 'class' },
127
- options: {
128
- class: self.name,
129
- method: __method__
130
- },
131
- eval_context: { klass: self }
132
- )
133
- super(*args, **rest_args, &block)
134
- end
135
- end
136
-
137
- singleton_class.send(:prepend, proxy)
138
- end
139
- end
140
- RUBY
141
- end
142
- # rubocop:enable Metrics/MethodLength
143
- end
144
- end
145
- end