Class: NumericWithUnit::Unit::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/numeric_with_unit/unit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
15
16
17
18
# File 'lib/numeric_with_unit/unit.rb', line 10

def initialize(parent=nil)
  @symbol = nil
  @dimension = Hash.new(0)
  @from_si = nil
  @to_si = nil
  @si = false
  
  @parent = parent
end

Instance Attribute Details

#derivationObject (readonly)

Returns the value of attribute derivation.



7
8
9
# File 'lib/numeric_with_unit/unit.rb', line 7

def derivation
  @derivation
end

#dimensionObject

Returns the value of attribute dimension.



7
8
9
# File 'lib/numeric_with_unit/unit.rb', line 7

def dimension
  @dimension
end

#siObject

Returns the value of attribute si.



8
9
10
# File 'lib/numeric_with_unit/unit.rb', line 8

def si
  @si
end

#symbolObject

Returns the value of attribute symbol.



7
8
9
# File 'lib/numeric_with_unit/unit.rb', line 7

def symbol
  @symbol
end

Instance Method Details

#compileObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/numeric_with_unit/unit.rb', line 20

def compile
  @dimension.delete_if{|k,v| v.zero?}
  
  @from_si ||= ->(x){x}
  @to_si ||= ->(x){x}

  @derivation = Hash.new(0)
  @derivation[@parent] += 1 unless @parent.nil?
  
  self
end

#from_si(&block) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/numeric_with_unit/unit.rb', line 46

def from_si(&block)
  if block_given?
    @from_si = block
  else
    @from_si
  end
end

#from_si=(arg) ⇒ Object



41
42
43
44
# File 'lib/numeric_with_unit/unit.rb', line 41

def from_si=(arg)
  raise unless arg.is_a?(Proc)
  @from_si = arg
end

#to_si(&block) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/numeric_with_unit/unit.rb', line 59

def to_si(&block)
  if block_given?
    @to_si = block
  else
    @to_si
  end
end

#to_si=(arg) ⇒ Object



54
55
56
57
# File 'lib/numeric_with_unit/unit.rb', line 54

def to_si=(arg)
  raise unless arg.is_a?(Proc)
  @to_si = arg
end