Class: Coupdoeil::Popover::Option::Offset

Inherits:
Coupdoeil::Popover::Option show all
Defined in:
app/models/coupdoeil/popover/option/offset.rb

Constant Summary collapse

INTEGER_PART_BITS =
8
FLOAT_PART_BITS =
10
CONFIG_PART_BITS =
2

Constants inherited from Coupdoeil::Popover::Option

InvalidOptionError

Instance Attribute Summary

Attributes inherited from Coupdoeil::Popover::Option

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Coupdoeil::Popover::Option

inherited, #initialize, into_bits

Constructor Details

This class inherits a constructor from Coupdoeil::Popover::Option

Class Method Details

.parse(value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/coupdoeil/popover/option/offset.rb', line 13

def parse(value)
  float_value = value.to_f
  return 0 if float_value.zero?

  base = 0
  base |= 1 if float_value.negative?
  base |= 2 if value.is_a?(String) && value.end_with?("rem")

  integer, decimals = float_value.abs.to_s.split(".")
  base |= (decimals.to_i << CONFIG_PART_BITS)
  base |= (integer.to_i << CONFIG_PART_BITS + FLOAT_PART_BITS)

  base
end

Instance Method Details

#validate!Object



29
30
31
32
33
# File 'app/models/coupdoeil/popover/option/offset.rb', line 29

def validate!
  return ensure_no_overflow if (value in Float | Integer) || value.to_s.match?(/^-?\d+(\.\d{1,3})?(px|rem)?$/)

  raise_invalid_option "Value should be a signed float or integer, followed or not by 'rem' or 'px'."
end