Class: Paggio::CSS::Unit

Inherits:
Object
  • Object
show all
Defined in:
lib/paggio/css/unit.rb

Constant Summary collapse

COMPATIBLE =
%i[in pt mm cm px pc]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, type) ⇒ Unit

Returns a new instance of Unit.



18
19
20
21
# File 'lib/paggio/css/unit.rb', line 18

def initialize(number, type)
  @number = number
  @type   = type
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



16
17
18
# File 'lib/paggio/css/unit.rb', line 16

def number
  @number
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/paggio/css/unit.rb', line 16

def type
  @type
end

Instance Method Details

#*(other) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/paggio/css/unit.rb', line 71

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) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/paggio/css/unit.rb', line 47

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

#+@Object



99
100
101
# File 'lib/paggio/css/unit.rb', line 99

def +@
  Unit.new(@number, @type)
end

#-(other) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/paggio/css/unit.rb', line 59

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

#-@Object



95
96
97
# File 'lib/paggio/css/unit.rb', line 95

def -@
  Unit.new(@number * -1, @type)
end

#/(other) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/paggio/css/unit.rb', line 83

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) ⇒ Object Also known as: eql?



27
28
29
# File 'lib/paggio/css/unit.rb', line 27

def ==(other)
  @number == convert(other, @type)
end

#===(other) ⇒ Object



31
32
33
# File 'lib/paggio/css/unit.rb', line 31

def ===(other)
  @type == other.type && @number == other.number
end

#coerce(other) ⇒ Object



23
24
25
# File 'lib/paggio/css/unit.rb', line 23

def coerce(other)
  return self, other
end

#hashObject



37
38
39
# File 'lib/paggio/css/unit.rb', line 37

def hash
  [@number, @type].hash
end

#to_fObject



107
108
109
# File 'lib/paggio/css/unit.rb', line 107

def to_f
  @number.to_f
end

#to_iObject



103
104
105
# File 'lib/paggio/css/unit.rb', line 103

def to_i
  @number.to_i
end

#to_sObject Also known as: to_str, inspect



115
116
117
# File 'lib/paggio/css/unit.rb', line 115

def to_s
  "#{@number}#{@type}"
end

#to_uObject



111
112
113
# File 'lib/paggio/css/unit.rb', line 111

def to_u
  self
end