Class: Paggio::CSS::Unit

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

Constant Summary collapse

TYPES =
%w[em ex ch rem vh vw vmin vmax px mm cm in pt pc s deg].map(&:to_sym)
COMPATIBLE =
%w[in pt mm cm px pc].map(&:to_sym)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, type) ⇒ Unit

Returns a new instance of Unit.



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

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

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#*(other) ⇒ Object



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

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



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

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



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

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

#-(other) ⇒ Object



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

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



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

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

#/(other) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/paggio/css/unit.rb', line 96

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?



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/paggio/css/unit.rb', line 28

def ==(other)
  unless Unit === other
    unless other.respond_to? :to_u
      raise TypeError, "no implicit conversion of #{other.class} into Unit"
    end

    other = other.to_u
  end

  unless Unit === other
    other = Unit.new(other, @type)
  end

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

#===(other) ⇒ Object



44
45
46
# File 'lib/paggio/css/unit.rb', line 44

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

#coerce(other) ⇒ Object



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

def coerce(other)
  return self, other
end

#hashObject



50
51
52
# File 'lib/paggio/css/unit.rb', line 50

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

#to_fObject



120
121
122
# File 'lib/paggio/css/unit.rb', line 120

def to_f
  @number.to_f
end

#to_iObject



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

def to_i
  @number.to_i
end

#to_sObject Also known as: to_str, inspect



128
129
130
# File 'lib/paggio/css/unit.rb', line 128

def to_s
  "#@number#@type"
end

#to_uObject



124
125
126
# File 'lib/paggio/css/unit.rb', line 124

def to_u
  self
end