Module: PhotoCook::DevicePixelRatio

Defined in:
lib/photo-cook/device-pixel-ratio.rb

Defined Under Namespace

Classes: Invalid, OutOfBounds

Constant Summary collapse

DEFAULT =
1.0.freeze
MAX =
4.0.freeze

Class Method Summary collapse

Class Method Details

.check!(x) ⇒ Object

Raises:



19
20
21
22
23
24
# File 'lib/photo-cook/device-pixel-ratio.rb', line 19

def check!(x)
  x = typecast(x)
  raise Invalid,     x if !x.kind_of?(Integer) && (x.nan? || x.infinite?)
  raise OutOfBounds, x if x < DEFAULT || x > MAX
  true
end

.parse(x) ⇒ Object



10
11
12
# File 'lib/photo-cook/device-pixel-ratio.rb', line 10

def parse(x)
  typecast(x)
end

.parse!(x) ⇒ Object



14
15
16
17
# File 'lib/photo-cook/device-pixel-ratio.rb', line 14

def parse!(x)
  check!(x = parse(x))
  x
end

.typecast(x) ⇒ Object



41
42
43
# File 'lib/photo-cook/device-pixel-ratio.rb', line 41

def typecast(x)
  x.kind_of?(Numeric) ? x : x.to_f
end

.unify(x) ⇒ Object

Do not produce various number of pixel ratios:

1.0 => 1
2.5 => 3
2.1 => 3
3.1 => 4


31
32
33
# File 'lib/photo-cook/device-pixel-ratio.rb', line 31

def unify(x)
  typecast(x).ceil
end

.valid?(x) ⇒ Boolean



35
36
37
38
39
# File 'lib/photo-cook/device-pixel-ratio.rb', line 35

def valid?(x)
  check!(x)
rescue Invalid, OutOfBounds
  false
end