Class: Content::Pipeline::Filter

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/content/pipeline/filters.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, opts = nil) ⇒ Filter




15
16
17
# File 'lib/content/pipeline/filters.rb', line 15

def initialize(str, opts = nil)
  @opts, @str = (opts || {}), str
end

Class Attribute Details

.filtersObject (readonly)

Returns the value of attribute filters.



45
46
47
# File 'lib/content/pipeline/filters.rb', line 45

def filters
  @filters
end

Class Method Details

.add_filter(*filters) ⇒ Object





51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/content/pipeline/filters.rb', line 51

def add_filter(*filters)
  @filters ||= []

  filters.each do |f|
    if f.is_a?(Hash)
      f.each do |k, v|
        @filters.push([
          k, v
        ])
      end
    else
      @filters.push([
        f,
        :str
      ])
    end
  end
end

Instance Method Details

#run(next_filter = nil) ⇒ Object





23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/content/pipeline/filters.rb', line 23

def run(next_filter = nil)
  return @str unless size > 0

  each_with_index do |f, i|
    send(f.first)

    unless size == i + 1 || @str.is_a?(String) || \
        self[i + 1].last == :nokogiri

      @str = @str.to_s
    end
  end

  next_filter = next_filter.filters.first if next_filter
  if ! next_filter || next_filter.last != :nokogiri
    return @str.to_s
  end

  @str
end