Class: Unite::Lookup
- Inherits:
-
Object
show all
- Defined in:
- lib/unite/lookup.rb
Defined Under Namespace
Classes: Duplicate, Invalid, Undefined
Class Method Summary
collapse
Class Method Details
.add(object) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/unite/lookup.rb', line 22
def add object
raise Invalid, object.errors.inspect unless object.valid?
names = [object.name] + object.aliases
names.each do |name|
raise Duplicate, "The Unit #{name} already exists" unless @@unit_names[name.to_sym].nil?
end
key = store_object object
names.each do |name|
@@unit_names[name.to_sym] = key
end
@@unit_ints[object.dimension_int] ||= []
@@unit_ints[object.dimension_int] << key
end
|
.add_property(object) ⇒ Object
69
70
71
72
73
74
75
76
|
# File 'lib/unite/lookup.rb', line 69
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
82
83
84
85
86
87
88
|
# File 'lib/unite/lookup.rb', line 82
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
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/unite/lookup.rb', line 46
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
41
42
43
44
|
# File 'lib/unite/lookup.rb', line 41
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
59
60
61
62
63
64
65
66
67
|
# File 'lib/unite/lookup.rb', line 59
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_names ⇒ Object
78
79
80
|
# File 'lib/unite/lookup.rb', line 78
def property_names
@@property_names.keys.map(&:to_sym)
end
|
.reload! ⇒ Object
90
91
92
|
# File 'lib/unite/lookup.rb', line 90
def reload!
load "#{File.dirname(__FILE__)}/lookup/definitions.rb"
end
|