Class: MeasureScaler::Unit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string_or_hash) ⇒ Unit

Returns a new instance of Unit.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/measure_scaler/unit.rb', line 4

def initialize(string_or_hash)
  case string_or_hash
  when String
    decode_string(string_or_hash)
  when Hash
    h = {direction: 1, preunit: nil}.merge(string_or_hash)
    @prefix    = Prefix.new(h[:prefix])
    @preunit   = h[:preunit]
    @unit      = h[:unit]
    @direction = h[:direction]

  else
    raise "attribute not valid"
  end
end

Instance Attribute Details

#directionObject (readonly)

Returns the value of attribute direction.



3
4
5
# File 'lib/measure_scaler/unit.rb', line 3

def direction
  @direction
end

#prefixObject (readonly)

Returns the value of attribute prefix.



3
4
5
# File 'lib/measure_scaler/unit.rb', line 3

def prefix
  @prefix
end

#preunitObject (readonly)

Returns the value of attribute preunit.



3
4
5
# File 'lib/measure_scaler/unit.rb', line 3

def preunit
  @preunit
end

#unitObject (readonly)

Returns the value of attribute unit.



3
4
5
# File 'lib/measure_scaler/unit.rb', line 3

def unit
  @unit
end

Instance Method Details

#pattern_found?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/measure_scaler/unit.rb', line 21

def pattern_found?
  !@prefix.nil?
end

#scale(order) ⇒ Object

order is a multiple of 3



25
26
27
28
29
30
31
32
33
34
# File 'lib/measure_scaler/unit.rb', line 25

def scale(order) # order is a multiple of 3
  definitive_order, new_prefix = @prefix.scale(order*@direction)
  [ definitive_order*@direction, 
    Unit.new(
      prefix: new_prefix, 
      preunit: @preunit, 
      unit: @unit, 
      direction: @direction)
  ]
end

#to_s(prefix = nil) ⇒ Object



36
37
38
# File 'lib/measure_scaler/unit.rb', line 36

def to_s(prefix=nil)
  "#{@preunit}#{prefix||@prefix}#{@unit}"
end