Class: Nucleon::Core

Inherits:
Config show all
Includes:
Mixin::Colors
Defined in:
lib/core/core.rb

Overview

  1. Provide an initialized lookup

Constant Summary collapse

@@logger =

Global logger instance

See:

  • Nucleon::Util::Logger

Util::Logger.new('core')
@@ui =

Global console instance

See:

  • Nucleon::Util::Console

Util::Console.new('core')
@@ui_lock =

Global UI Mutex

TODO: This may not be needed?

Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::Colors

#black, #blue, #cyan, #green, #grey, #purple, #red, #yellow

Methods inherited from Config

#[], #[]=, #append, array, #array, #clear, #defaults, #delete, #empty?, ensure, #export, #filter, filter, #get, #get_array, #get_hash, #has_key?, hash, #hash, #import, #init, init, init_flat, #keys, #prepend, #set, #string, string, string_map, #string_map, symbol, #symbol, #symbol_array, #symbol_map, symbol_map, test, #test

Methods included from Mixin::ConfigOptions

#all_options, #clear_options, #contexts, #get_options, #set_options

Methods included from Mixin::ConfigCollection

#all_properties, #clear_properties, #delete_property, #get_property, #save_properties, #set_property

Constructor Details

#initialize(data = {}, defaults = {}, force = true, set_initialized = true, basic_merge = true) ⇒ Core

Initialize a new core Nucleon object

TODO: Figure out some way to make the console and logging systems pluggable?

  • Parameters

    • nil, Hash, Nucleon::Config

      data Configurations to import

    • Hash

      defaults Configuration defaults that may be overridden by config data

    • Boolean

      force Whether or not to force override of values where types don’t match during merge

    • Boolean

      set_initialized Whether or not to the initialized flag is set after this object is constructed

    • Boolean

      basic_merge Whether or not to perform a basic merge or deep (recursive) merge

  • Returns

    • Void

      This method does not return a value

  • Errors

See also:

  • Nucleon::Config::new

  • Nucleon::Config#delete

  • Nucleon::Config#export

  • Nucleon::Config#defaults

  • Nucleon::Util::Data::ensure_value

  • Nucleon::Util::Console::colorize



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/core/core.rb', line 81

def initialize(data = {}, defaults = {}, force = true, set_initialized = true, basic_merge = true)
  super(data, defaults, force, basic_merge)

  @initialized = false
  @class_color = Util::Data.ensure_value(delete(:class_color, :cyan), :cyan)
  @class_label = self.class.to_s.downcase.gsub(/^nucleon::/, '')

  self.logger = delete(:logger, @class_label)
  self.ui     = Config.new(export).defaults({ :resource => Util::Console.colorize(@class_label, @class_color) })

  logger.debug('Initialized instance logger and interface')
  @initialized = true if set_initialized
end

Instance Attribute Details

#loggerObject

Nucleon::Util::Logger

Instance logger



120
121
122
# File 'lib/core/core.rb', line 120

def logger
  @logger
end

#uiObject

Nucleon::Util::Console

Instance console



124
125
126
# File 'lib/core/core.rb', line 124

def ui
  @ui
end

Class Method Details

.loggerObject

Return global logger instance

  • Parameters

  • Returns

    • Nucleon::Util::Logger

      Global logger instance

  • Errors



135
136
137
# File 'lib/core/core.rb', line 135

def self.logger
  return @@logger
end

.uiObject

Return global console instance

This is named ui for historical reasons. It might change to console in the future.

  • Parameters

  • Returns

    • Nucleon::Util::Console

      Global console instance

  • Errors



175
176
177
# File 'lib/core/core.rb', line 175

def self.ui
  return @@ui
end

.ui_group(resource, color = :cyan) ⇒ Object

Contextualize console operations in a code block with a given resource name.

TODO: May not need Mutex synchronization?

  • Parameters

    • String, Symbol

      resource Console resource identifier (prefix)

    • Symbol

      color Color to use; :black, :red, :green, :yellow, :blue, :purple, :cyan, :grey

  • Returns

    • Void

      This method does not return a value

  • Errors

  • Yields

    • Nucleon::Util::Console

      ui Current global console instance

See also:

  • Nucleon::Util::Console::colorize



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/core/core.rb', line 222

def self.ui_group(resource, color = :cyan) # :yields: ui
  @@ui_lock.synchronize do
    begin
      ui_resource = ui.resource
      ui.resource = Util::Console.colorize(resource, color)
      yield(ui)

    ensure
      ui.resource = ui_resource
    end
  end
end

Instance Method Details

#initialized?Boolean

Check if object is initialized?

The initialized flag must be set from a method within the class. It can not be set externally.

  • Parameters

  • Returns

    • Boolean

      Whether or not object has been marked as initialized

  • Errors

Returns:

  • (Boolean)


110
111
112
# File 'lib/core/core.rb', line 110

def initialized?
  @initialized
end

#ui_group(resource, color = :cyan) ⇒ Object

Contextualize console operations in a code block with a given resource name.

  • Parameters

    • String, Symbol

      resource Console resource identifier (prefix)

    • Symbol

      color Color to use; :black, :red, :green, :yellow, :blue, :purple, :cyan, :grey

  • Returns

    • Void

      This method does not return a value

  • Errors

  • Yields

    • Nucleon::Util::Console

      ui Current object console instance

See also:

  • Nucleon::Util::Console::colorize



252
253
254
255
256
257
258
259
# File 'lib/core/core.rb', line 252

def ui_group(resource, color = :cyan) # :yields: ui
  ui_resource = ui.resource
  ui.resource = Util::Console.colorize(resource, color)
  yield(ui)

ensure
  ui.resource = ui_resource
end