Class: TypographicUnit::Unit
- Inherits:
-
Numeric
- Object
- Numeric
- TypographicUnit::Unit
- Defined in:
- lib/typographic-unit/unit.rb
Overview
This is a base class for all unit classes.
Direct Known Subclasses
AmericanPoint, BigPoint, Centimeter, Cicero, DidotPoint, Gou, Inch, Meter, Millimeter, Pica, PostScriptPoint, Q, ScaledPoint, TeXPoint
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.base ⇒ Class
Return base unit class.
- .register(short, unit) ⇒ Object private
-
.short ⇒ Symbol
Return short name of the unit.
-
.size ⇒ Rational
Return size of the unit.
-
.unit(short, base, size) ⇒ void
Define a unit.
Instance Method Summary collapse
-
#%(other) ⇒ Object
Same as Number#%.
-
#*(other) ⇒ Object
Same as Number#*.
-
#+(other) ⇒ Unit
Perform addition.
-
#+@ ⇒ Object
Same as Number#+@.
-
#-(other) ⇒ Unit
Perform subtraction.
-
#-@ ⇒ Object
Same as Number#-@.
- #<=>(other) ⇒ Object private
-
#>>(other) ⇒ Object
(also: #convert)
Convert the length into other unit.
-
#abs ⇒ Object
Same as Number#abs.
-
#ceil ⇒ Object
Same as Number#ceil.
-
#div(other) ⇒ Object
Same as Number#div.
-
#floor ⇒ Object
Same as Number#floor.
-
#initialize(value) ⇒ Unit
constructor
A new instance of Unit.
- #inspect ⇒ Object private
-
#integer? ⇒ Boolean
Same as Number#integer?.
-
#nonzero? ⇒ Boolean
Same as Number#nonzero?.
-
#quo(other) ⇒ Object
Same as Number#quo.
-
#round ⇒ Object
Same as Number#round.
-
#scaled_point ⇒ ScaledPoint
Convert the value into scaled point.
-
#step(limit, step = 1) ⇒ Object
Same as Number#step but you can specify
step
as unit length. -
#to_f ⇒ Unit
Convert to float representation.
-
#to_i ⇒ Unit
Convert to int representaion.
-
#to_int ⇒ Unit
Convert to int representation.
-
#to_s ⇒ Object
Return string format.
-
#truncate ⇒ Object
Same as Number#truncate.
-
#zero? ⇒ Boolean
Same as Number#zero?.
Constructor Details
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
52 53 54 |
# File 'lib/typographic-unit/unit.rb', line 52 def value @value end |
Class Method Details
.base ⇒ Class
Return base unit class.
39 40 41 |
# File 'lib/typographic-unit/unit.rb', line 39 def base @base end |
.register(short, unit) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
6 7 8 |
# File 'lib/typographic-unit/unit.rb', line 6 def register(short, unit) TypographicUnit::TABLE[short] = unit end |
.short ⇒ Symbol
Return short name of the unit.
31 32 33 |
# File 'lib/typographic-unit/unit.rb', line 31 def short @short end |
.size ⇒ Rational
Return size of the unit.
47 48 49 |
# File 'lib/typographic-unit/unit.rb', line 47 def size @size end |
.unit(short, base, size) ⇒ void
This method returns an undefined value.
Define a unit. This method is used in concrete unit classes.
20 21 22 23 24 25 |
# File 'lib/typographic-unit/unit.rb', line 20 def unit(short, base, size) @short = short @base = base @size = size register(short, self) end |
Instance Method Details
#%(other) ⇒ Object
Same as Number#%.
89 90 91 92 93 94 95 96 97 98 |
# 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) ⇒ Object
Same as Number#*.
166 167 168 169 |
# 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) ⇒ Unit
Perform addition.
147 148 149 150 |
# 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 |
#+@ ⇒ Object
Same as Number#+@.
125 126 127 |
# File 'lib/typographic-unit/unit.rb', line 125 def +@ self.class.new(+@value) end |
#-(other) ⇒ Unit
Perform subtraction.
160 161 162 163 |
# 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 |
#-@ ⇒ Object
Same as Number#-@.
130 131 132 |
# File 'lib/typographic-unit/unit.rb', line 130 def -@ self.class.new(-@value) end |
#<=>(other) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
71 72 73 74 |
# 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) ⇒ Object Also known as: convert
Convert the length into other unit.
80 81 82 83 84 |
# 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 |
#abs ⇒ Object
Same as Number#abs.
135 136 137 |
# File 'lib/typographic-unit/unit.rb', line 135 def abs self.class.new(@value.abs) end |
#ceil ⇒ Object
Same as Number#ceil.
172 173 174 |
# File 'lib/typographic-unit/unit.rb', line 172 def ceil self.class.new(@value.ceil) end |
#div(other) ⇒ Object
Same as Number#div.
101 102 103 104 105 106 107 108 109 110 |
# 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 ⇒ Object
Same as Number#floor.
177 178 179 |
# File 'lib/typographic-unit/unit.rb', line 177 def floor self.class.new(@value.floor) end |
#inspect ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
255 256 257 |
# File 'lib/typographic-unit/unit.rb', line 255 def inspect "#<#{@value.to_s}#{self.class.short.to_s}>" end |
#integer? ⇒ Boolean
Same as Number#integer?.
192 193 194 |
# File 'lib/typographic-unit/unit.rb', line 192 def integer? @value.integer? end |
#nonzero? ⇒ Boolean
Same as Number#nonzero?.
197 198 199 |
# File 'lib/typographic-unit/unit.rb', line 197 def nonzero? @value.nonzero? end |
#quo(other) ⇒ Object
Same as Number#quo.
113 114 115 116 117 118 119 120 121 122 |
# 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 ⇒ Object
Same as Number#round.
182 183 184 |
# File 'lib/typographic-unit/unit.rb', line 182 def round self.class.new(@value.round) end |
#scaled_point ⇒ ScaledPoint
Convert the value into scaled point.
65 66 67 68 |
# 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) ⇒ Object
Same as Number#step but you can specify step
as unit length.
242 243 244 245 246 247 |
# 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 ⇒ Unit
Convert to float representation.
207 208 209 |
# File 'lib/typographic-unit/unit.rb', line 207 def to_f self.class.new(@value.to_f) end |
#to_i ⇒ Unit
Convert to int representaion.
217 218 219 |
# File 'lib/typographic-unit/unit.rb', line 217 def to_i self.class.new(@value.to_i) end |
#to_int ⇒ Unit
Convert to int representation.
227 228 229 |
# File 'lib/typographic-unit/unit.rb', line 227 def to_int @value.to_i end |
#to_s ⇒ Object
Return string format.
250 251 252 |
# File 'lib/typographic-unit/unit.rb', line 250 def to_s @value.to_s + self.class.short.to_s end |
#truncate ⇒ Object
Same as Number#truncate.
187 188 189 |
# File 'lib/typographic-unit/unit.rb', line 187 def truncate self.class.new(@value.truncate) end |
#zero? ⇒ Boolean
Same as Number#zero?.
232 233 234 |
# File 'lib/typographic-unit/unit.rb', line 232 def zero? @value.zero? end |