class TypographicUnit::Unit

This is a base class for all unit classes.

Attributes

value[R]

Public Class Methods

base() click to toggle source

Return base unit class.

@return [Class]

base unit class
# File lib/typographic-unit/unit.rb, line 39
def base
  @base
end
new(value) click to toggle source
# File lib/typographic-unit/unit.rb, line 54
def initialize(value)
  unless value.kind_of?(Numeric) and not(value.kind_of?(Unit))
    raise ArgumentError.new, value
  end
  @value = value
end
short() click to toggle source

Return short name of the unit.

@return [Symbol]

short name of the unit
# File lib/typographic-unit/unit.rb, line 31
def short
  @short
end
size() click to toggle source

Return size of the unit.

@return [Rational]

size of the unit
# File lib/typographic-unit/unit.rb, line 47
def size
  @size
end
unit(short, base, size) click to toggle source

Define a unit. This method is used in concrete unit classes.

@param short [Symbol]

short name for the unit

@param base [Unit]

base unit class

@param size [Rational]

unit size

@return [void]

# File lib/typographic-unit/unit.rb, line 20
def unit(short, base, size)
  @short = short
  @base = base
  @size = size
  register(short, self)
end

Private Class Methods

register(short, unit) click to toggle source

@api private

# File lib/typographic-unit/unit.rb, line 6
def register(short, unit)
  TypographicUnit::TABLE[short] = unit
end

Public Instance Methods

%(other) click to toggle source

Same as +Number#%+.

# File lib/typographic-unit/unit.rb, line 89
def %(other)
  case other
  when Unit
    self.class.new(@value % (other >> self.class).value)
  when Integer, Float
    self.class.new(@value % other)
  else
    raise ArgumentError, other
  end
end
*(other) click to toggle source

Same as +Number#*+.

# File lib/typographic-unit/unit.rb, line 166
def *(other)
  raise ArgumentError, other unless other.kind_of?(Integer) or other.kind_of?(Float)
  self.class.new(@value * other)
end
+(other) click to toggle source

Perform addition.

@param other [Unit]

additional value

@return [Unit]

new value of the unit

@example

1.cm + 1.mm #=> 1.1.cm
# File lib/typographic-unit/unit.rb, line 147
def +(other)
  raise ArgumentError, other unless other.kind_of?(Unit)
  self.class.new((@value + (other >> self.class).value).to_f)
end
+@() click to toggle source

Same as Number#+@.

# File lib/typographic-unit/unit.rb, line 125
def +@
  self.class.new(+@value)
end
-(other) click to toggle source

Perform subtraction.

@param other [Unit]

subtractional value

@return [Unit]

new value of the unit

@example

1.cm - 1.mm #=> 0.9.cm
# File lib/typographic-unit/unit.rb, line 160
def -(other)
  raise ArgumentError, other unless other.kind_of?(Unit)
  self.class.new((@value - (other >> self.class).value).to_f)
end
-@() click to toggle source

Same as +Number#-@+.

# File lib/typographic-unit/unit.rb, line 130
def -@
  self.class.new(-@value)
end
<=>(other) click to toggle source

@api private

# File lib/typographic-unit/unit.rb, line 71
def <=>(other)
  raise ArgumentError, other unless other.kind_of?(Unit)
  self.scaled_point.value.to_i <=> other.scaled_point.value.to_i
end
>>(other) click to toggle source

Convert the length into other unit.

@param other [Unit]

target unit
# File lib/typographic-unit/unit.rb, line 80
def >>(other)
  oclass = other.kind_of?(Symbol) ? TABLE[other] : other
  u = oclass.new(1)
  oclass.new((self.scaled_point.value / u.scaled_point.value).to_f)
end
Also aliased as: convert
abs() click to toggle source

Same as +Number#abs+.

# File lib/typographic-unit/unit.rb, line 135
def abs
  self.class.new(@value.abs)
end
ceil() click to toggle source

Same as +Number#ceil+.

# File lib/typographic-unit/unit.rb, line 172
def ceil
  self.class.new(@value.ceil)
end
convert(other)
Alias for: >>
div(other) click to toggle source

Same as +Number#div+.

# File lib/typographic-unit/unit.rb, line 101
def div(other)
  case other
  when Unit
    @value.div((other >> self.class).value)
  when Integer, Float
    @value.div(other)
  else
    raise ArgumentError, other
  end
end
floor() click to toggle source

Same as +Number#floor+.

# File lib/typographic-unit/unit.rb, line 177
def floor
  self.class.new(@value.floor)
end
inspect() click to toggle source

@api private

# File lib/typographic-unit/unit.rb, line 255
def inspect
  "#<#{@value.to_s}#{self.class.short.to_s}>"
end
integer?() click to toggle source

Same as +Number#integer?+.

# File lib/typographic-unit/unit.rb, line 192
def integer?
  @value.integer?
end
nonzero?() click to toggle source

Same as +Number#nonzero?+.

# File lib/typographic-unit/unit.rb, line 197
def nonzero?
  @value.nonzero?
end
quo(other) click to toggle source

Same as +Number#quo+.

# File lib/typographic-unit/unit.rb, line 113
def quo(other)
  case other
  when Unit
    @value.quo((other >> self.class).value)
  when Integer, Float
    @value.quo(other)
  else
    raise ArgumentError, other
  end
end
round() click to toggle source

Same as +Number#round+.

# File lib/typographic-unit/unit.rb, line 182
def round
  self.class.new(@value.round)
end
scaled_point() click to toggle source

Convert the value into scaled point.

@return [ScaledPoint]

scaled point representation of the length
# File lib/typographic-unit/unit.rb, line 65
def scaled_point
  val = self.class.base.new(@value * self.class.size)
  val.kind_of?(ScaledPoint) ? val : val.scaled_point
end
step(limit, step=1) { |class.new(n)| ... } click to toggle source

Same as +Number#step+ but you can specify step as unit length.

@param limit [Unit]

limit length

@param step [Unit]

step
# File lib/typographic-unit/unit.rb, line 242
def step(limit, step=1)
  step = step.kind_of?(Unit) ? step : self.class.new(step)
  @value.step(limit.value, (step >> self.class).value) do |n|
    yield(self.class.new(n)) if block_given?
  end
end
to_f() click to toggle source

Convert to float representation.

@return [Unit]

same unit but the value is float

@example

1.cm.to_f #=> 1.0.cm
# File lib/typographic-unit/unit.rb, line 207
def to_f
  self.class.new(@value.to_f)
end
to_i() click to toggle source

Convert to int representaion.

@return [Unit]

same unit but the value is int

@example

1.0.cm.to_i #=> 1.cm
# File lib/typographic-unit/unit.rb, line 217
def to_i
  self.class.new(@value.to_i)
end
to_int() click to toggle source

Convert to int representation.

@return [Unit]

same unit but the value is float

@example

1.cm.to_f #=> 1.0.cm
# File lib/typographic-unit/unit.rb, line 227
def to_int
  @value.to_i
end
to_s() click to toggle source

Return string format.

# File lib/typographic-unit/unit.rb, line 250
def to_s
  @value.to_s + self.class.short.to_s
end
truncate() click to toggle source

Same as +Number#truncate+.

# File lib/typographic-unit/unit.rb, line 187
def truncate
  self.class.new(@value.truncate)
end
zero?() click to toggle source

Same as +Number#zero?+.

# File lib/typographic-unit/unit.rb, line 232
def zero?
  @value.zero?
end