Class: Settings::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/configurates/storage.rb

Defined Under Namespace

Classes: ValueNotFound

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object (private)

Redirect message to childen node if this node can response to the message otherwise try to responce itself



144
145
146
147
# File 'lib/configurates/storage.rb', line 144

def method_missing(method, *args, &blk)
  child = find_child_for_response(method)
  child.nil? ? super : child
end

Class Method Details

.create(hsh) ⇒ Object

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



37
38
39
# File 'lib/configurates/storage.rb', line 37

def self.create(hsh)
  new hsh
end

Instance Method Details

#[](arg) ⇒ Object

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.

Raises:



51
52
53
54
55
56
57
# 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

#merge(_hsh) ⇒ Object

NOT IMPLEMENTED!

Raises:

  • (StandardError)


62
63
64
# File 'lib/configurates/storage.rb', line 62

def merge(_hsh)
 raise StandardError, 'not implemented!'
end