Module: CZTop::Config::Traversing

Included in:
CZTop::Config
Defined in:
lib/cztop/config/traversing.rb

Overview

Methods used to traverse a CZTop::Config tree.

Defined Under Namespace

Classes: ChildrenAccessor, FamilyAccessor, SiblingsAccessor

Instance Method Summary collapse

Instance Method Details

#childrenChildrenAccessor

Access to this config item’s direct children.

Returns:



52
53
54
# File 'lib/cztop/config/traversing.rb', line 52

def children
  ChildrenAccessor.new(self)
end

#execute {|config, level| ... } ⇒ Object

Calls the given block once for each CZTop::Config item in the tree, starting with self.

Yield Parameters:

  • config (Config)

    the config item

  • level (Integer)

    level of the item (self has level 0, its direct children have level 1)

Returns:

  • (Object)

    the block’s return value

Raises:

  • (ArgumentError)

    if no block given

  • (Exception)

    the block’s exception, in case it raises (it won’t call the block any more after that)



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cztop/config/traversing.rb', line 16

def execute
  raise ArgumentError, 'no block given' unless block_given?

  exception                           = nil
  block_value                         = nil
  ret                                 = nil
  callback                            = CZMQ::FFI::Zconfig.fct do |zconfig, _arg, level|
    begin
      # NOTE: work around JRuby and Rubinius bug, where it'd keep calling
      # this FFI::Function, even when the block `break`ed
      if ret != -1
        config      = from_ffi_delegate(zconfig)
        block_value = yield config, level
        ret         = 0 # report success to keep zconfig_execute() going
      end
    rescue StandardError
      # remember exception, so we can raise it later to the ruby code
      # (it can't be raised now, as we have to report failure to
      # zconfig_execute())
      exception = $ERROR_INFO

      ret = -1 # report failure to stop zconfig_execute() immediately
    ensure
      ret ||= -1 # in case of 'break'
    end
    ret
  end
  ffi_delegate.execute(callback, _arg = nil)
  raise exception if exception

  block_value
end

#last_at_depth(level) ⇒ Config?

Finds last item at given level (0 = root).

Returns:

  • (Config)

    the last config item at given level

  • (nil)

    if there’s no config item at given level



174
175
176
177
178
179
# File 'lib/cztop/config/traversing.rb', line 174

def last_at_depth(level)
  ptr = ffi_delegate.at_depth(level)
  return nil if ptr.null?

  from_ffi_delegate(ptr)
end

#locate(path) ⇒ Config?

Finds a config item along a path, relative to the current item.

Parameters:

  • path (String)

    path (leading slash is optional and will be ignored)

Returns:

  • (Config)

    the found config item

  • (nil)

    if there’s no config item under this path



163
164
165
166
167
168
# File 'lib/cztop/config/traversing.rb', line 163

def locate(path)
  ptr = ffi_delegate.locate(path)
  return nil if ptr.null?

  from_ffi_delegate(ptr)
end

#siblingsSiblingsAccessor

Note:

Only the “younger” (later in the ZPL file) config items are considered.

Access to this config item’s siblings.

Returns:



61
62
63
# File 'lib/cztop/config/traversing.rb', line 61

def siblings
  SiblingsAccessor.new(self)
end