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 HasFFIDelegate::ClassMethods

ffi_delegate, from_ffi_delegate

Methods included from Serialization::ClassMethods

_load, from_string, load

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) (defaults to: nil)

    parent config item

Yield Parameters:

  • config (self)


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

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



120
121
122
123
# File 'lib/cztop/config.rb', line 120

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



105
106
107
108
109
110
# File 'lib/cztop/config.rb', line 105

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:



91
92
93
# File 'lib/cztop/config.rb', line 91

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:



11
12
13
# File 'lib/cztop/config/comments.rb', line 11

def comments
  CommentsAccessor.new(self)
end

#inspectString

Inspects this CZTop::Config item.

Returns:

  • (String)

    shows class, name, and value



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

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)



45
46
47
48
49
50
# File 'lib/cztop/config.rb', line 45

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)


56
57
58
# File 'lib/cztop/config.rb', line 56

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



130
131
132
# File 'lib/cztop/config.rb', line 130

def tree_equal?(other)
  self == other && 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)


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

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)


75
76
77
# File 'lib/cztop/config.rb', line 75

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