Module: Alchemist
- Defined in:
- lib/alchemist.rb,
lib/alchemist/version.rb,
lib/alchemist/geospatial.rb,
lib/alchemist/measurement.rb,
lib/alchemist/module_builder.rb,
lib/alchemist/conversion_table.rb,
lib/alchemist/compound_measurement.rb,
lib/alchemist/measurement_convertor.rb,
lib/alchemist/objects/planets/earth.rb
Defined Under Namespace
Classes: CompoundMeasurement, ConversionTable, Earth, Measurement, MeasurementConvertor, ModuleBuilder
Constant Summary
collapse
- DATA_DIR =
File.join(File.dirname(__FILE__), "alchemist", "data")
- DEFAULT_UNITS_FILE =
File.join(DATA_DIR, "units.yml")
- DEFAULT_BINARY_PREFIXES_FILE =
File.join(DATA_DIR, "binary_prefixes.yml")
- DEFAULT_SI_UNITS_FILE =
File.join(DATA_DIR, "si_units.yml")
- DEFAULT_UNIT_PREFIXES_FILE =
File.join(DATA_DIR, "unit_prefixes.yml")
- VERSION =
"0.1.6"
Class Method Summary
collapse
Class Method Details
.binary_prefixes ⇒ Object
69
70
71
|
# File 'lib/alchemist.rb', line 69
def self.binary_prefixes
@binary_prefixes ||= YAML.load_file(DEFAULT_BINARY_PREFIXES_FILE)
end
|
.conversion_table ⇒ Object
93
94
95
|
# File 'lib/alchemist.rb', line 93
def self.conversion_table
@conversion_table ||= load_conversion_table
end
|
.conversions ⇒ Object
97
98
99
|
# File 'lib/alchemist.rb', line 97
def self.conversions
@conversions ||= load_conversions
end
|
.has_measurement?(name) ⇒ Boolean
28
29
30
|
# File 'lib/alchemist.rb', line 28
def self.has_measurement? name
conversions.keys.include? name.to_sym
end
|
.load_conversion_table(filename = DEFAULT_UNITS_FILE) ⇒ Object
89
90
91
|
# File 'lib/alchemist.rb', line 89
def self.load_conversion_table(filename=DEFAULT_UNITS_FILE)
@conversion_table = ConversionTable.new.load_all(filename)
end
|
.measure(value, unit, exponent = 1.0) ⇒ Object
24
25
26
|
# File 'lib/alchemist.rb', line 24
def self.measure value, unit, exponent = 1.0
Measurement.new value, unit, exponent
end
|
.measurement_for(name) ⇒ Object
32
33
34
|
# File 'lib/alchemist.rb', line 32
def self.measurement_for name
conversions[ name.to_sym ]
end
|
.operator_actions ⇒ Object
65
66
67
|
# File 'lib/alchemist.rb', line 65
def self.operator_actions
@operator_actions ||= {}
end
|
.parse_prefix(unit) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/alchemist.rb', line 48
def self.parse_prefix(unit)
matches = unit.to_s.match(prefix_matcher)
prefix, parsed_unit = matches.captures
if prefix && si_units.include?(parsed_unit)
value = prefixed_value_for(prefix.to_sym, parsed_unit)
[value, parsed_unit.to_sym]
else
[1, unit]
end
end
|
.register(type, names, value) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/alchemist.rb', line 36
def self.register(type, names, value)
names = Array(names)
value = value.is_a?(Measurement) ? value.base(type) : value
Alchemist.conversion_table[type] ||= {}
names.each do |name|
conversions[name] ||= []
conversions[name] << type
Alchemist.conversion_table[type][name] = value
end
end
|
.register_operation_conversions(type, other_type, operation, converted_type) ⇒ Object
60
61
62
63
|
# File 'lib/alchemist.rb', line 60
def self.register_operation_conversions type, other_type, operation, converted_type
operator_actions[operation] ||= []
operator_actions[operation] << [type, other_type, converted_type]
end
|
.setup(category = nil) ⇒ Object
16
17
18
19
20
21
22
|
# File 'lib/alchemist.rb', line 16
def self.setup category = nil
if category
Numeric.send(:include, ModuleBuilder.new(category).build)
else
conversion_table.keys.each { |category| Numeric.send(:include, ModuleBuilder.new(category).build) }
end
end
|
.si_units ⇒ Object
73
74
75
|
# File 'lib/alchemist.rb', line 73
def self.si_units
@si_units ||= YAML.load_file(DEFAULT_SI_UNITS_FILE)
end
|
.unit_prefixes ⇒ Object
77
78
79
|
# File 'lib/alchemist.rb', line 77
def self.unit_prefixes
@unit_prefixes ||= YAML.load_file(DEFAULT_UNIT_PREFIXES_FILE)
end
|
.use_si ⇒ Object
81
82
83
|
# File 'lib/alchemist.rb', line 81
def self.use_si
@use_si ||= false
end
|
.use_si=(use_si) ⇒ Object
85
86
87
|
# File 'lib/alchemist.rb', line 85
def self.use_si= use_si
@use_si = use_si
end
|