Class: LanGrove::Plugin::Assessor

Inherits:
Base
  • Object
show all
Defined in:
lib/langrove/plugin/assessor.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#config_exception, #type

Constructor Details

#initialize(root, config, name = nil) ⇒ Assessor

Returns a new instance of Assessor.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/langrove/plugin/assessor.rb', line 179

def initialize( root, config, name = nil )
  
  super( root, config, name )
  
  @refference = @config[:ref]
  
  validate_config
  
  @provides = {
    
    :assessor => {
      
      :version => ASSESSOR_VERSION
      
    }
    
  }

end

Instance Attribute Details

#persistorObject (readonly)

Assessor has an internal Persistor used to store|fetch the previous ‘capsule’ contents.



37
38
39
# File 'lib/langrove/plugin/assessor.rb', line 37

def persistor
  @persistor
end

Class Method Details

.typeObject



26
27
28
29
30
# File 'lib/langrove/plugin/assessor.rb', line 26

def self.type
  
  :assessor
  
end

Instance Method Details

#assess(handler) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/langrove/plugin/assessor.rb', line 85

def assess( handler )

  #
  # The passed in handler will have the key attribute set.
  # 
  # Use the id from that key with the local persistors  
  # configured table to collect the 'previous' contents 
  # of the handlers capsule (as was passed through this
  # collator last time the capsule changed). 
  #
  # 
  begin 
    
    @logger.error( 
  
      "ERROR: Assessor encountered undefined #{handler.class}.key"
  
    )
    
    return
    
  end if handler.key.nil?
  
  key = handler.key
  key['table'] = @persistor.instance_variable_get(:@table)
  
  previous = Handler.new( key )
  
  #
  # Fetch previous 'capsule'
  #
  
  unless persistor.fetch( previous )
    
    @logger.info "Persistor did/could not fetch previous, make this fail the job..."
    
    return
    
  end

  each_change( previous.capsule, handler.capsule ) do |key, state, value, delta|
    
    #
    # Assemble the notification
    #
    notifiation = {
      
      :source => handler.unique,
      :system => @root.config.system,
      :application => @root.config.application,
      :key => key,
      :state => state,
      :value => value,
      :delta => delta,
      
    }
    
    notifiation[:ref] = @refference unless @refference.nil?
    
    #
    # Pass to the Handler for possible
    # filtration. 
    #
    handler.event_filter( :notification, notifiation ) do |notify|

      I.show notify
      
      #
      # Filtered. 
      # 
      # Pass to trigger.
      # 
      # Note: It is passed in the capsule of a 
      #       mini Handler, to stick to pattern.
      #
      @root.trigger( 
      
        Handler.new( nil, notifiation ), :handler, :notify
        
      )
      
    end
    
  end
  
  #
  # Save the current to previous
  #
  previous.capsule = handler.capsule
  persistor.store( previous )
  
end

#assess_handler(handler) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/langrove/plugin/assessor.rb', line 199

def assess_handler( handler )
  
  #
  # Option to trigger :handler_assess:
  #
  
  assess( handler )
  
  #
  # Option to trigger :handler_after_assess:
  #
  
end

#assess_present(old_val, new_val, key, &block) ⇒ Object

assess_present tests for presense of a key(s) between two hashes.

  • If on the first(old) but not the second(new)

    an :off notification is generated

  • If on both,

    a :repeat: notification is generated

  • If not on the first(old) but is on the second

    an :on notification is generated



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/langrove/plugin/assessor.rb', line 56

def assess_present( old_val, new_val, key, &block )
  
  return if @reported.has_key?( key )
  
  delta = nil
  
  begin
  
    delta = new_val - old_val
  
  rescue; end
  
  if old_val.nil? 
    
    yield key, :on, new_val, delta unless new_val.nil?
    
  else
    
    yield key, :repeat, new_val, delta unless new_val.nil?
    yield key, :off, new_val, delta if new_val.nil?
    
  end
  
  @reported[key] = 1
  
end

#each_change(old_hash, new_hash, &block) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/langrove/plugin/assessor.rb', line 252

def each_change( old_hash, new_hash, &block )
  
  @reported = {}
  
  @assess.each do |key_match, assess_type| 

    #
    # Search both hashes in tandem and yield the 
    # values found at the match key,
    #
    # If the value at the matching key is a new
    # hash, yield all subkeys from each in 
    # tandem
    # 
    tandem_recurse( old_hash, new_hash, key_match ) do |o, n, key|
      
      self.send( 
      
        "assess_#{assess_type}".to_sym, 
        o, n, key, &block 
        
      )
      
    end
    
  end
  
end

#tandem_recurse(oldh, newh, match, &block) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/langrove/plugin/assessor.rb', line 281

def tandem_recurse( oldh, newh, match, &block )
  
  if ( oldh.is_a?( Hash ) and oldh.has_key?( match ) ) or 
     ( newh.is_a?( Hash ) and newh.has_key?( match ) )
    
    o = oldh[match] unless oldh.nil?
    n = newh[match] unless newh.nil?
    
    unless o.is_a?( Hash ) or n.is_a?( Hash )
      
      yield o, n, match if block

      return

    end
    
    if n.is_a?( Hash )
      
      n.each do |key, value|
        
        oo = o[key] unless o.nil?
      
        yield oo, value, key if block
        
      end
      
    end

    if o.is_a?( Hash )
      
      o.each do |key, value|
        
        nn = n[key] unless n.nil?
      
        yield value, nn, key if block
        
      end
      
    end
    
  end
  
  unless newh.nil? or newh.size == 0
    
    newh.each do |key, value|
      
      if value.is_a?( Hash )
        
        o = nil
        o ||= oldh[key]
        
        tandem_recurse( o, value, match, &block )
        
      end
      
    end
    
  end
  
  unless oldh.nil? or oldh.size == 0
    
    oldh.each do |key, value|
      
      if value.is_a?( Hash )
        
        n = nil
        n ||= newh[key]
        
        tandem_recurse( value, n, match, &block )
        
      end
      
    end
    
  end
  
end

#validate_configObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/langrove/plugin/assessor.rb', line 214

def validate_config
  
  config_exception( 
  
    "#{self.class} requires a nested Persistor :plugin:" 
    
  ) if (
    @config[:plugin].nil? or
    @persistor = @root.config.get_plugin( @config[:plugin] ) and
    @persistor.nil?
  
  )
  
  config_exception( 
  
    "#{self.class} requires :assess: subconfig"
  
  ) unless (
  
    @config.has_key?( :assess ) and
    @assess = @config[:assess]
    
  )
  
  @assess ||= {}
  
  config_exception( 
  
    "#{self.class} requires :assess: to contain a subconfig of dataitems to collate"
  
  ) unless (
  
    @assess.is_a?( Hash )
  
  )
  
end