class TypographicUnit::Unit
This is a base class for all unit classes.
Attributes
Public Class Methods
Return base unit class.
@return [Class]
base unit class
# File lib/typographic-unit/unit.rb, line 39 def base @base end
# 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
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
Return size of the unit.
@return [Rational]
size of the unit
# File lib/typographic-unit/unit.rb, line 47 def size @size end
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
@api private
# File lib/typographic-unit/unit.rb, line 6 def register(short, unit) TypographicUnit::TABLE[short] = unit end
Public Instance Methods
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
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
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
Same as Number#+@.
# File lib/typographic-unit/unit.rb, line 125 def +@ self.class.new(+@value) end
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
Same as +Number#-@+.
# File lib/typographic-unit/unit.rb, line 130 def -@ self.class.new(-@value) end
@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
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
Same as +Number#abs+.
# File lib/typographic-unit/unit.rb, line 135 def abs self.class.new(@value.abs) end
Same as +Number#ceil+.
# File lib/typographic-unit/unit.rb, line 172 def ceil self.class.new(@value.ceil) end
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
Same as +Number#floor+.
# File lib/typographic-unit/unit.rb, line 177 def floor self.class.new(@value.floor) end
@api private
# File lib/typographic-unit/unit.rb, line 255 def inspect "#<#{@value.to_s}#{self.class.short.to_s}>" end
Same as +Number#integer?+.
# File lib/typographic-unit/unit.rb, line 192 def integer? @value.integer? end
Same as +Number#nonzero?+.
# File lib/typographic-unit/unit.rb, line 197 def nonzero? @value.nonzero? end
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
Same as +Number#round+.
# File lib/typographic-unit/unit.rb, line 182 def round self.class.new(@value.round) end
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
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
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
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
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
Return string format.
# File lib/typographic-unit/unit.rb, line 250 def to_s @value.to_s + self.class.short.to_s end
Same as +Number#truncate+.
# File lib/typographic-unit/unit.rb, line 187 def truncate self.class.new(@value.truncate) end
Same as +Number#zero?+.
# File lib/typographic-unit/unit.rb, line 232 def zero? @value.zero? end