evnt 3.1.4 → 3.2.5

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
  SHA256:
3
- metadata.gz: 4e0bb0645a93a6ed42052a22f21fa0e7e824deb91cbd5fd7e421243a0ced8a64
4
- data.tar.gz: ec4f5f8ce047a48e29439423d5dd21b8268206af8488d871199cec128020e047
3
+ metadata.gz: 31e331c91b6b97338b9583587d74b8a3da99c861b9ee46b584f7029185cd25db
4
+ data.tar.gz: ce5fadcd4669d194b13a2840ff2e5c311da73f6149c0c7c628cb0bfe6872a641
5
5
  SHA512:
6
- metadata.gz: 9df745c424d54e31d72f238c734807273894afbf901269fd915b5d1e5882e301a2810dce6ca6d1294ed0eac8b415714deabba46408892f7613d17518338f147b
7
- data.tar.gz: 37831f9c0159c3888944ed1a7677a39f52c57e10c0e30c179686b974d7e95754764cef49c23ae030e5b4c176f113fcc1204949389bcd2f0f4c092a605e2cf360
6
+ metadata.gz: 138fcfc99e148ac1665b0f317ab4f9efe53dae0ef220774defc067e7c7f8e2f79174d19ff81cf17509a4eb1a26169237751e9b8117335d98b094fbd7abcaa0a6
7
+ data.tar.gz: 944820d7276892bc245167d0340ed2098f0c5bb9d70cc3b3db3e231dfb0abb39288cfae3c96b7132c1cdbed59b9dec35d10ad61d3909b66508dcdb268abc6c36
@@ -119,7 +119,7 @@ module Evnt
119
119
  exceptions: false,
120
120
  nullify_empty_params: false
121
121
  }
122
- default_options = @_default_options || {}
122
+ default_options = _safe_default_options || {}
123
123
  params_options = params[:_options] || {}
124
124
  @options = initial_options.merge(default_options)
125
125
  .merge(params_options)
@@ -131,17 +131,16 @@ module Evnt
131
131
  # This function calls requested steps (callback) for the command.
132
132
  def _run_command_steps
133
133
  _validate_single_params
134
- _normalize_params if @state[:result] && defined?(_normalize_params)
135
- _validate_params if @state[:result] && defined?(_validate_params)
136
- _validate_logic if @state[:result] && defined?(_validate_logic)
137
- _initialize_events if @state[:result] && defined?(_initialize_events)
134
+ _safe_normalize_params if @state[:result]
135
+ _safe_validate_params if @state[:result]
136
+ _safe_validate_logic if @state[:result]
137
+ _safe_initialize_events if @state[:result]
138
138
  end
139
139
 
140
140
  # This function validates the single parameters sets with the "validates" method.
141
141
  def _validate_single_params
142
- return if self.class._validations.nil? || self.class._validations.empty?
143
-
144
- self.class._validations.each do |val|
142
+ validations = _safe_validations
143
+ validations.each do |val|
145
144
  value_to_validate = _value_to_validate(val)
146
145
  validator = Evnt::Validator.new(value_to_validate, val[:options])
147
146
 
@@ -163,24 +162,60 @@ module Evnt
163
162
  params[val[:param]]
164
163
  end
165
164
 
165
+ # Safe defined functions:
166
+
167
+ def _safe_default_options
168
+ return _default_options if defined?(_default_options)
169
+ {}
170
+ end
171
+
172
+ def _safe_validations
173
+ return _validations if defined?(_validations)
174
+ []
175
+ end
176
+
177
+ def _safe_normalize_params
178
+ return _normalize_params if defined?(_normalize_params)
179
+ nil
180
+ end
181
+
182
+ def _safe_validate_params
183
+ return _validate_params if defined?(_validate_params)
184
+ nil
185
+ end
186
+
187
+ def _safe_validate_logic
188
+ return _validate_logic if defined?(_validate_logic)
189
+ nil
190
+ end
191
+
192
+ def _safe_initialize_events
193
+ return _initialize_events if defined?(_initialize_events)
194
+ nil
195
+ end
196
+
166
197
  # Class functions:
167
198
  ############################################################################
168
199
 
169
200
  # This class contain the list of settings for the command.
170
201
  class << self
171
202
 
172
- attr_accessor :_default_options, :_validations
173
-
174
203
  # This function sets the default options that should be used by the command.
175
204
  def default_options(options)
176
- instance_variable_set(:@_default_options, options)
205
+ @options ||= {}
206
+ @options.merge!(options)
207
+ command_options = @options
208
+
209
+ define_method('_default_options', -> { return command_options })
177
210
  end
178
211
 
179
212
  # This function sets the single validation request for a command parameter.
180
213
  def validates(param, options)
181
- validations = instance_variable_get(:@_validations) || []
182
- validations.push(param: param, options: options)
183
- instance_variable_set(:@_validations, validations)
214
+ @validations ||= []
215
+ @validations.push(param: param, options: options)
216
+ command_validations = @validations
217
+
218
+ define_method('_validations', -> { return command_validations })
184
219
  end
185
220
 
186
221
  # This function sets the normalize params function for the command.
@@ -81,7 +81,7 @@ module Evnt
81
81
  initial_options = {
82
82
  silent: false
83
83
  }
84
- default_options = @_default_options || {}
84
+ default_options = _safe_default_options || {}
85
85
  params_options = params[:_options] || {}
86
86
  @options = initial_options.merge(default_options)
87
87
  .merge(params_options)
@@ -96,8 +96,8 @@ module Evnt
96
96
  extras.each { |k, v| @extras[k[1..-1].to_sym] = v }
97
97
 
98
98
  # set other datas
99
- @name = self.class._name
100
- @attributes = self.class._attributes
99
+ @name = _safe_name
100
+ @attributes = _safe_attributes
101
101
  end
102
102
 
103
103
  # This function generates the complete event payload.
@@ -105,7 +105,7 @@ module Evnt
105
105
  # add evnt informations
106
106
  params[:evnt] = {
107
107
  timestamp: Time.now.to_i,
108
- name: self.class._name
108
+ name: @name
109
109
  }
110
110
  # return payload
111
111
  params
@@ -113,55 +113,90 @@ module Evnt
113
113
 
114
114
  # This function validates all payload and check they are completed.
115
115
  def _validate_payload
116
- return unless self.class._attributes
117
-
118
116
  # check all attributes are present
119
- check_attr = (@payload.keys - [:evnt]) == self.class._attributes
117
+ check_attr = (@payload.keys - [:evnt]) == @attributes
120
118
  raise 'Event parameters are not correct' unless check_attr
121
119
  end
122
120
 
123
121
  # This function calls requested steps for the event.
124
122
  def _run_event_steps
125
- _write_event if defined?(_write_event)
123
+ _safe_write_event
126
124
  end
127
125
 
128
126
  # This function notify all handlers for the event.
129
127
  def _notify_handlers
130
- return unless self.class._handlers
131
128
  return if @options[:silent]
129
+ handlers = _safe_handlers
132
130
 
133
131
  # notify every handler
134
- self.class._handlers.each do |handler|
132
+ handlers.each do |handler|
135
133
  handler.notify(self)
136
134
  end
137
135
  end
138
136
 
137
+ # Safe defined functions:
138
+
139
+ def _safe_default_options
140
+ return _default_options if defined?(_default_options)
141
+ {}
142
+ end
143
+
144
+ def _safe_name
145
+ return _name if defined?(_name)
146
+ ''
147
+ end
148
+
149
+ def _safe_attributes
150
+ return _attributes if defined?(_attributes)
151
+ []
152
+ end
153
+
154
+ def _safe_handlers
155
+ return _handlers if defined?(_handlers)
156
+ []
157
+ end
158
+
159
+ def _safe_write_event
160
+ return _write_event if defined?(_write_event)
161
+ nil
162
+ end
163
+
139
164
  # Class functions:
140
165
  ############################################################################
141
166
 
142
167
  # This class contain the list of settings for the event.
143
168
  class << self
144
169
 
145
- attr_accessor :_default_options, :_name, :_attributes, :_handlers
146
-
147
170
  # This function sets the default options that should be used by the event.
148
171
  def default_options(options)
149
- instance_variable_set(:@_default_options, options)
172
+ @options ||= {}
173
+ @options.merge!(options)
174
+ event_options = @options
175
+
176
+ define_method('_default_options', -> { return event_options })
150
177
  end
151
178
 
152
179
  # This function sets the name for the event.
153
- def name_is(event_name)
154
- instance_variable_set(:@_name, event_name)
180
+ def name_is(name)
181
+ define_method('_name', -> { return name })
155
182
  end
156
183
 
157
184
  # This function sets the list of attributes for the event.
158
185
  def attributes_are(*attributes)
159
- instance_variable_set(:@_attributes, attributes)
186
+ @attributes ||= []
187
+ @attributes.concat(attributes).uniq!
188
+ event_attributes = @attributes
189
+
190
+ define_method('_attributes', -> { return event_attributes })
160
191
  end
161
192
 
162
193
  # This function sets the list of handlers for the event.
163
194
  def handlers_are(handlers)
164
- instance_variable_set(:@_handlers, handlers)
195
+ @handlers ||= []
196
+ @handlers.concat(handlers)
197
+ event_handlers = @handlers
198
+
199
+ define_method('_handlers', -> { return event_handlers })
165
200
  end
166
201
 
167
202
  # This function sets the write event function for the event.
@@ -36,20 +36,37 @@ module Evnt
36
36
 
37
37
  # This function calls requested steps for the handler.
38
38
  def _run_handler_steps
39
- update_queries = "#{@event.name}_update_queries"
40
- send(update_queries) if respond_to? update_queries
39
+ _safe_update_queries(@event.name)
41
40
 
42
41
  if event.reloaded?
43
42
  # manage event reloaded
44
- manage_reloaded_event = "#{@event.name}_manage_reloaded_event"
45
- send(manage_reloaded_event) if respond_to? manage_reloaded_event
43
+ _safe_manage_reloaded_event(@event.name)
46
44
  else
47
45
  # manage normal event
48
- manage_event = "#{@event.name}_manage_event"
49
- send(manage_event) if respond_to? manage_event
46
+ _safe_manage_event(@event.name)
50
47
  end
51
48
  end
52
49
 
50
+ # Safe defined functions:
51
+
52
+ def _safe_update_queries(event)
53
+ update_queries = "#{event}_update_queries"
54
+ return send(update_queries) if respond_to? update_queries
55
+ nil
56
+ end
57
+
58
+ def _safe_manage_reloaded_event(event)
59
+ manage_reloaded_event = "#{event}_manage_reloaded_event"
60
+ return send(manage_reloaded_event) if respond_to? manage_reloaded_event
61
+ nil
62
+ end
63
+
64
+ def _safe_manage_event(event)
65
+ manage_event = "#{event}_manage_event"
66
+ return send(manage_event) if respond_to? manage_event
67
+ nil
68
+ end
69
+
53
70
  # Class functions:
54
71
  ############################################################################
55
72
 
@@ -3,6 +3,6 @@
3
3
  # Evnt.
4
4
  module Evnt
5
5
 
6
- VERSION = '3.1.4'
6
+ VERSION = '3.2.5'
7
7
 
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: evnt
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.4
4
+ version: 3.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ideonetwork