Class: CZTop::Config
- Inherits:
-
Object
- Object
- CZTop::Config
- 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
ZPL attributes collapse
-
#[](path, default = '') ⇒ String, default
(also: #get)
Get the value of the current config item.
-
#[]=(path, value) ⇒ value
(also: #put)
Update the value of a config item by path.
-
#inspect ⇒ String
Inspects this Config item.
-
#name ⇒ String?
Gets the name.
-
#name=(new_name) ⇒ new_name
Sets a new name.
-
#value ⇒ String
Get the value of the config item.
-
#value=(new_value) ⇒ new_value
Set or update the value of the config item.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares this config item to another.
-
#comments ⇒ CommentsAccessor
Access this config item’s comments.
-
#initialize(name = nil, value = nil, parent: nil) {|config| ... } ⇒ Config
constructor
Initializes a new Config item.
-
#tree_equal?(other) ⇒ Boolean
Compares this config tree to another tree or subtree.
Methods included from HasFFIDelegate::ClassMethods
ffi_delegate, from_ffi_delegate
Methods included from Serialization::ClassMethods
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, #to_ptr
Constructor Details
#initialize(name = nil, value = nil, parent: nil) {|config| ... } ⇒ Config
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.
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?.
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
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.
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.
91 92 93 |
# File 'lib/cztop/config.rb', line 91 def []=(path, value) ffi_delegate.put(path.to_s, value.to_s) end |
#comments ⇒ CommentsAccessor
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.
11 12 13 |
# File 'lib/cztop/config/comments.rb', line 11 def comments CommentsAccessor.new(self) end |
#inspect ⇒ String
Inspects this CZTop::Config item.
82 83 84 |
# File 'lib/cztop/config.rb', line 82 def inspect "#<#{self.class.name}: name=#{name.inspect} value=#{value.inspect}>" end |
#name ⇒ String?
Gets the 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.
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.
130 131 132 |
# File 'lib/cztop/config.rb', line 130 def tree_equal?(other) self == other && children == other.children end |
#value ⇒ String
This returns an empty string if the value is unset.
Get the value of the config item.
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.
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 |