Skip to content

Commit

Permalink
modernize syntax using Rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
paddor committed Oct 18, 2022
1 parent 019d52f commit c76eeae
Show file tree
Hide file tree
Showing 33 changed files with 242 additions and 21 deletions.
12 changes: 10 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Layout/SpaceBeforeBrackets: # new in 1.7
Lint/AmbiguousAssignment: # new in 1.7
Enabled: true
Lint/AmbiguousOperatorPrecedence: # new in 1.21
Enabled: true
Enabled: false
Lint/AmbiguousRange: # new in 1.19
Enabled: true
Lint/ConstantOverwrittenInRescue: # new in 1.31
Expand Down Expand Up @@ -163,5 +163,13 @@ Style/WordArray:
Enabled: false
Style/Semicolon:
Enabled: false
Lint/AmbiguousOperatorPrecedence:
Naming/MethodName:
Enabled: false
Style/EmptyMethod:
Enabled: false
Layout/EmptyLinesAroundClassBody:
Enabled: true
EnforcedStyle: empty_lines_except_namespace
Layout/EmptyLinesAroundModuleBody:
Enabled: true
EnforcedStyle: empty_lines_except_namespace
11 changes: 11 additions & 0 deletions lib/cztop/actor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,23 @@ module CZTop
#
# @see http://api.zeromq.org/czmq3-0:zactor
class Actor

include HasFFIDelegate
extend CZTop::HasFFIDelegate::ClassMethods
include ZsockOptions
include SendReceiveMethods
include PolymorphicZsockMethods
include ::CZMQ::FFI


# Raised when trying to interact with a terminated actor.
class DeadActorError < RuntimeError; end


# @return [Exception] the exception that crashed this actor, if any
attr_reader :exception


# Creates a new actor. Either pass a callback directly or a block. The
# block will be called for every received message.
#
Expand Down Expand Up @@ -113,6 +117,7 @@ def <<(message)
retry
end
end

self
end

Expand Down Expand Up @@ -206,8 +211,10 @@ def crashed?
!!@exception # if set, it has crashed
end


private


# Shims the given handler. The shim is used to do the handshake, to
# {#process_messages}, and ensure we're notified when the handler has
# terminated.
Expand All @@ -227,6 +234,7 @@ def shim(handler)
@pipe = Socket::PAIR.from_ffi_delegate(pipe_delegate)
@pipe.signal # handshake, so zactor_new() returns
end

process_messages(handler)
rescue Exception
@exception = $ERROR_INFO
Expand All @@ -242,9 +250,11 @@ def handler_shimmed?
!!@handler_thread # if it exists, it's shimmed
end


# the command which causes an actor handler to terminate
TERM = '$TERM'


# Successively receive messages that were sent to the actor and
# yield them to the given handler to process them. The a pipe (a
# {Socket::PAIR} socket) is also passed to the handler so it can send back
Expand Down Expand Up @@ -321,5 +331,6 @@ def await_handler_death
@running = false
end
end

end
end
2 changes: 2 additions & 0 deletions lib/cztop/authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module CZTop
#
# @see http://api.zeromq.org/czmq3-0:zauth
class Authenticator

include ::CZMQ::FFI

# function pointer to the +zauth()+ function
Expand Down Expand Up @@ -107,5 +108,6 @@ def gssapi
@actor << 'GSSAPI'
@actor.wait
end

end
end
2 changes: 2 additions & 0 deletions lib/cztop/beacon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module CZTop
#
# @see http://api.zeromq.org/czmq3-0:zbeacon
class Beacon

include ::CZMQ::FFI

# function pointer to the +zbeacon()+ function
Expand Down Expand Up @@ -111,5 +112,6 @@ def unsubscribe
def receive
@actor.receive
end

end
end
2 changes: 2 additions & 0 deletions lib/cztop/cert_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module CZTop
#
# @see http://api.zeromq.org/czmq3-0:zcertstore
class CertStore

include ::CZMQ::FFI
include HasFFIDelegate
extend CZTop::HasFFIDelegate::ClassMethods
Expand Down Expand Up @@ -53,5 +54,6 @@ def insert(cert)
ffi_delegate.insert(cert.ffi_delegate)
@_inserted_pubkeys << pubkey
end

end
end
6 changes: 4 additions & 2 deletions lib/cztop/certificate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module CZTop
# Represents a CZMQ::FFI::Zcert.
class Certificate

include HasFFIDelegate
extend CZTop::HasFFIDelegate::ClassMethods
include ::CZMQ::FFI
Expand Down Expand Up @@ -122,8 +123,8 @@ def meta_keys
first_key = zlist.first
return [] if first_key.null?

keys = [first_key.read_string]
while key = zlist.next
keys = [first_key.read_string]
while (key = zlist.next)
break if key.null?

keys << key.read_string
Expand Down Expand Up @@ -203,5 +204,6 @@ def dup
def ==(other)
ffi_delegate.eq(other.ffi_delegate)
end

end
end
4 changes: 4 additions & 0 deletions lib/cztop/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ module CZTop
# Represents a CZMQ::FFI::Zconfig item.
# @see http://rfc.zeromq.org/spec:4/ZPL
class Config

include HasFFIDelegate
extend CZTop::HasFFIDelegate::ClassMethods


# Initializes a new {Config} item. Takes an optional block to initialize
# the item further.
# @param name [String] config item name
Expand Down Expand Up @@ -110,6 +112,7 @@ def [](path, default = '')

# @!endgroup


# Compares this config item to another. Only the name and value are
# considered. If you need to compare a config tree, use {#tree_equal?}.
# @param other [Config] the other config item
Expand All @@ -127,5 +130,6 @@ def ==(other)
def tree_equal?(other)
self == other && children == other.children
end

end
end
4 changes: 4 additions & 0 deletions lib/cztop/config/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module CZTop
class Config

# Access this config item's comments.
# @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
Expand All @@ -14,6 +15,7 @@ def comments

# Used to access a {Config}'s comments.
class CommentsAccessor

include Enumerable

# @param config [Config]
Expand Down Expand Up @@ -68,6 +70,8 @@ def size
def _zlist
@config.ffi_delegate.comments
end

end

end
end
6 changes: 6 additions & 0 deletions lib/cztop/config/serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Methods used around serialization of {CZTop::Config} items.
module CZTop::Config::Serialization

# Serialize to a string in the ZPL format.
# @return [String]
def to_s
Expand All @@ -18,6 +19,7 @@ def filename

# Some class methods for {Config} related to serialization.
module ClassMethods

# Loads a {Config} tree from a string.
# @param string [String] the tree
# @return [Config]
Expand Down Expand Up @@ -47,6 +49,7 @@ def load(path)
def _load(string)
from_string(string)
end

end


Expand Down Expand Up @@ -87,10 +90,13 @@ def reload
def _dump(_level)
to_s
end

end


class CZTop::Config

include Serialization
extend Serialization::ClassMethods

end
10 changes: 10 additions & 0 deletions lib/cztop/config/traversing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Methods used to traverse a {CZTop::Config} tree.
module CZTop::Config::Traversing

# Calls the given block once for each {Config} item in the tree, starting
# with self.
#
Expand Down Expand Up @@ -65,6 +66,7 @@ def siblings
# Used to give access to a {Config} item's children or siblings.
# @abstract
class FamilyAccessor

include Enumerable

# @param config [Config] the relative starting point (either parent or
Expand Down Expand Up @@ -107,11 +109,13 @@ def ==(other)
end
true
end

end


# Accesses the younger siblings of a given {Config} item.
class SiblingsAccessor < FamilyAccessor

# Returns the first sibling.
# @return [Config]
# @return [nil] if no younger siblings
Expand All @@ -121,11 +125,13 @@ def first

CZTop::Config.from_ffi_delegate(ptr)
end

end


# Accesses the direct children of a given {Config} item.
class ChildrenAccessor < FamilyAccessor

def first
ptr = @config.ffi_delegate.child
return nil if ptr.null?
Expand All @@ -145,6 +151,7 @@ def new(name = nil, value = nil)
yield config if block_given?
config
end

end


Expand All @@ -170,9 +177,12 @@ def last_at_depth(level)

from_ffi_delegate(ptr)
end

end


class CZTop::Config

include Traversing

end
2 changes: 2 additions & 0 deletions lib/cztop/frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module CZTop
#
# @see http://api.zeromq.org/czmq3-0:zframe
class Frame

include HasFFIDelegate
extend CZTop::HasFFIDelegate::ClassMethods

Expand Down Expand Up @@ -187,5 +188,6 @@ def group=(new_group)
rc = ffi_delegate.set_group(new_group)
raise_zmq_err(format('unable to set group to %p', new_group)) if rc == -1
end

end
end
4 changes: 4 additions & 0 deletions lib/cztop/has_ffi_delegate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# This module is used to attach the low-level objects of classes within the
# CZMQ::FFI namespace (coming from the _czmq-ffi-gen_ gem) as delegates.
module CZTop::HasFFIDelegate

# @return [CZMQ::FFI::*] the attached delegate
attr_reader :ffi_delegate

Expand Down Expand Up @@ -67,6 +68,7 @@ def raise_zmq_err(msg = CZMQ::FFI::Errors.strerror,

# Some class methods related to FFI delegates.
module ClassMethods

include Forwardable

# Delegate specified instance method to the registered FFI delegate.
Expand All @@ -92,5 +94,7 @@ def from_ffi_delegate(ffi_delegate)
obj.attach_ffi_delegate(ffi_delegate)
obj
end

end

end
2 changes: 2 additions & 0 deletions lib/cztop/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module CZTop
# Represents a CZMQ::FFI::Zmsg.
class Message

include HasFFIDelegate
extend CZTop::HasFFIDelegate::ClassMethods
include ::CZMQ::FFI
Expand Down Expand Up @@ -208,5 +209,6 @@ def routing_id=(new_routing_id)

ffi_delegate.set_routing_id(new_routing_id)
end

end
end
4 changes: 4 additions & 0 deletions lib/cztop/message/frames.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module CZTop
class Message

# @return [Integer] number of frames
# @see content_size
def size
Expand All @@ -18,6 +19,7 @@ def frames

# Used to access a {Message}'s {Frame}s.
class FramesAccessor

include Enumerable

# @param message [Message]
Expand Down Expand Up @@ -79,6 +81,8 @@ def each
end
self
end

end

end
end
Loading

0 comments on commit c76eeae

Please sign in to comment.