Class: DynamicImage::Breakpoints
- Inherits:
-
Object
- Object
- DynamicImage::Breakpoints
- Defined in:
- lib/dynamic_image/breakpoints.rb
Overview
DynamicImage Breakpoints
Computes the candidate widths for a srcset, given the widest the image can be rendered at.
A range steps down geometrically from the available width, an array lists the widths outright, and a single number gives one candidate.
Constant Summary collapse
- SNAP =
Candidate widths are snapped down to a multiple of this, so that images of similar size share widths.
10
Instance Attribute Summary collapse
- #spec ⇒ Range, ... readonly
-
#step ⇒ Object
readonly
Returns the value of attribute step.
Instance Method Summary collapse
-
#initialize(spec = nil, step: nil) ⇒ Breakpoints
constructor
A new instance of Breakpoints.
-
#widths(available) ⇒ Array<Integer>
Returns the candidate widths, smallest first.
Constructor Details
#initialize(spec = nil, step: nil) ⇒ Breakpoints
Returns a new instance of Breakpoints.
34 35 36 37 38 |
# File 'lib/dynamic_image/breakpoints.rb', line 34 def initialize(spec = nil, step: nil) @spec = spec.nil? ? DynamicImage.default_breakpoints : spec @step = (step || DynamicImage.breakpoint_step).to_f validate! end |
Instance Attribute Details
#spec ⇒ Range, ... (readonly)
27 28 29 |
# File 'lib/dynamic_image/breakpoints.rb', line 27 def spec @spec end |
#step ⇒ Object (readonly)
Returns the value of attribute step.
27 |
# File 'lib/dynamic_image/breakpoints.rb', line 27 attr_reader :spec, :step |
Instance Method Details
#widths(available) ⇒ Array<Integer>
Returns the candidate widths, smallest first.
The largest candidate is always the available width, whether or not it falls inside the range. An image narrower than the range gets a single candidate.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dynamic_image/breakpoints.rb', line 49 def widths(available) available = available.to_i return [] unless available.positive? case spec when Range then stepped(available) when Array then fixed(available) when Integer then [spec] else unknown_spec! end end |