Module: CelluloidPubsub::Helper

Included in:
BaseActor, ClientConnection
Defined in:
lib/celluloid_pubsub/helper.rb

Overview

class that holds the options that are configurable for this gem

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.action_subscribe?(action) ⇒ Boolean

method used to determine if a action is a subsribe action

Parameters:

  • action (string)

    The action that will be checked

Returns:

  • (Boolean)

    Returns true if the action equals to ‘subscribe’



118
119
120
# File 'lib/celluloid_pubsub/helper.rb', line 118

def action_subscribe?(action)
  action == 'subscribe'
end

.fetch_gem_version(gem_name) ⇒ Float?

returns the parsed version of the gem

Parameters:

  • gem_name (String)

    name of the gem

Returns:

  • (Float, nil)

    returns the version of the gem



82
83
84
85
# File 'lib/celluloid_pubsub/helper.rb', line 82

def fetch_gem_version(gem_name)
  version = find_loaded_gem_property(gem_name)
  version.blank? ? nil : get_parsed_version(version)
end

.filtered_error?(error) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

checks if a given error needs to be filtered

Parameters:

  • error (Exception::Error)

Returns:

  • (Boolean)

    returns true if the error should be filtered otherwise false



177
178
179
# File 'lib/celluloid_pubsub/helper.rb', line 177

def filtered_error?(error)
  [Interrupt].any? { |class_name| error.is_a?(class_name) }
end

.find_loaded_gem(name, property = nil) ⇒ String?

returns the gem’s property from its speification or nil

Parameters:

  • name (String)

    the name of the gem

  • property (String) (defaults to: nil)

    name of the property we want

Returns:

  • (String, nil)

    returns the version of the gem



60
61
62
63
# File 'lib/celluloid_pubsub/helper.rb', line 60

def find_loaded_gem(name, property = nil)
  gem_spec = Gem.loaded_specs.values.find { |repo| repo.name == name }
  gem_spec.present? && property.present? ? gem_spec.send(property) : gem_spec
end

.find_loaded_gem_property(gem_name, property = 'version') ⇒ String?

returns the gem’s property from its speification or nil

Parameters:

  • gem_name (String)

    name of the gem

  • property (String) (defaults to: 'version')

    name of the property we want

Returns:

  • (String, nil)

    returns the version of the gem



72
73
74
# File 'lib/celluloid_pubsub/helper.rb', line 72

def find_loaded_gem_property(gem_name, property = 'version')
  find_loaded_gem(gem_name, property)
end

.get_parsed_version(version) ⇒ Float?

returns the parsed version as a float or nil

Parameters:

  • version (String)

    the version that needs to be parsed

Returns:

  • (Float, nil)

    returns the version of the gem



93
94
95
96
# File 'lib/celluloid_pubsub/helper.rb', line 93

def get_parsed_version(version)
  version_parser = CelluloidPubsub::GemVersionParser.new(version)
  version_parser.parsed_number
end

.log_debug(message) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

receives a message, and logs it to the log file if debug is enabled

:nocov:

Parameters:

  • message (Object)


203
204
205
206
207
# File 'lib/celluloid_pubsub/helper.rb', line 203

def log_debug(message)
  return unless respond_to?(:debug_enabled?)
  return if Celluloid.logger.blank? || !debug_enabled?
  Celluloid.logger.debug(message)
end

.parse_options(options) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

receives a list of options that need to be parsed if it is an Array will return the first element , otherwise if it is an Hash will return the hash with string keys, otherwise an empty hash

Parameters:

  • options (Hash, Array)

    the options that need to be parsed

Returns:

  • (Hash)


190
191
192
193
# File 'lib/celluloid_pubsub/helper.rb', line 190

def parse_options(options)
  options = options.is_a?(Array) ? options.last : options
  options.is_a?(Hash) ? options.deep_stringify_keys : {}
end

.setup_celluloid_exception_handlervoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

sets the celluloid exception handler

:nocov:



144
145
146
147
148
149
150
151
152
153
# File 'lib/celluloid_pubsub/helper.rb', line 144

def setup_celluloid_exception_handler
  Celluloid.task_class = defined?(Celluloid::TaskThread) ? Celluloid::TaskThread : Celluloid::Task::Threaded
  Celluloid.exception_handler do |ex|
    unless filtered_error?(ex)
      puts ex
      puts ex.backtrace
      puts ex.cause
    end
  end
end

.setup_celluloid_loggervoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

sets the celluloid logger and the celluloid exception handler

:nocov:



128
129
130
131
132
133
134
135
# File 'lib/celluloid_pubsub/helper.rb', line 128

def setup_celluloid_logger
  return if !debug_enabled? || (respond_to?(:log_file_path) && log_file_path.blank?)
  setup_log_file
  Celluloid.logger = ::Logger.new(log_file_path).tap do |logger|
    logger.level = respond_to?(:log_level) ? log_level : ::Logger::Severity::INFO
  end
  setup_celluloid_exception_handler
end

.setup_log_filevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

creates the log file where the debug messages will be printed

:nocov:



162
163
164
165
166
167
# File 'lib/celluloid_pubsub/helper.rb', line 162

def setup_log_file
  return if !debug_enabled? || (respond_to?(:log_file_path) && log_file_path.blank?)
  FileUtils.mkdir_p(File.dirname(log_file_path)) unless File.directory?(log_file_path)
  log_file = File.open(log_file_path, 'wb')
  log_file.sync = true
end

.verify_gem_version(gem_version, version, options = {}) ⇒ Boolean

returns true if gem_version is less or equal to the specified version, otherwise false

Parameters:

  • gem_version (String)

    the version of the gem

  • version (String)

    the version that will be checked against

  • options (Hash) (defaults to: {})

    additional options

Returns:

  • (Boolean)

    returns true if gem_version is less or equal to the specified version, otherwise false



106
107
108
109
110
# File 'lib/celluloid_pubsub/helper.rb', line 106

def verify_gem_version(gem_version, version, options = {})
  options.stringify_keys!
  version = get_parsed_version(version)
  get_parsed_version(gem_version).send(options.fetch('operator', '<='), version)
end

Instance Method Details

#actor_dead?(actor) ⇒ Boolean

the method try to decide if an actor is dead In Celluloid 0.18 there is no ‘dead?` method anymore

So we are trying to be backward-compatible with older versions

Parameters:

  • actor (Celluloid::Actor)

Returns:

  • (Boolean)

    returns true if the actor is dead, otherwise false



27
28
29
30
31
# File 'lib/celluloid_pubsub/helper.rb', line 27

def actor_dead?(actor)
  raise actor.class.inspect if !actor.respond_to?(:dead?) && !actor.respond_to?(:alive?)
  (actor.respond_to?(:dead?) && actor.dead?) ||
    (actor.respond_to?(:alive?) && !actor.alive?)
end

#cell_actor::Celluloid::Actor

returns the current actor

Returns:

  • (::Celluloid::Actor)

    returns the current actor



47
48
49
# File 'lib/celluloid_pubsub/helper.rb', line 47

def cell_actor
  ::Celluloid::Actor.current
end

#own_selfObject

returns the instance of the class that includes the actor, this is useful in tests

Returns:

  • (Object)

    returns the object



38
39
40
# File 'lib/celluloid_pubsub/helper.rb', line 38

def own_self
  self
end

#succesfull_subscription?(message) ⇒ Boolean

checks if the message has the successfull subscription action

Parameters:

  • message (string)

    The message that will be checked

Returns:

  • (Boolean)

    return true if message contains key client_action with value ‘succesfull_subscription’



15
16
17
# File 'lib/celluloid_pubsub/helper.rb', line 15

def succesfull_subscription?(message)
  message.is_a?(Hash) && message['client_action'] == 'successful_subscription'
end