Class: Tins::Unit::UnitParser
- Defined in:
- lib/tins/unit.rb
Constant Summary collapse
- NUMBER =
/([+-]? (?:0|[1-9]\d*) (?: \.\d+(?i:e[+-]?\d+) | \.\d+ | (?i:e[+-]?\d+) )? )/x
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Instance Method Summary collapse
-
#initialize(source, unit, prefixes = nil) ⇒ UnitParser
constructor
A new instance of UnitParser.
- #parse ⇒ Object
- #scan(re) ⇒ Object
- #scan_char(char) ⇒ Object
- #scan_number ⇒ Object
- #scan_unit ⇒ Object
Constructor Details
#initialize(source, unit, prefixes = nil) ⇒ UnitParser
Returns a new instance of UnitParser.
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tins/unit.rb', line 62 def initialize(source, unit, prefixes = nil) super source if prefixes @unit_re = unit_re(Tins::Unit.prefixes(prefixes), unit) @unit_lc_re = @unit_uc_re = nil else @unit_lc_re = unit_re(Tins::Unit.prefixes(:lc), unit) @unit_uc_re = unit_re(Tins::Unit.prefixes(:uc), unit) @unit_re = nil end @number = 1.0 end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
87 88 89 |
# File 'lib/tins/unit.rb', line 87 def number @number end |
Instance Method Details
#parse ⇒ Object
117 118 119 |
# File 'lib/tins/unit.rb', line 117 def parse raise NotImplementedError end |
#scan(re) ⇒ Object
89 90 91 92 |
# File 'lib/tins/unit.rb', line 89 def scan(re) re.nil? and return super end |
#scan_char(char) ⇒ Object
113 114 115 |
# File 'lib/tins/unit.rb', line 113 def scan_char(char) scan(/#{char}/) or return end |
#scan_number ⇒ Object
94 95 96 97 |
# File 'lib/tins/unit.rb', line 94 def scan_number scan(NUMBER) or return @number *= BigDecimal(self[1]) end |
#scan_unit ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tins/unit.rb', line 99 def scan_unit case when scan(@unit_re) prefix = @unit_re.prefixes.find { |pre| pre.name == self[1] } or return @number *= prefix.multiplier when scan(@unit_lc_re) prefix = @unit_lc_re.prefixes.find { |pre| pre.name == self[1] } or return @number *= prefix.multiplier when scan(@unit_uc_re) prefix = @unit_uc_re.prefixes.find { |pre| pre.name == self[1] } or return @number *= prefix.multiplier end end |