Class: Nanoc::Helpers::Capturing::SetContent Private

Inherits:
Object
  • Object
show all
Includes:
Nanoc::Helpers::Capturing
Defined in:
lib/nanoc/helpers/capturing.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Methods included from Nanoc::Helpers::Capturing

#capture, #content_for

Constructor Details

#initialize(name, params, item) ⇒ SetContent

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SetContent.



10
11
12
13
14
# File 'lib/nanoc/helpers/capturing.rb', line 10

def initialize(name, params, item)
  @name = name
  @params = params
  @item = item
end

Instance Method Details

#runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/nanoc/helpers/capturing.rb', line 16

def run(&)
  existing_behavior = @params.fetch(:existing, :error)

  # Capture
  content_string = capture(&)

  # Get existing contents and prep for store
  compiled_content_store = @item._context.compiled_content_store
  rep = @item.reps[:default]._unwrap
  capture_name = :"__capture_#{@name}"
  old_content_string =
    case existing_behavior
    when :overwrite
      ''
    when :append
      c = compiled_content_store.get(rep, capture_name)
      c ? c.string : ''
    when :error
      contents = compiled_content_store.get(rep, capture_name)
      if contents && contents.string != content_string
        # FIXME: get proper exception
        raise "a capture named #{@name.inspect} for #{@item.identifier} already exists"
      else
        ''
      end
    else
      raise ArgumentError, 'expected :existing_behavior param to #content_for to be one of ' \
        ":overwrite, :append, or :error, but #{existing_behavior.inspect} was given"
    end

  # Store
  new_content = Nanoc::Core::TextualContent.new(old_content_string + content_string)
  compiled_content_store.set(rep, capture_name, new_content)
end