Class: Coupdoeil::Hovercard::Option::Offset

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

Constant Summary

Constants inherited from Coupdoeil::Hovercard::Option

InvalidOptionError

Instance Attribute Summary

Attributes inherited from Coupdoeil::Hovercard::Option

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Coupdoeil::Hovercard::Option

inherited, #initialize, into_bits

Constructor Details

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

Class Method Details

.parse(value) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/coupdoeil/hovercard/option/offset.rb', line 10

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 << 2)
  base |= (integer.to_i << 2 + 11)

  base
end

Instance Method Details

#validate!Object



26
27
28
29
30
31
# File 'app/models/coupdoeil/hovercard/option/offset.rb', line 26

def validate!
  return if value in Float | Integer
  return if 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