Class: Universals::Universe

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeUniverse

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

#clearObject

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.

Returns:

  • (Boolean)


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

#keysObject

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.

Returns:

  • (Boolean)


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

#sizeObject

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

#valuesObject

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