Class: CZTop::Config

Inherits:
Object
  • Object
show all
Extended by:
Serialization::ClassMethods, HasFFIDelegate::ClassMethods
Includes:
Serialization, Traversing, HasFFIDelegate
Defined in:
lib/cztop/config.rb,
lib/cztop/config/comments.rb,
lib/cztop/config/traversing.rb,
lib/cztop/config/serialization.rb

Overview

Represents a CZMQ::FFI::Zconfig item.

Defined Under Namespace

Modules: Serialization, Traversing Classes: CommentsAccessor

Instance Attribute Summary

Attributes included from HasFFIDelegate

#ffi_delegate

ZPL attributes collapse

Instance Method Summary collapse

Methods included from Serialization::ClassMethods

_load, from_string, load

Methods included from HasFFIDelegate::ClassMethods

ffi_delegate, from_ffi_delegate

Methods included from Serialization

#_dump, #filename, #reload, #save, #to_s

Methods included from Traversing

#children, #execute, #last_at_depth, #locate, #siblings

Methods included from HasFFIDelegate

#attach_ffi_delegate, #from_ffi_delegate, #raise_zmq_err, raise_zmq_err, #to_ptr

Constructor Details

#initialize(name = nil, value = nil, parent: nil) {|config| ... } ⇒ Config

Note:

If parent is given, the native child will be destroyed when the native parent is destroyed (and not when the child's corresponding CZTop::Config object is garbage collected).

Initializes a new CZTop::Config item. Takes an optional block to initialize the item further.

Parameters:

  • name (String) (defaults to: nil)

    config item name

  • value (String) (defaults to: nil)

    config item value

  • parent (Config)

    parent config item

Yield Parameters:

  • config (self)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cztop/config.rb', line 18

def initialize(name = nil, value = nil, parent: nil)
  if parent
    parent = parent.ffi_delegate if parent.is_a?(Config)
    delegate = ::CZMQ::FFI::Zconfig.new(name, parent)
    attach_ffi_delegate(delegate)

    # NOTE: this delegate must not be freed automatically, because the
    # parent will free it.
    delegate.__undef_finalizer
  else
    delegate = ::CZMQ::FFI::Zconfig.new(name, nil)
    attach_ffi_delegate(delegate)
  end

  self.value = value if value
  yield self if block_given?
end

Instance Method Details

#==(other) ⇒ Boolean

Compares this config item to another. Only the name and value are considered. If you need to compare a config tree, use #tree_equal?.

Parameters:

  • other (Config)

    the other config item

Returns:

  • (Boolean)

    whether they're equal



108
109
110
111
# File 'lib/cztop/config.rb', line 108

def ==(other)
  name == other.name &&
  value == other.value
end

#[](path, default = "") ⇒ String, default Also known as: get

Note:

The default value is not returned when the config item exists but just doesn't have a value. In that case, it'll return the empty string.

Get the value of the current config item.

Parameters:

  • path (String, #to_s)

    path to config item

  • default (String, #to_s) (defaults to: "")

    default value to return if config item doesn't exist

Returns:

  • (String)
  • (default)

    if config item doesn't exist



95
96
97
98
99
# File 'lib/cztop/config.rb', line 95

def [](path, default = "")
  ptr = ffi_delegate.get(path, default)
  return nil if ptr.null?
  ptr.read_string
end

#[]=(path, value) ⇒ value Also known as: put

Update the value of a config item by path.

Parameters:

  • path (String, #to_s)

    path to config item

  • value (String, #to_s)

    path to config item

Returns:



81
82
83
# File 'lib/cztop/config.rb', line 81

def []=(path, value)
  ffi_delegate.put(path.to_s, value.to_s)
end

#commentsCommentsAccessor

Note:

Note that comments are discarded when loading a config (either from a string or file) and thus, only the comments you add during runtime are accessible.

Access this config item's comments.

Returns:



9
10
11
# File 'lib/cztop/config/comments.rb', line 9

def comments
  return CommentsAccessor.new(self)
end

#inspectString

Inspects this CZTop::Config item.

Returns:

  • (String)

    shows class, name, and value



73
74
75
# File 'lib/cztop/config.rb', line 73

def inspect
  "#<#{self.class.name}: name=#{name.inspect} value=#{value.inspect}>"
end

#nameString?

Gets the name.

Returns:

  • (String)

    name of the config item

  • (nil)

    for unnamed elements (like freshly initialized without a name)



42
43
44
45
46
# File 'lib/cztop/config.rb', line 42

def name
  ptr = ffi_delegate.name
  return nil if ptr.null? # NOTE: for unnamed elements
  ptr.read_string
end

#name=(new_name) ⇒ new_name

Sets a new name.

Parameters:

  • new_name (String, #to_s)

Returns:

  • (new_name)


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

def name=(new_name)
  ffi_delegate.set_name(new_name.to_s)
end

#tree_equal?(other) ⇒ Boolean

Compares this config tree to another tree or subtree. Names, values, and children are considered.

Parameters:

  • other (Config)

    the other config tree

Returns:

  • (Boolean)

    whether they're equal



117
118
119
# File 'lib/cztop/config.rb', line 117

def tree_equal?(other)
  self == other && self.children == other.children
end

#valueString

Note:

This returns an empty string if the value is unset.

Get the value of the config item.

Returns:

  • (String)


58
59
60
61
62
# File 'lib/cztop/config.rb', line 58

def value
  ptr = ffi_delegate.value
  return "" if ptr.null? # NOTE: for root elements
  ptr.read_string
end

#value=(new_value) ⇒ new_value

Set or update the value of the config item.

Parameters:

  • new_value (String, #to_s)

Returns:

  • (new_value)


67
68
69
# File 'lib/cztop/config.rb', line 67

def value=(new_value)
  ffi_delegate.set_value("%s", :string, new_value.to_s)
end