Class: Webby::Filters::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/webby/filters.rb

Overview

Instances of this class handle processing a set of filters for a given renderer and page. Note: The instance is passed as the second argument to filters

that require two parameters and can be used to access 
information on the renderer, page, or filters being
processed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(renderer, page) ⇒ Cursor

Returns a new instance of Cursor.



36
37
38
39
40
41
42
# File 'lib/webby/filters.rb', line 36

def initialize(renderer, page)
  @renderer, @page = renderer, page
  @filters = Array(page.filter)
  @log = Logging::Logger[Webby::Renderer]
  @processed = 0
  @prev_cursor = nil
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



35
36
37
# File 'lib/webby/filters.rb', line 35

def filters
  @filters
end

#pageObject (readonly)

Returns the value of attribute page.



35
36
37
# File 'lib/webby/filters.rb', line 35

def page
  @page
end

#rendererObject (readonly)

Returns the value of attribute renderer.



35
36
37
# File 'lib/webby/filters.rb', line 35

def renderer
  @renderer
end

Instance Method Details

#_handle(filter, handler, *args) ⇒ Object

Process arguments through a single filter



69
70
71
72
73
74
75
# File 'lib/webby/filters.rb', line 69

def _handle(filter, handler, *args)
  result = handler.call(*args)
  @processed += 1
  result
rescue StandardError => err
  raise ::Webby::Error, "#{filter} filter error: #{err.message.inspect}"
end

#current_filterObject

The name of the current filter



64
65
66
# File 'lib/webby/filters.rb', line 64

def current_filter
  filters[@processed]
end

#remaining_filtersObject

The list of filters yet to be processed



59
60
61
# File 'lib/webby/filters.rb', line 59

def remaining_filters
  filters[@processed..-1]
end

#start_for(input) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/webby/filters.rb', line 44

def start_for(input)
  @prev_cursor = @renderer.instance_variable_get(:@_cursor)
  @renderer.instance_variable_set(:@_cursor, self)
  filters.inject(input) do |result, filter|
    handler = Filters[filter]
    raise ::Webby::Error, "unknown filter: #{filter.inspect}" if handler.nil?

    args = [result, self][0, handler.arity]
    _handle(filter, handler, *args)
  end
ensure
  @renderer.instance_variable_set(:@_cursor, @prev_cursor)
end