Class: Unit::System
- Inherits:
-
Object
- Object
- Unit::System
- Defined in:
- lib/unit/system.rb
Constant Summary collapse
- SI =
new('SI') do |system| system.load(:si) system.load(:binary) system.load(:degree) system.load(:time) end
Instance Attribute Summary collapse
-
#factor ⇒ Object
readonly
Returns the value of attribute factor.
-
#factor_symbol ⇒ Object
readonly
Returns the value of attribute factor_symbol.
-
#factor_value ⇒ Object
readonly
Returns the value of attribute factor_value.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
-
#unit_symbol ⇒ Object
readonly
Returns the value of attribute unit_symbol.
Instance Method Summary collapse
-
#initialize(name) {|_self| ... } ⇒ System
constructor
A new instance of System.
- #load(input) ⇒ Object
- #parse_unit(expr) ⇒ Object
- #validate_unit(units) ⇒ Object
Constructor Details
#initialize(name) {|_self| ... } ⇒ System
Returns a new instance of System.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/unit/system.rb', line 8 def initialize(name) @name = name @unit = {} @unit_symbol = {} # one is internal trivial factor @factor = {:one => {:symbol => 'one', :value => 1} } @factor_symbol = {'one' => :one} @factor_value = {1 => :one} yield(self) if block_given? end |
Instance Attribute Details
#factor ⇒ Object (readonly)
Returns the value of attribute factor.
6 7 8 |
# File 'lib/unit/system.rb', line 6 def factor @factor end |
#factor_symbol ⇒ Object (readonly)
Returns the value of attribute factor_symbol.
6 7 8 |
# File 'lib/unit/system.rb', line 6 def factor_symbol @factor_symbol end |
#factor_value ⇒ Object (readonly)
Returns the value of attribute factor_value.
6 7 8 |
# File 'lib/unit/system.rb', line 6 def factor_value @factor_value end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/unit/system.rb', line 6 def name @name end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
6 7 8 |
# File 'lib/unit/system.rb', line 6 def unit @unit end |
#unit_symbol ⇒ Object (readonly)
Returns the value of attribute unit_symbol.
6 7 8 |
# File 'lib/unit/system.rb', line 6 def unit_symbol @unit_symbol end |
Instance Method Details
#load(input) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/unit/system.rb', line 21 def load(input) case input when Hash raise "Invalid hash format to load system" unless (input["units"] && input["units"].first.last['def']) || input.first.last['def'] data = input['units'] || input when IO data = YAML.load(input.read) when String if File.exist?(input) data = YAML.load_file(input) else data = YAML.load_file(File.join(File.dirname(__FILE__), 'systems', "#{input}.yml")) end when Symbol data = YAML.load_file(File.join(File.dirname(__FILE__), 'systems', "#{input}.yml")) end (data['factors'] || {}).each do |name, factor| name = name.to_sym symbols = [factor['sym'] || []].flatten factor['def'] =~ /^(\d+)\^(-?\d+)$/ base = $1.to_i exp = $2.to_i value = base ** exp $stderr.puts "Factor #{name} already defined" if @factor[name] @factor[name] = { :symbol => symbols.first, :value => value } symbols.each do |sym| $stderr.puts "Factor symbol #{sym} for #{name} already defined" if @factor_symbol[name] @factor_symbol[sym] = name end @factor_symbol[name.to_s] = @factor_value[value] = name end (data['units'] || {}).each do |name, unit| name = name.to_sym symbols = [unit['sym'] || []].flatten $stderr.puts "Unit #{name} already defined" if @unit[name] @unit[name] = { :symbol => symbols.first, :def => parse_unit(unit['def']) } symbols.each do |sym| $stderr.puts "Unit symbol #{sym} for #{name} already defined" if @unit_symbol[name] @unit_symbol[sym] = name end @unit_symbol[name.to_s] = name end @unit.each {|name, unit| validate_unit(unit[:def]) } true end |
#parse_unit(expr) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/unit/system.rb', line 81 def parse_unit(expr) stack, result, implicit_mul = [], [], false expr.to_s.scan(TOKENIZER).each do |tok| if tok == '(' stack << '(' implicit_mul = false elsif tok == ')' compute(result, stack.pop) while !stack.empty? && stack.last != '(' raise(SyntaxError, 'Unexpected token )') if stack.empty? stack.pop implicit_mul = true elsif OPERATOR.key?(tok) compute(result, stack.pop) while !stack.empty? && stack.last != '(' && OPERATOR[stack.last][1] >= OPERATOR[tok][1] stack << OPERATOR[tok][0] implicit_mul = false else val = case tok when REAL then [[:one, tok.to_f, 1]] when DEC then [[:one, tok.to_i, 1]] when SYMBOL then symbol_to_unit(tok) end stack << '*' if implicit_mul implicit_mul = true result << val end end compute(result, stack.pop) while !stack.empty? result.last end |
#validate_unit(units) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/unit/system.rb', line 71 def validate_unit(units) units.each do |factor, unit, exp| #raise TypeError, 'Factor must be symbol' if !(Symbol === factor) #raise TypeError, 'Unit must be symbol' if !(Numeric === unit || Symbol === unit) #raise TypeError, 'Exponent must be numeric' if !(Numeric === exp) raise TypeError, "Undefined factor #{factor}" if !@factor[factor] raise TypeError, "Undefined unit #{unit}" if !(Numeric === unit || @unit[unit]) end end |