Class: Rulerr::Conversion

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

Constant Summary collapse

DEFAULT_UNIT =
'cm'
FACTOR_BY_UNIT =
{
  'm'  => 1.0,
  'cm' => 100.0,
  'mm' => 1000.0,
  'px' => 3779.5275591,
  'pt' => 2834.64567,
  'in' => 39.37
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Conversion

Returns a new instance of Conversion.



17
18
19
20
21
22
23
# File 'lib/rulerr.rb', line 17

def initialize(value)
  @number = value.to_f
  @unit   = (
    value.match(/[a-z]{1,2}$/i).to_a[0] ||
    DEFAULT_UNIT
  ).downcase
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



15
16
17
# File 'lib/rulerr.rb', line 15

def number
  @number
end

#unitObject (readonly)

Returns the value of attribute unit.



15
16
17
# File 'lib/rulerr.rb', line 15

def unit
  @unit
end

Instance Method Details

#to_cmObject



30
31
32
# File 'lib/rulerr.rb', line 30

def to_cm
  to_m * FACTOR_BY_UNIT['cm']
end

#to_inObject



42
43
44
# File 'lib/rulerr.rb', line 42

def to_in
  to_m * FACTOR_BY_UNIT['in']
end

#to_mObject



34
35
36
# File 'lib/rulerr.rb', line 34

def to_m
  number / FACTOR_BY_UNIT[unit]
end

#to_mmObject

eg: “23.45cm” => “234.5mm”



26
27
28
# File 'lib/rulerr.rb', line 26

def to_mm
  to_m * FACTOR_BY_UNIT['mm']
end

#to_ptObject



46
47
48
# File 'lib/rulerr.rb', line 46

def to_pt
  to_m * FACTOR_BY_UNIT['pt']
end

#to_pxObject



38
39
40
# File 'lib/rulerr.rb', line 38

def to_px
  to_m * FACTOR_BY_UNIT['px']
end