Class: Sass::Script::Number

Inherits:
Literal show all
Defined in:
lib/sass/script/number.rb

Overview

:nodoc:

Constant Summary collapse

PRECISION =
1000.0

Instance Attribute Summary collapse

Attributes inherited from Literal

#value

Instance Method Summary collapse

Methods inherited from Literal

#==, #and, #comma, #concat, #neq, #or, #perform, #to_bool, #unary_div, #unary_not

Constructor Details

#initialize(value, numerator_units = [], denominator_units = []) ⇒ Number

Returns a new instance of Number.



10
11
12
13
14
15
# File 'lib/sass/script/number.rb', line 10

def initialize(value, numerator_units = [], denominator_units = [])
  super(value)
  @numerator_units = numerator_units
  @denominator_units = denominator_units
  normalize!
end

Instance Attribute Details

#denominator_unitsObject (readonly)

Returns the value of attribute denominator_units.



6
7
8
# File 'lib/sass/script/number.rb', line 6

def denominator_units
  @denominator_units
end

#numerator_unitsObject (readonly)

Returns the value of attribute numerator_units.



6
7
8
# File 'lib/sass/script/number.rb', line 6

def numerator_units
  @numerator_units
end

Instance Method Details

#div(other) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/sass/script/number.rb', line 49

def div(other)
  if other.is_a? Number
    operate(other, :/)
  else
    super
  end
end

#eq(other) ⇒ Object



68
69
70
71
72
# File 'lib/sass/script/number.rb', line 68

def eq(other)
  Sass::Script::Bool.new(super.to_bool &&
    self.numerator_units.sort == other.numerator_units.sort &&
    self.denominator_units.sort == other.denominator_units.sort)
end

#gt(other) ⇒ Object

Raises:

  • (NoMethodError)


74
75
76
77
# File 'lib/sass/script/number.rb', line 74

def gt(other)
  raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
  operate(other, :>)
end

#gte(other) ⇒ Object

Raises:

  • (NoMethodError)


79
80
81
82
# File 'lib/sass/script/number.rb', line 79

def gte(other)
  raise NoMethodError.new(nil, :gte) unless other.is_a?(Number)
  operate(other, :>=)
end

#inspectObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/sass/script/number.rb', line 99

def inspect
  value =
    if self.value.is_a?(Float) && (self.value.infinite? || self.value.nan?)
      self.value
    elsif int?
      self.value.to_i
    else
      (self.value * PRECISION).round / PRECISION
    end
  "#{value}#{unit_str}"
end

#int?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/sass/script/number.rb', line 116

def int?
  value % 1 == 0.0
end

Returns:

  • (Boolean)


124
125
126
# File 'lib/sass/script/number.rb', line 124

def legal_units?
  (numerator_units.empty? || numerator_units.size == 1) && denominator_units.empty?
end

#lt(other) ⇒ Object

Raises:

  • (NoMethodError)


84
85
86
87
# File 'lib/sass/script/number.rb', line 84

def lt(other)
  raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
  operate(other, :<)
end

#lte(other) ⇒ Object

Raises:

  • (NoMethodError)


89
90
91
92
# File 'lib/sass/script/number.rb', line 89

def lte(other)
  raise NoMethodError.new(nil, :lte) unless other.is_a?(Number)
  operate(other, :<=)
end

#minus(other) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/sass/script/number.rb', line 27

def minus(other)
  if other.is_a? Number
    operate(other, :-)
  else
    super
  end
end

#mod(other) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/sass/script/number.rb', line 57

def mod(other)
  if other.is_a?(Number)
    unless other.unitless?
      raise Sass::SyntaxError.new("Cannot modulo by a number with units: #{other.inspect}.")
    end
    operate(other, :%)
  else
    raise NoMethodError.new(nil, :mod)
  end
end

#plus(other) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/sass/script/number.rb', line 17

def plus(other)
  if other.is_a? Number
    operate(other, :+)
  elsif other.is_a?(Color)
    other.plus(self)
  else
    super
  end
end

#times(other) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/sass/script/number.rb', line 39

def times(other)
  if other.is_a? Number
    self.operate(other, :*)
  elsif other.is_a? Color
    other.times(self)
  else
    raise NoMethodError.new(nil, :times)
  end
end

#to_iObject



111
112
113
114
# File 'lib/sass/script/number.rb', line 111

def to_i
  super unless int?
  return value
end

#to_sObject

Raises:



94
95
96
97
# File 'lib/sass/script/number.rb', line 94

def to_s
  raise Sass::SyntaxError.new("#{inspect} isn't a valid CSS value.") unless legal_units?
  inspect
end

#unary_minusObject



35
36
37
# File 'lib/sass/script/number.rb', line 35

def unary_minus
  Number.new(-value, numerator_units, denominator_units)
end

#unitless?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/sass/script/number.rb', line 120

def unitless?
  numerator_units.empty? && denominator_units.empty?
end