Class: Unit::System
- Inherits:
-
Object
- Object
- Unit::System
- Defined in:
- lib/unit/system.rb
Constant Summary
Instance Attribute Summary (collapse)
-
- (Object) factor
readonly
Returns the value of attribute factor.
-
- (Object) factor_symbol
readonly
Returns the value of attribute factor_symbol.
-
- (Object) factor_value
readonly
Returns the value of attribute factor_value.
-
- (Object) name
readonly
Returns the value of attribute name.
-
- (Object) unit
readonly
Returns the value of attribute unit.
-
- (Object) unit_symbol
readonly
Returns the value of attribute unit_symbol.
Instance Method Summary (collapse)
-
- (System) initialize(name) {|_self| ... }
constructor
A new instance of System.
- - (Object) load(input)
- - (Object) parse_unit(expr)
- - (Object) validate_unit(units)
Constructor Details
- (System) initialize(name) {|_self| ... }
A new instance of System
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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} @loaded_systems = [] @loaded_filenames = [] yield(self) if block_given? end |
Instance Attribute Details
- (Object) factor (readonly)
Returns the value of attribute factor
6 7 8 |
# File 'lib/unit/system.rb', line 6 def factor @factor end |
- (Object) factor_symbol (readonly)
Returns the value of attribute factor_symbol
6 7 8 |
# File 'lib/unit/system.rb', line 6 def factor_symbol @factor_symbol end |
- (Object) factor_value (readonly)
Returns the value of attribute factor_value
6 7 8 |
# File 'lib/unit/system.rb', line 6 def factor_value @factor_value end |
- (Object) name (readonly)
Returns the value of attribute name
6 7 8 |
# File 'lib/unit/system.rb', line 6 def name @name end |
- (Object) unit (readonly)
Returns the value of attribute unit
6 7 8 |
# File 'lib/unit/system.rb', line 6 def unit @unit end |
- (Object) unit_symbol (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
- (Object) load(input)
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 |
# File 'lib/unit/system.rb', line 24 def load(input) case input when Hash data = input when IO data = YAML.load(input.read) when String if File.exist?(input) return if @loaded_filenames.include?(input) data = YAML.load_file(input) @loaded_filenames << input else load(input.to_sym) return end when Symbol return if @loaded_systems.include?(input) data = YAML.load_file(File.join(File.dirname(__FILE__), 'systems', "#{input}.yml")) @loaded_systems << input end load_factors(data['factors']) if data['factors'] load_units(data['units']) if data['units'] @unit.each {|name, unit| validate_unit(unit[:def]) } true end |
- (Object) parse_unit(expr)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/unit/system.rb', line 63 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 |
- (Object) validate_unit(units)
53 54 55 56 57 58 59 60 61 |
# File 'lib/unit/system.rb', line 53 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 |