Class: Browser::CSS::Unit

Inherits:
Object show all
Defined in:
opal/browser/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.


8
9
10
11
# File 'opal/browser/css/unit.rb', line 8

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

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type


6
7
8
# File 'opal/browser/css/unit.rb', line 6

def type
  @type
end

Instance Method Details

#*(other) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
# File 'opal/browser/css/unit.rb', line 61

def *(other)
  return Unit.new(@number * other, @type) unless Unit === other

  if @type == other.type
    Unit.new(@number * other.to_f, @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


37
38
39
40
41
42
43
44
45
46
47
# File 'opal/browser/css/unit.rb', line 37

def +(other)
  return Unit.new(@number + other, @type) unless Unit === other

  if @type == other.type
    Unit.new(@number + other.to_f, @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

#[email protected]Object


89
90
91
# File 'opal/browser/css/unit.rb', line 89

def [email protected]
  Unit.new(@number, @type)
end

#-(other) ⇒ Object


49
50
51
52
53
54
55
56
57
58
59
# File 'opal/browser/css/unit.rb', line 49

def -(other)
  return Unit.new(@number - other, @type) unless Unit === other

  if @type == other.type
    Unit.new(@number - other.to_f, @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

#[email protected]Object


85
86
87
# File 'opal/browser/css/unit.rb', line 85

def [email protected]
  Unit.new(@number * -1, @type)
end

#/(other) ⇒ Object


73
74
75
76
77
78
79
80
81
82
83
# File 'opal/browser/css/unit.rb', line 73

def /(other)
  return Unit.new(@number / other, @type) unless Unit === other

  if @type == other.type
    Unit.new(@number / other.to_f, @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?


17
18
19
# File 'opal/browser/css/unit.rb', line 17

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

#===(other) ⇒ Object


21
22
23
# File 'opal/browser/css/unit.rb', line 21

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

#coerce(other) ⇒ Object


13
14
15
# File 'opal/browser/css/unit.rb', line 13

def coerce(other)
  return self, other
end

#hashObject


27
28
29
# File 'opal/browser/css/unit.rb', line 27

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

#to_fObject


97
98
99
# File 'opal/browser/css/unit.rb', line 97

def to_f
  @number.to_f
end

#to_iObject


93
94
95
# File 'opal/browser/css/unit.rb', line 93

def to_i
  @number.to_i
end

#to_sObject Also known as: to_str, inspect


105
106
107
# File 'opal/browser/css/unit.rb', line 105

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

#to_uObject


101
102
103
# File 'opal/browser/css/unit.rb', line 101

def to_u
  self
end