Module: Alchemist

Defined in:
lib/alchemist.rb,
lib/alchemist/numeric_ext.rb,
lib/alchemist/unit_prefixes.rb,
lib/alchemist/conversion_table.rb,
lib/alchemist/numeric_conversion.rb,
lib/alchemist/compound_numeric_conversion.rb

Defined Under Namespace

Modules: ConversionTable, NumericExt, UnitPrefixes Classes: CompoundNumericConversion, NumericConversion

Constant Summary collapse

Conversions =
{}
@@si_units =
%w[m meter metre meters metres liter litre litres liters l L farad farads F coulombs C gray grays Gy siemen siemens S mhos mho ohm ohms volt volts V ]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.use_siObject

Returns the value of attribute use_si.



12
13
14
# File 'lib/alchemist.rb', line 12

def use_si
  @use_si
end

Class Method Details

.base(unit) ⇒ Object



90
91
92
# File 'lib/alchemist.rb', line 90

def self.base(unit)
  parse_prefix(unit).last
end

.conversion_tableObject



24
25
26
# File 'lib/alchemist.rb', line 24

def self.conversion_table
  @@conversion_table ||= ConversionTable.default
end

.convertable?(*units) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/alchemist.rb', line 42

def self.convertable?(*units)
  raise ArgumentError.new('You need specify at least two units') if units.length < 2

  units.each do |unit_a|
    type_a = type(unit_a)
    units.each do |unit_b|
      return false if type_a != type(unit_b)
    end
  end

  true
end

.convertable_units(unit) ⇒ Object



37
38
39
40
# File 'lib/alchemist.rb', line 37

def self.convertable_units(unit)
  list = conversion_table[type(unit)]
  list ? list.keys - [unit] : nil
end

.operator_actionsObject



28
29
30
# File 'lib/alchemist.rb', line 28

def self.operator_actions
  @@operator_actions ||= {}
end

.parse_prefix(unit) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/alchemist.rb', line 78

def self.parse_prefix(unit)
  unit_prefixes.each do |prefix, value|
    if unit.to_s =~ /^#{prefix}.+/ && @@si_units.include?(unit.to_s.gsub(/^#{prefix}/,''))        
      if !(Conversions[ unit.to_s.gsub(/^#{prefix}/,'').to_sym ] & [ :information_storage ]).empty? && !@use_si && value >= 1000.0 && value.to_i & -value.to_i != value
        value = 2 ** (10 * (Math.log(value) / Math.log(10)) / 3)
      end
      return [value, unit.to_s.gsub(/^#{prefix}/,'').to_sym]
    end
  end
  [1.0, unit]
end

.register(type, names, value) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/alchemist.rb', line 62

def self.register(type, names, value)
  names = [names] unless names.is_a?(Array)
  value = value.is_a?(NumericConversion) ? 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



73
74
75
76
# File 'lib/alchemist.rb', line 73

def self.register_operation_conversions type, other_type, operation, converted_type
  operator_actions[operation] ||= []
   operator_actions[operation] << [type, other_type, converted_type]
end

.type(measurement) ⇒ Object



32
33
34
35
# File 'lib/alchemist.rb', line 32

def self.type(measurement)
  convos = Conversions[base(measurement)]
  convos ? convos.first : nil
end

.unit_prefixesObject



20
21
22
# File 'lib/alchemist.rb', line 20

def self.unit_prefixes
  @@unit_prefixes ||= UnitPrefixes.default
end