class Paggio::CSS::Unit

Constants

COMPATIBLE
TYPES

Attributes

number[R]
type[R]

Public Class Methods

new(number, type) click to toggle source
# File lib/paggio/css/unit.rb, line 19
def initialize(number, type)
  @number = number
  @type   = type
end

Public Instance Methods

*(other) click to toggle source
# File lib/paggio/css/unit.rb, line 84
def *(other)
  return Unit.new(@number * other, @type) unless Unit === other

  if @type == other.type
    Unit.new(@number * other.number, @type)
  elsif compatible?(self) and compatible?(other)
    Unit.new(@number * convert(other, @type), @type)
  else
    raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
  end
end
+(other) click to toggle source
# File lib/paggio/css/unit.rb, line 60
def +(other)
  return Unit.new(@number + other, @type) unless Unit === other

  if @type == other.type
    Unit.new(@number + other.number, @type)
  elsif compatible?(self) and compatible?(other)
    Unit.new(@number + convert(other, @type), @type)
  else
    raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
  end
end
+@() click to toggle source
# File lib/paggio/css/unit.rb, line 112
def +@
  Unit.new(@number, @type)
end
-(other) click to toggle source
# File lib/paggio/css/unit.rb, line 72
def -(other)
  return Unit.new(@number - other, @type) unless Unit === other

  if @type == other.type
    Unit.new(@number - other.number, @type)
  elsif compatible?(self) and compatible?(other)
    Unit.new(@number - convert(other, @type), @type)
  else
    raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
  end
end
-@() click to toggle source
# File lib/paggio/css/unit.rb, line 108
def -@
  Unit.new(@number * -1, @type)
end
/(other) click to toggle source
# File lib/paggio/css/unit.rb, line 96
def /(other)
  return Unit.new(@number / other, @type) unless Unit === other

  if @type == other.type
    Unit.new(@number / other.number, @type)
  elsif compatible?(self) and compatible?(other)
    Unit.new(@number / convert(other, @type), @type)
  else
    raise ArgumentError, "#{other.type} isn't compatible with #{@type}"
  end
end
==(other) click to toggle source
# File lib/paggio/css/unit.rb, line 28
def ==(other)
  unless Unit === other
    unless other.respond_to? :to_u
      raise TypeError, "no implicit conversion of #{other.class} into Unit"
    end

    other = other.to_u
  end

  unless Unit === other
    other = Unit.new(other, @type)
  end

  @number == convert(other, @type)
end
Also aliased as: eql?
===(other) click to toggle source
# File lib/paggio/css/unit.rb, line 44
def ===(other)
  @type == other.type && @number == other.number
end
coerce(other) click to toggle source
# File lib/paggio/css/unit.rb, line 24
def coerce(other)
  return self, other
end
eql?(other)
Alias for: ==
hash() click to toggle source
# File lib/paggio/css/unit.rb, line 50
def hash
  [@number, @type].hash
end
inspect()
Alias for: to_s
to_f() click to toggle source
# File lib/paggio/css/unit.rb, line 120
def to_f
  @number.to_f
end
to_i() click to toggle source
# File lib/paggio/css/unit.rb, line 116
def to_i
  @number.to_i
end
to_s() click to toggle source
# File lib/paggio/css/unit.rb, line 128
def to_s
  "#@number#@type"
end
Also aliased as: to_str, inspect
to_str()
Alias for: to_s
to_u() click to toggle source
# File lib/paggio/css/unit.rb, line 124
def to_u
  self
end

Private Instance Methods

compatible?(unit) click to toggle source
# File lib/paggio/css/unit.rb, line 136
def compatible?(unit)
  COMPATIBLE.include?(unit.type)
end
convert(unit, type) click to toggle source
# File lib/paggio/css/unit.rb, line 140
def convert(unit, type)
  value = unit.number

  return value if unit.type == type

  px = case unit.type
  when :in then value * 96
  when :pt then value * 4.0 / 3.0
  when :pc then value / 12 * 4.0 / 3.0
  when :mm then value * 3.77953
  when :cm then value * 10 * 3.77953
  when :px then value
  end

  case type
  when :in then px / 96.0
  when :pt then px / 4.0 / 3.0
  when :pc then px * 12 / 4.0 / 3.0
  when :mm then px / 3.77953
  when :cm then px / 10 / 3.77953
  when :px then px
  end
end