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



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

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



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

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



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

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



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

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



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

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