Class: Webby::Filters::Cursor
- Inherits:
-
Object
- Object
- Webby::Filters::Cursor
- 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
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#renderer ⇒ Object
readonly
Returns the value of attribute renderer.
Instance Method Summary collapse
-
#_handle(filter, handler, *args) ⇒ Object
Process arguments through a single filter.
-
#current_filter ⇒ Object
The name of the current filter.
-
#initialize(renderer, page) ⇒ Cursor
constructor
A new instance of Cursor.
-
#remaining_filters ⇒ Object
The list of filters yet to be processed.
- #start_for(input) ⇒ Object
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
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
35 36 37 |
# File 'lib/webby/filters.rb', line 35 def filters @filters end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
35 36 37 |
# File 'lib/webby/filters.rb', line 35 def page @page end |
#renderer ⇒ Object (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..inspect}" end |
#current_filter ⇒ Object
The name of the current filter
64 65 66 |
# File 'lib/webby/filters.rb', line 64 def current_filter filters[@processed] end |
#remaining_filters ⇒ Object
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 |