Class: Universals::Universe
- Inherits:
-
Object
- Object
- Universals::Universe
- Includes:
- Enumerable, Singleton
- Defined in:
- lib/universals/universe.rb
Overview
The Universals class includes the Ruby Singleton mixin and provides a single source location to store and retrieve data that is used across the entirety of an application or libraries code base. This avoids having to use global variables or to have to pass data elements around. The class behaves very much like a Hash, mapping keys to values. The class also makes the values stored within it available as properties, so all values stored within a Universe must be stored under String keys with names that conform to standard Ruby method naming criteria.
Instance Method Summary collapse
-
#clear ⇒ Object
This method deletes all entries from a Universe instance.
-
#each(&block) ⇒ Object
Implementation of the each() method for the Universe class.
-
#get_property(key, default = nil) ⇒ Object
(also: #[])
This method fetches the value associated with a given key.
-
#has_property?(key) ⇒ Boolean
(also: #include?)
This method is used to check whether a property is stored under a given key within a Universe object.
-
#initialize ⇒ Universe
constructor
Constructor for the Universe class.
-
#key(value) ⇒ Object
This method fetches the key associated with a value stored in the Universe instance.
-
#keys ⇒ Object
This method fetches a collection of the keys stored within a Universe instance.
-
#method_missing(name, *arguments, &block) ⇒ Object
This method is invoked whenever a method invocation takes place against a Universe object but an existing explicit method cannot be found to meet the call.
-
#respond_to?(name) ⇒ Boolean
This method overrides the default respond_to?() method to provide customer property handling for Universe objects.
-
#set(values = {}) ⇒ Object
This method allows multiple values to be set on the Universe object in a single request.
-
#set_property(key, value) ⇒ Object
(also: #[]=)
This method assigns a value under a specified key within a Universe object.
-
#size ⇒ Object
This method fetches a count of the number of items held within an instance of the Universe class.
-
#values ⇒ Object
This method fetches a collection of the values stored within a Universe instance.
Constructor Details
#initialize ⇒ Universe
Constructor for the Universe class.
25 26 27 |
# File 'lib/universals/universe.rb', line 25 def initialize @values = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *arguments, &block) ⇒ Object
This method is invoked whenever a method invocation takes place against a Universe object but an existing explicit method cannot be found to meet the call.
Parameters
- name
-
The name of the method invoked.
- arguments
-
A collection of the arguments passed to the method call.
- block
-
Any block associated with the method call.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/universals/universe.rb', line 126 def method_missing(name, *arguments, &block) if @values.include?(name.to_s) get_property(name.to_s) elsif name.to_s[-1, 1] == "=" property = name.to_s property = property[0, property.length - 1] set_property(property, arguments[0]) self else super end end |
Instance Method Details
#clear ⇒ Object
This method deletes all entries from a Universe instance.
105 106 107 |
# File 'lib/universals/universe.rb', line 105 def clear @values.clear end |
#each(&block) ⇒ Object
Implementation of the each() method for the Universe class.
73 74 75 |
# File 'lib/universals/universe.rb', line 73 def each(&block) @values.each(&block) end |
#get_property(key, default = nil) ⇒ Object Also known as: []
This method fetches the value associated with a given key.
Parameters
- key
-
The key for the value to be retrieved.
- default
-
The default to return if the key does not exist within the Universe object. Defaults to nil.
44 45 46 47 48 49 50 51 |
# File 'lib/universals/universe.rb', line 44 def get_property(key, default=nil) value = @values.fetch(key, default) if value.respond_to?(:call) value = value.call @values[key] = value end value end |
#has_property?(key) ⇒ Boolean Also known as: include?
This method is used to check whether a property is stored under a given key within a Universe object.
Parameters
- key
-
The key to perform the check for.
34 35 36 |
# File 'lib/universals/universe.rb', line 34 def has_property?(key) @values.include?(key) end |
#key(value) ⇒ Object
This method fetches the key associated with a value stored in the Universe instance.
Parameters
- value
-
The value to retrieve the key for.
88 89 90 |
# File 'lib/universals/universe.rb', line 88 def key(value) @values.key(value) end |
#keys ⇒ Object
This method fetches a collection of the keys stored within a Universe instance.
94 95 96 |
# File 'lib/universals/universe.rb', line 94 def keys @values.keys end |
#respond_to?(name) ⇒ Boolean
This method overrides the default respond_to?() method to provide customer property handling for Universe objects.
Parameters
- name
-
The name of the method to perform the check for.
114 115 116 |
# File 'lib/universals/universe.rb', line 114 def respond_to?(name) @hash.include?(name) || super(name) end |
#set(values = {}) ⇒ Object
This method allows multiple values to be set on the Universe object in a single request.
Parameters
- values
-
A Hash of the values to be set on the object. Keys should be adhere to the standard name requirements for the Universe object or an exception will be raised. Values specified in this way will override values already in the object.
68 69 70 |
# File 'lib/universals/universe.rb', line 68 def set(values={}) values.each {|name, value| set_property(name, value)} end |
#set_property(key, value) ⇒ Object Also known as: []=
This method assigns a value under a specified key within a Universe object. Existing entries will be overwritten by this method.
55 56 57 58 |
# File 'lib/universals/universe.rb', line 55 def set_property(key, value) validate_name(key) @values[key] = value end |
#size ⇒ Object
This method fetches a count of the number of items held within an instance of the Universe class.
79 80 81 |
# File 'lib/universals/universe.rb', line 79 def size @values.size end |
#values ⇒ Object
This method fetches a collection of the values stored within a Universe instance.
100 101 102 |
# File 'lib/universals/universe.rb', line 100 def values @values.values end |