Class: TypographicUnit::Unit

Inherits:
Numeric
  • Object
show all
Defined in:
lib/typographic-unit/unit.rb

Overview

This is a base class for all unit classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Unit

Returns a new instance of Unit.



54
55
56
57
58
59
# 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

Instance Attribute Details

#valueObject (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

.baseClass

Return base unit class.

Returns:

  • (Class)

    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

.shortSymbol

Return short name of the unit.

Returns:

  • (Symbol)

    short name of the unit



31
32
33
# File 'lib/typographic-unit/unit.rb', line 31

def short
  @short
end

.sizeRational

Return size of the unit.

Returns:

  • (Rational)

    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.

Parameters:

  • short (Symbol)

    short name for the unit

  • base (Unit)

    base unit class

  • size (Rational)

    unit size



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#*.

Raises:

  • (ArgumentError)


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.

Examples:

1.cm + 1.mm #=> 1.1.cm

Parameters:

  • other (Unit)

    additional value

Returns:

  • (Unit)

    new value of the unit

Raises:

  • (ArgumentError)


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.

Examples:

1.cm - 1.mm #=> 0.9.cm

Parameters:

  • other (Unit)

    subtractional value

Returns:

  • (Unit)

    new value of the unit

Raises:

  • (ArgumentError)


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.

Raises:

  • (ArgumentError)


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.

Parameters:

  • other (Unit)

    target 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

#absObject

Same as Number#abs.



135
136
137
# File 'lib/typographic-unit/unit.rb', line 135

def abs
  self.class.new(@value.abs)
end

#ceilObject

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

#floorObject

Same as Number#floor.



177
178
179
# File 'lib/typographic-unit/unit.rb', line 177

def floor
  self.class.new(@value.floor)
end

#inspectObject

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?.

Returns:

  • (Boolean)


192
193
194
# File 'lib/typographic-unit/unit.rb', line 192

def integer?
  @value.integer?
end

#nonzero?Boolean

Same as Number#nonzero?.

Returns:

  • (Boolean)


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

#roundObject

Same as Number#round.



182
183
184
# File 'lib/typographic-unit/unit.rb', line 182

def round
  self.class.new(@value.round)
end

#scaled_pointScaledPoint

Convert the value into scaled point.

Returns:

  • (ScaledPoint)

    scaled point representation of the length



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.

Parameters:

  • limit (Unit)

    limit length

  • step (Unit) (defaults to: 1)

    step



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_fUnit

Convert to float representation.

Examples:

1.cm.to_f #=> 1.0.cm

Returns:

  • (Unit)

    same unit but the value is float



207
208
209
# File 'lib/typographic-unit/unit.rb', line 207

def to_f
  self.class.new(@value.to_f)
end

#to_iUnit

Convert to int representaion.

Examples:

1.0.cm.to_i #=> 1.cm

Returns:

  • (Unit)

    same unit but the value is int



217
218
219
# File 'lib/typographic-unit/unit.rb', line 217

def to_i
  self.class.new(@value.to_i)
end

#to_intUnit

Convert to int representation.

Examples:

1.cm.to_f #=> 1.0.cm

Returns:

  • (Unit)

    same unit but the value is float



227
228
229
# File 'lib/typographic-unit/unit.rb', line 227

def to_int
  @value.to_i
end

#to_sObject

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

#truncateObject

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?.

Returns:

  • (Boolean)


232
233
234
# File 'lib/typographic-unit/unit.rb', line 232

def zero?
  @value.zero?
end