Class: Unbreakable::Processors::Transform

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

Overview

You may implement a transform process 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

You may also override transform, which calls perform and persist in the default implementation, but you probably won’t have to.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



30
31
32
# File 'lib/unbreakable/processors/transform.rb', line 30

def opts
  @opts
end

#temp_objectObject (readonly)

Returns the value of attribute temp_object.



30
31
32
# File 'lib/unbreakable/processors/transform.rb', line 30

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



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/unbreakable/processors/transform.rb', line 34

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