Class: Dimensional::System

Inherits:
String
  • Object
show all
Defined in:
lib/dimensional/system.rb

Overview

Represents a set of units for comprehensive measurement of physical quantities.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, abbreviation = nil, description = nil) ⇒ System

Returns a new instance of System.



37
38
39
40
41
# File 'lib/dimensional/system.rb', line 37

def initialize(name, abbreviation = nil, description = nil)
  @abbreviation = abbreviation && abbreviation.to_s
  @description = description || name
  super(name)
end

Instance Attribute Details

#abbreviationObject (readonly)

Returns the value of attribute abbreviation.



35
36
37
# File 'lib/dimensional/system.rb', line 35

def abbreviation
  @abbreviation
end

#descriptionObject (readonly)

Returns the value of attribute description.



35
36
37
# File 'lib/dimensional/system.rb', line 35

def description
  @description
end

Class Method Details

.[](sym) ⇒ Object

Lookup the system by name or abbreviation



21
22
23
24
25
26
# File 'lib/dimensional/system.rb', line 21

def self.[](sym)
  sym = sym && sym.to_sym
  s = @abbreviation_registry[sym] || @registry[sym]
  raise "Unknown system #{sym.to_s}" unless s
  s
end

.register(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/dimensional/system.rb', line 10

def self.register(*args)
  s = new(*args)
  raise "System #{s} already exists" if @registry[s.to_sym]
  raise "System #{s}'s abbreviation already exists" if @abbreviation_registry[s.abbreviation]
  @registry[s.to_sym] = s
  @abbreviation_registry[s.abbreviation.to_sym] = s if s.abbreviation
  const_set(s.abbreviation, s) rescue nil # Not all symbols strings are valid constant names
  s
end

.reset!Object

Purge all systems from storage.



29
30
31
32
33
# File 'lib/dimensional/system.rb', line 29

def self.reset!
  constants.each {|d| remove_const(d)}
  @registry.clear
  @abbreviation_registry.clear
end