class Settings::Storage
Attributes
Subnodes array (childs) for current node. Can be empty.
Name for node. Root node has 'root' name.
Values array (storage) for methods registered in the node.
Public Class Methods
Create new storage.
Get hash and try to register each key as method or even sub-storage (it needs for nested hashes). Some keys are not allowed for method registration (f.e. when key = :42). All limitation about Ruby methods naming convention. You can get access to method with any name throw [] thought.
WARNING! Storage
is case-sensitive! It means that keys :config and :Config are two different keys!
Sample:
hsh = { cinema: 'tiger',
rate: {3: 'excellent', 2: 'good', 1: 'overage', 0: 'not rated'}, watched: false, 'premier' 'soon' }
s = Storage.new(hsh)
s.cinema # => 'tiger' s.rate # => 'not rated' s.watched? # => false s # => soon
# File lib/configurates/storage.rb, line 37 def self.create(hsh) new hsh end
Initialization. Additional param “#{name}” is used only for nested storage naming.
# File lib/configurates/storage.rb, line 72 def initialize(hsh, name = nil) set_initial_values name fill_in_storage hsh end
Public Instance Methods
Alternative way to get stored value.
How it can be used:
conf[:raise_on_error] # => true conf.load # => :yes
Method will raise an ValueNotFound
exception if nothing was found.
# File lib/configurates/storage.rb, line 51 def [](arg) return @storage[arg] unless @storage[arg].nil? chaild = find_child_for_response(arg) return chaild unless chaild.nil? msg = "Variable with name '#{arg}' not found!" raise ValueNotFound, msg end
NOT IMPLEMENTED!
# File lib/configurates/storage.rb, line 62 def merge(_hsh) raise StandardError, 'not implemented!' end
Private Instance Methods
For each pair “key: value” in #{“input_hsh”} we need to find a place where pairs can be stored. So, pairs with values === Hash is used to create new storages, other pairs will be stored in current storage.
# File lib/configurates/storage.rb, line 98 def fill_in_storage(input_hsh) tmp = input_hsh.dup tmp.each_pair do |key, value| case value when Hash @childs << Storage.new(value, key) else @storage[key] = value register_methods_for key if key_can_be_a_method?(key) end end end
Look for nested node with the “#{name}”. Since we working with hash we can believe that only one nested storage can be found (equal names disallowed).
# File lib/configurates/storage.rb, line 162 def find_child_for_response(name) @childs.select { |chaild| chaild.name == name }.first end
Unfortunately some names are not allowed to be registered as methods. With key_can_be_a_method we should determinate is it possible to define a method with “#{key}” name or not.
# File lib/configurates/storage.rb, line 116 def key_can_be_a_method?(key) key_as_string_valid_integer = key.to_s.to_i.to_s == key.to_s key.is_a?(Integer) || key_as_string_valid_integer ? false : true end
Redirect message to childen node if this node can response to the message otherwise try to responce itself
# File lib/configurates/storage.rb, line 144 def method_missing(method, *args, &blk) child = find_child_for_response(method) child.nil? ? super : child end
Register method with method_name.
For TrueClass or FalseClass === @storage it will define additional predicate method, like:
config.skip_lazy_load? # => true
# File lib/configurates/storage.rb, line 129 def register_methods_for(method_name) define_singleton_method(method_name.to_sym) do @storage[method_name] end return unless [true, false].include? @storage[method_name] define_singleton_method("#{method_name.to_sym}?") do @storage[method_name] end end
Return TRUE if storage or any childen node can response to the message otherwise return FALSE
# File lib/configurates/storage.rb, line 153 def respond_to?(method, include_private = false) super || find_child_for_response(method) ? true : false end
# File lib/configurates/storage.rb, line 87 def set_initial_values(name = nil) @name ||= name || 'root' @childs ||= [] @storage ||= {} end
Slightly better way to tell about storage.
# File lib/configurates/storage.rb, line 80 def to_ary [nodes: @childs.map(&:name), variables: @storage.to_h] end