Class: LogStash::Util::Accessors

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/util/accessors.rb

Overview

Accessors uses a lookup table to speedup access of a field reference of the form “[hello]” to the underlying store hash into => {“world” => “foo”}

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Accessors

Returns a new instance of Accessors.

Parameters:

  • store (Hash)

    the backing data store field refereces point to



41
42
43
44
45
46
47
48
49
# File 'lib/logstash/util/accessors.rb', line 41

def initialize(store)
  @store = store

  # @lut is a lookup table between a field reference and a [target, key] tuple
  # where target is the containing Hash or Array for key in @store.
  # this allows us to directly access the containing object for key instead of
  # walking the field reference path into the inner @store objects
  @lut = {}
end

Instance Method Details

#del(field_reference) ⇒ Object

Returns the removed value in @store for this field reference.

Parameters:

  • field_reference (String)

    the field reference to remove

Returns:

  • (Object)

    the removed value in @store for this field reference



69
70
71
72
73
# File 'lib/logstash/util/accessors.rb', line 69

def del(field_reference)
  target, key = lookup(field_reference)
  return nil unless target
  target.is_a?(Array) ? target.delete_at(key.to_i) : target.delete(key)
end

#get(field_reference) ⇒ Object

Returns the value in @store for this field reference.

Parameters:

  • field_reference (String)

    the field reference

Returns:

  • (Object)

    the value in @store for this field reference



53
54
55
56
57
# File 'lib/logstash/util/accessors.rb', line 53

def get(field_reference)
  target, key = lookup(field_reference)
  return nil unless target
  target.is_a?(Array) ? target[key.to_i] : target[key]
end

#include?(field_reference) ⇒ Boolean

Returns true if the store contains a value for this field reference.

Parameters:

  • field_reference (String)

    the field reference to test for inclusion in the store

Returns:

  • (Boolean)

    true if the store contains a value for this field reference



77
78
79
80
81
82
# File 'lib/logstash/util/accessors.rb', line 77

def include?(field_reference)
  target, key = lookup(field_reference)
  return false unless target

  target.is_a?(Array) ? !target[key.to_i].nil? : target.include?(key)
end

#set(field_reference, value) ⇒ Object

Returns the value set.

Parameters:

  • field_reference (String)

    the field reference

  • value (Object)

    the value to set in @store for this field reference

Returns:

  • (Object)

    the value set



62
63
64
65
# File 'lib/logstash/util/accessors.rb', line 62

def set(field_reference, value)
  target, key = lookup_or_create(field_reference)
  target[target.is_a?(Array) ? key.to_i : key] = value
end