Class: Unity::Lookup

Inherits:
Object
  • Object
show all
Defined in:
lib/unity/lookup.rb

Defined Under Namespace

Classes: Duplicate, Invalid, Undefined

Class Method Summary collapse

Class Method Details

.add(object) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
# File 'lib/unity/lookup.rb', line 22

def add object
  raise Invalid, object.errors.inspect unless object.valid?
  raise Duplicate, "The Unit #{object.name} already exists" unless @@unit_names[object.name.to_sym].nil?
  key = store_object object
  @@unit_names[object.name.to_sym] = key
  @@unit_ints[object.dimension_int] ||= []
  @@unit_ints[object.dimension_int] << key
end

.add_property(object) ⇒ Object

Raises:



59
60
61
62
63
64
65
66
# File 'lib/unity/lookup.rb', line 59

def add_property object
  raise Invalid, object.errors.inspect unless object.valid?
  existing = @@property_ints[object.dimension_int]
  raise Duplicate, "The property for dimension int #{object.dimension_int} is already defined as #{get_object(existing).name}" unless existing.nil?
  key = store_object object
  @@property_names[object.name.to_sym] = key 
  @@property_ints[object.dimension_int]= key
end

.clear!Object



72
73
74
75
76
77
78
# File 'lib/unity/lookup.rb', line 72

def clear!
  @@object_cache = {}
  @@unit_names = Hash.new
  @@unit_ints = Hash.new
  @@property_ints = Hash.new
  @@property_names= Hash.new
end

.compatible_units(int_or_sym_or_string) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/unity/lookup.rb', line 36

def compatible_units int_or_sym_or_string
  int =
    if int_or_sym_or_string.is_a? Integer
      int_or_sym_or_string
    else
      tmp = find_property(int_or_sym_or_string)
      tmp.nil? ? nil : tmp.dimension_int
    end
  (@@unit_ints[int] || []).map do |key|
    get_object key
  end
end

.find!(name) ⇒ Object



31
32
33
34
# File 'lib/unity/lookup.rb', line 31

def find! name
  key = @@unit_names[name.to_sym].tap {|k| raise Undefined, "Unit #{name} is undefined" unless k }
  get_object key
end

.find_property(int_or_sym_or_string) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/unity/lookup.rb', line 49

def find_property int_or_sym_or_string
  key =
    if int_or_sym_or_string.is_a? Integer
      @@property_ints[int_or_sym_or_string]
    else
      @@property_names[int_or_sym_or_string.to_sym]
    end.tap {|k| return nil unless k }
    get_object key
end

.property_namesObject



68
69
70
# File 'lib/unity/lookup.rb', line 68

def property_names
  @@property_names.keys.map(&:to_sym)
end

.reload!Object



80
81
82
# File 'lib/unity/lookup.rb', line 80

def reload!
  load "#{File.dirname(__FILE__)}/lookup/definitions.rb"
end