Class: LazyImages::Rails::Placeholder

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::AssetTagHelper
Defined in:
lib/lazy_images/rails/placeholder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(svg, options) ⇒ Placeholder

Returns a new instance of Placeholder.



11
12
13
14
# File 'lib/lazy_images/rails/placeholder.rb', line 11

def initialize(svg, options)
  @svg = svg
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/lazy_images/rails/placeholder.rb', line 9

def options
  @options
end

#svgObject (readonly)

Returns the value of attribute svg.



8
9
10
# File 'lib/lazy_images/rails/placeholder.rb', line 8

def svg
  @svg
end

Instance Method Details

#to_sObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lazy_images/rails/placeholder.rb', line 16

def to_s
  if options[:size]
    # N. B. extract_dimensions is a private method from
    # actionview/lib/action_view/helpers/asset_tag_helper.rb
    # beware of breakage
    options[:width], options[:height] =
      extract_dimensions(options.delete(:size))

    fragment = Nokogiri::XML::DocumentFragment.parse(svg)
    fragment.first_element_child['width'] = options[:width]
    fragment.first_element_child['height'] = options[:height]

    fragment.to_s
  else
    svg
  end
end