Class: Unbreakable::Processors::Transform

Inherits:
Object
  • Object
show all
Includes:
Dragonfly::Configurable, Dragonfly::Loggable
Defined in:
lib/unbreakable/processors/transform.rb

Overview

If you are writing a simple scraper and only need one processor, you may implement a single transform processor method by subclassing this class:

require 'nokogiri'
class MyProcessor < Unbreakable::Processors::Transform
  # Extracts the page title from an HTML page.
  def perform
    Nokogiri::HTML(temp_object.data).at_css('title')
  end

  # Saves the page title to an external database.
  def persist(arg)
    MyModel.create(:title => arg)
  end
end
MyScraper.processor.register MyProcessor

The following instance methods must be implemented in sub-classes:

  • perform

  • persist

transform calls persist with the output of perform. This makes it easy for others to subclass your processor and just change the persist method to change the external database, for example, while still taking advantage of the hard work done by perform.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



77
78
79
# File 'lib/unbreakable/processors/transform.rb', line 77

def opts
  @opts
end

#temp_objectObject (readonly)

Returns the value of attribute temp_object.



77
78
79
# File 'lib/unbreakable/processors/transform.rb', line 77

def temp_object
  @temp_object
end

Class Method Details

.inherited(subclass) ⇒ Object

#transform must be defined on the subclass for Dragonfly to see it.

Parameters:

  • subclass (Class)

    a subclass



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/unbreakable/processors/transform.rb', line 81

def self.inherited(subclass)
  subclass.class_eval do
    # @param [Dragonfly::TempObject] temp_object
    # @param [Hash] opts
    # @return [Dragonfly::TempObject] the same object
    def transform(temp_object, opts = {})
      @temp_object, @opts = temp_object, opts
      persist perform
      temp_object
    end
  end
end