Class: Nucleon::Config::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/core/config/collection.rb

Overview

  • See configuration mixin Nucleon::Mixin::ConfigCollection

Constant Summary collapse

@@lock =

Global access lock

TODO: Might not be needed?

Mutex.new
@@properties =

Global property collection

Structure: @@properties = value

{}

Class Method Summary collapse

Class Method Details

.allObject

Return a reference to all of the globally defined properties.

This method generally should not be used in favor of the ::get method.

  • Parameters

  • Returns

    • Hash<Symbol|ANY>

      Global reference to property registry

  • Errors



61
62
63
# File 'lib/core/config/collection.rb', line 61

def self.all
  @@properties
end

.clearObject

Clear all properties from the collection.

  • Parameters

  • Returns

    • Void

      This method does not currently have a return value

  • Errors



125
126
127
128
129
# File 'lib/core/config/collection.rb', line 125

def self.clear
  @@lock.synchronize do
    @@properties = {}
  end
end

.delete(name) ⇒ Object

Delete property from collection.

  • Parameters

    • String, Symbol

      name Property name to remove

  • Returns

    • Void

      This method does not currently have a return value

  • Errors



110
111
112
113
114
# File 'lib/core/config/collection.rb', line 110

def self.delete(name)
  @@lock.synchronize do
    @@properties.delete(name.to_sym)
  end
end

.get(name) ⇒ Object

Return specified property value.

  • Parameters

    • String, Symbol

      name Property name to return value

  • Returns

    • ANY

      Specified property value

  • Errors



75
76
77
78
79
80
81
# File 'lib/core/config/collection.rb', line 75

def self.get(name)
  value = nil
  @@lock.synchronize do
    value = @@properties[name.to_sym]
  end
  value
end

.save(options = {}) ⇒ Object

Dump properties to disk.

This class was originally designed as a logging mechanism so it is focused on providing write methods so far. Notice the missing load() method.

The property dump must be explicitly enabled with the :config_store option.

TODO:

  1. This method will undergo a large’ish transformation in the future as it is rewritten to make it more flexible.

  2. Throw appropriate error if write fails.

  • Parameters

    • Hash<Symbol|ANY>

      options Method options

      • String

        :log_dir Directory to store the log files

      • String

        :log_name Name of the log file (*no dot or extension*)

      • Boolean

        :config_store Check whether configurations should be stored

  • Returns

    • Void

      This method does not currently have a return value

  • Errors

See also:

  • Nucleon::Util::Data::empty?

  • Nucleon::Util::Data.string_map

  • Nucleon::Util::Data::to_json

  • Nucleon::Util::Data::to_yaml



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/core/config/collection.rb', line 160

def self.save(options = {})
  unless Util::Data.empty?(options[:log_dir])
    @@lock.synchronize do
      log_dir  = options[:log_dir]

      log_name = options[:log_name]
      log_name = 'properties' unless log_name

      if options[:config_store]
        unless File.directory?(log_dir)
          FileUtils.mkdir_p(log_dir)
        end
        Util::Disk.write(File.join(log_dir, "#{log_name}.json"), Util::Data.to_json(@@properties, true))
        Util::Disk.write(File.join(log_dir, "#{log_name}.yaml"), Util::Data.to_yaml(Util::Data.string_map(@@properties)))
      end
    end
  end
end

.set(name, value) ⇒ Object

Set property value.

  • Parameters

    • String, Symbol

      name Property name to set value

    • ANY

      value Specified property value

  • Returns

    • Void

      This method does not currently have a return value

  • Errors



94
95
96
97
98
# File 'lib/core/config/collection.rb', line 94

def self.set(name, value)
  @@lock.synchronize do
    @@properties[name.to_sym] = value
  end
end