Module: Hash::Hooked::HashInterface

Includes:
IdentifiesAs
Included in:
Hash::Hooked
Defined in:
lib/hash/hooked/hash_interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configuration_instanceObject

Returns Object that instance is attached to; primarily useful for reference from hooks.

Returns:

  • (Object)

    Object that instance is attached to; primarily useful for reference from hooks.



50
51
52
# File 'lib/hash/hooked/hash_interface.rb', line 50

def configuration_instance
  @configuration_instance
end

Instance Method Details

#[](key) ⇒ Object

#



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/hash/hooked/hash_interface.rb', line 202

def []( key )

  object = nil
  
  if @without_hooks
    pre_get_hook_result = true
  else
    pre_get_hook_result = pre_get_hook( key )
  end
  
  if pre_get_hook_result
  
    object = super( key )
    
    unless @without_hooks
      object = post_get_hook( key, object )
    end
    
  end
    
  return object
  
end

#[]=(key, object) ⇒ Object Also known as: store

[]= #



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/hash/hooked/hash_interface.rb', line 257

def []=( key, object )
  
  unless @without_hooks
    object = pre_set_hook( key, object )
  end
  
  perform_set_between_hooks( key, object )

  unless @without_hooks
    object = post_set_hook( key, object )
  end
  
  return object

end

#clearObject

clear #



682
683
684
685
686
687
688
# File 'lib/hash/hooked/hash_interface.rb', line 682

def clear
  
  delete_if { true }

  return self

end

#clear_without_hooksObject

Alias to #clear that bypasses hooks.

Returns:

  • (Object)

    Self.



701
702
703
704
705
706
707
708
709
710
711
# File 'lib/hash/hooked/hash_interface.rb', line 701

def clear_without_hooks
  
  @without_hooks = true
  
  clear
  
  @without_hooks = false
  
  return self
  
end

#delete(key) ⇒ Object

delete #



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/hash/hooked/hash_interface.rb', line 310

def delete( key )

  object = nil
  
  if @without_hooks
    pre_delete_result = true
  else
    pre_delete_result = pre_delete_hook( key )
  end
  
  if pre_delete_result
  
    object = perform_delete_between_hooks( key )
    
    unless @without_hooks
      object = post_delete_hook( key, object )
    end
    
  end
  
  return object

end

#delete_ifObject

delete_if #



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/hash/hooked/hash_interface.rb', line 365

def delete_if

  return to_enum unless block_given?

  indexes = [ ]
  
  self.each do |this_key, this_object|
    if yield( this_key, this_object )
      delete( this_key )
    end
  end
      
  return self

end

#delete_if_without_hooks { ... } ⇒ Object

Alias to #delete_if that bypasses hooks.

Yields:

  • Block passed to :delete_if.

Returns:

  • (Object)

    Deleted element.



396
397
398
399
400
401
402
403
404
# File 'lib/hash/hooked/hash_interface.rb', line 396

def delete_if_without_hooks( & block )
  
  @without_hooks = true
  
  delete_if( & block )
  
  @without_hooks = false
  
end

#delete_without_hooks(key) ⇒ Object

Alias to #delete that bypasses hooks.

Parameters:

  • object (Object)

    Element being deleted.

Returns:

  • (Object)

    Element returned.



349
350
351
352
353
354
355
356
357
358
359
# File 'lib/hash/hooked/hash_interface.rb', line 349

def delete_without_hooks( key )
  
  @without_hooks = true
  
  object = delete( key )
  
  @without_hooks = false
  
  return object
  
end

#get_without_hooks(key) ⇒ Object

Alias to #[] that bypasses hooks.

Parameters:

  • key (Object)

    Key where object is to be stored.

Returns:

  • (Object)

    Object retrieved.



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/hash/hooked/hash_interface.rb', line 241

def get_without_hooks( key )
  
  @without_hooks = true
  
  object = self[ key ]
  
  @without_hooks = false
  
  return object
  
end

#initialize(configuration_instance, hash_initialization_arg, ...) ⇒ Object

Initialize with reference a configuration instance.

Parameters:

  • configuration_instance (Object)

    Object that instance will be attached to; primarily useful for reference from hooks.

  • hash_initialization_arg (Object)

    Parameters passed through super to Hash#initialize.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hash/hooked/hash_interface.rb', line 25

def initialize( configuration_instance = nil, default_value = nil, & default_value_block )
  
  @configuration_instance = configuration_instance
  
  if default_value
    super( default_value )
  elsif default_value_block
    super( & default_value_block )
  else
    super()
  end
      
end

#keep_ifObject

keep_if #



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
# File 'lib/hash/hooked/hash_interface.rb', line 458

def keep_if

  return to_enum unless block_given?

  indexes = [ ]
  
  self.each do |this_key, this_object|
    unless yield( this_key, this_object )
      delete( this_key )
    end
  end
      
  return self


end

#keep_if_without_hooks { ... } ⇒ Object

Alias to #keep_if that bypasses hooks.

Yields:

  • Block passed to :keep_if.

Returns:

  • (Object)

    Deleted element.



490
491
492
493
494
495
496
497
498
499
500
# File 'lib/hash/hooked/hash_interface.rb', line 490

def keep_if_without_hooks( & block )
  
  @without_hooks = true
  
  keep_if( & block )
  
  @without_hooks = false
  
  return self
  
end

#merge!(other_hash) ⇒ Object Also known as: update

merge! #

update  #


555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/hash/hooked/hash_interface.rb', line 555

def merge!( other_hash )

  other_hash.each do |this_key, this_object|
    if @compositing_proc
      self[ this_key ] = @compositing_proc.call( self, this_key, this_object )
    else
      self[ this_key ] = this_object
    end
  end

  return self

end

#merge_without_hooks!Object Also known as: update_without_hooks

Alias to #merge! that bypasses hooks.

Returns:

  • (Object)

    Self.



583
584
585
586
587
588
589
590
591
592
593
# File 'lib/hash/hooked/hash_interface.rb', line 583

def merge_without_hooks!
  
  @without_hooks = true
  
  merge!( other_hash )
  
  @without_hooks = false
  
  return self
  
end

#post_delete_hook(key, object) ⇒ Object

A hook that is called after deleting a value.

Parameters:

  • key (Object)

    Key where object has been deleted.

  • object (Object)

    Element deleted.

Returns:

  • (Object)

    Object returned in place of delete result.



190
191
192
193
194
# File 'lib/hash/hooked/hash_interface.rb', line 190

def post_delete_hook( key, object )
  
  return object
  
end

#post_get_hook(key, object) ⇒ Object

A hook that is called after getting a value.

Parameters:

  • key (Object)

    Key where object has been retrieved.

  • object (Object)

    Element retrieved.

Returns:

  • (Object)

    Object returned in place of get result.



144
145
146
147
148
# File 'lib/hash/hooked/hash_interface.rb', line 144

def post_get_hook( key, object )
  
  return object
  
end

#post_set_hook(key, object) ⇒ Object

A hook that is called after setting a value.

Parameters:

  • key (Object)

    Key where object is to be stored.

  • object (Object)

    Element being stored.

Returns:

  • (Object)

    Ignored.



98
99
100
101
102
# File 'lib/hash/hooked/hash_interface.rb', line 98

def post_set_hook( key, object )

  return object
  
end

#pre_delete_hook(key) ⇒ true, false

A hook that is called before deleting a value; if return value is false, delete does not occur.

Parameters:

  • key (Object)

    Key where object is to be deleted.

Returns:

  • (true, false)

    If return value is false, delete does not occur.



165
166
167
168
169
# File 'lib/hash/hooked/hash_interface.rb', line 165

def pre_delete_hook( key )
  
  return true
  
end

#pre_get_hook(key) ⇒ true, false

A hook that is called before getting a value; if return value is false, get does not occur.

Parameters:

  • key (Object)

    Key where object is to be retrieved.

Returns:

  • (true, false)

    If return value is false, get does not occur.



119
120
121
122
123
# File 'lib/hash/hooked/hash_interface.rb', line 119

def pre_get_hook( key )
  
  return true
  
end

#pre_set_hook(key, object) ⇒ true, false

A hook that is called before setting a value; return value is used in place of object.

Parameters:

  • key (Object)

    Key where object is to be stored.

  • object (Object)

    Element being stored.

Returns:

  • (true, false)

    Return value is used in place of object.



73
74
75
76
77
# File 'lib/hash/hooked/hash_interface.rb', line 73

def pre_set_hook( key, object )
  
  return object
  
end

#reject!Object

reject! #



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/hash/hooked/hash_interface.rb', line 410

def reject!

  return to_enum unless block_given?
  
  return_value = nil
  
  self.each do |this_key, this_object|
    if yield( this_key, this_object )
      delete( this_key )
      return_value = self
    end
  end
  
  return return_value

end

#reject_without_hooks! { ... } ⇒ Object

Alias to #reject that bypasses hooks.

Yields:

  • Block passed to :keep_if.

Returns:

  • (Object)

    Self.



442
443
444
445
446
447
448
449
450
451
452
# File 'lib/hash/hooked/hash_interface.rb', line 442

def reject_without_hooks!
  
  @without_hooks = true
  
  return_value = reject!( & block )
  
  @without_hooks = false
  
  return return_value
  
end

#replace(other_hash) ⇒ Object

replace #



601
602
603
604
605
606
607
608
609
610
611
# File 'lib/hash/hooked/hash_interface.rb', line 601

def replace( other_hash )

  # clear current values
  clear

  # merge replacement settings
  merge!( other_hash )

  return self

end

#replace_without_hooks(other_hash) ⇒ Object

Alias to #replace that bypasses hooks.

Parameters:

  • other_hash (Array)

    Other hash to replace self with.

Returns:

  • (Object)

    Self.



628
629
630
631
632
633
634
635
636
# File 'lib/hash/hooked/hash_interface.rb', line 628

def replace_without_hooks( other_hash )
  
  @without_hooks = true
  
  replace( other_hash )
  
  @without_hooks = false
  
end

#select!Object

select! #



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/hash/hooked/hash_interface.rb', line 506

def select!

  return to_enum unless block_given?
  
  return_value = nil
  
  self.each do |this_key, this_object|
    unless yield( this_key, this_object )
      delete( this_key )
      return_value = self
    end
  end
  
  return return_value

end

#select_without_hooks! { ... } ⇒ Object

Alias to #select that bypasses hooks.

Yields:

  • Block passed to :select!.

Returns:

  • (Object)

    Self.



538
539
540
541
542
543
544
545
546
547
548
# File 'lib/hash/hooked/hash_interface.rb', line 538

def select_without_hooks!( & block )
  
  @without_hooks = true
  
  return_value = select!( & block )
  
  @without_hooks = false
  
  return return_value
  
end

#shiftObject

shift #



642
643
644
645
646
647
648
649
650
651
652
653
# File 'lib/hash/hooked/hash_interface.rb', line 642

def shift
  
  object = nil
  
  unless empty?
    last_key = first[ 0 ]
    object = delete( last_key )
  end    

  return [ last_key, object ]

end

#shift_without_hooksObject

Alias to #shift that bypasses hooks.

Returns:

  • (Object)

    Element shifted.



666
667
668
669
670
671
672
673
674
675
676
# File 'lib/hash/hooked/hash_interface.rb', line 666

def shift_without_hooks
  
  @without_hooks = true
  
  object = shift
  
  @without_hooks = false
  
  return object
  
end

#store_without_hooks(key, object) ⇒ Object

Alias to #[]= that bypasses hooks.

Parameters:

  • key (Object)

    Key where object is to be stored.

  • object (Object)

    Element being stored.

Returns:

  • (Object)

    Element returned.



294
295
296
297
298
299
300
301
302
303
304
# File 'lib/hash/hooked/hash_interface.rb', line 294

def store_without_hooks( key, object )
  
  @without_hooks = true
  
  self[ key ] = object
  
  @without_hooks = false
  
  return object
  
end