Class: RSAC::Number

Inherits:
LexicalUnit show all
Defined in:
lib/antisamy/csspool/rsac/sac/lexical_unit.rb

Constant Summary collapse

NON_NEGATIVE_UNITS =
[
  :SAC_DEGREE,
  :SAC_GRADIAN,
  :SAC_RADIAN,
  :SAC_MILLISECOND,
  :SAC_SECOND,
  :SAC_HERTZ,
  :SAC_KILOHERTZ,
]
UNITS =
{
  'deg'   => :SAC_DEGREE,
  'rad'   => :SAC_RADIAN,
  'grad'  => :SAC_GRADIAN,
  'ms'    => :SAC_MILLISECOND,
  's'     => :SAC_SECOND,
  'hz'    => :SAC_HERTZ,
  'khz'   => :SAC_KILOHERTZ,
  'px'    => :SAC_PIXEL,
  'cm'    => :SAC_CENTIMETER,
  'mm'    => :SAC_MILLIMETER,
  'in'    => :SAC_INCH,
  'pt'    => :SAC_POINT,
  'pc'    => :SAC_PICA,
  '%'     => :SAC_PERCENTAGE,
  'em'    => :SAC_EM,
  'ex'    => :SAC_EX,
}

Instance Attribute Summary

Attributes inherited from LexicalUnit

#dimension_unit_text, #float_value, #function_name, #integer_value, #lexical_unit_type, #parameters, #string_value

Instance Method Summary collapse

Methods inherited from LexicalUnit

#eql?

Constructor Details

#initialize(value, unit = nil, type = nil) ⇒ Number

Returns a new instance of Number.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/antisamy/csspool/rsac/sac/lexical_unit.rb', line 159

def initialize(value, unit = nil, type = nil)
  if value.is_a?(String)
    value =~ /^(-?[0-9.]*)(.*)$/
    value = $1
    unit  ||= $2
  end
  type  ||= UNITS[self.dimension_unit_text]
  self.string_value = "#{value}#{unit}"
  self.float_value = value.to_f
  self.integer_value = value.to_i
  self.dimension_unit_text = unit.downcase
  self.lexical_unit_type = UNITS[self.dimension_unit_text] ||
  (value =~ /\./ ? :SAC_NUMBER : :SAC_INTEGER)
end

Instance Method Details

#==(other) ⇒ Object



174
175
176
177
178
179
180
181
# File 'lib/antisamy/csspool/rsac/sac/lexical_unit.rb', line 174

def ==(other)
  return true if self.float_value == 0 && other.float_value == 0
  return false unless super

  %w{ float_value integer_value dimension_unit_text }.all? { |x|
    self.send(x.to_sym) == other.send(x.to_sym)
  }
end

#hashObject



183
184
185
186
187
188
189
190
191
# File 'lib/antisamy/csspool/rsac/sac/lexical_unit.rb', line 183

def hash
  if self.float_value == 0
    self.float_value.hash
  else
    %w{ float_value integer_value dimension_unit_text }.map { |x|
      self.send(x.to_sym)
    }.hash
  end
end

#to_sObject



193
194
195
196
197
198
199
# File 'lib/antisamy/csspool/rsac/sac/lexical_unit.rb', line 193

def to_s
  if self.float_value == 0
    "0"
  else
    super
  end
end