Sha256: d48fc9b82b6134e7a427447afa5fadf1034f2c3fbd4a2c69441c764844d107a4

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

module Unbreakable
  module Processors
    # 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(temp_object)
    #         Nokogiri::HTML(temp_object.data).at_css('title')
    #       end
    #
    #       # Saves the page title to an external database.
    #       def persist(temp_object, 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.
    class Transform
      include Dragonfly::Configurable
      include Dragonfly::Loggable

      # +#transform+ must be defined on the subclass for Dragonfly to see it.
      # @param [Class] subclass a subclass
      def self.inherited(subclass)
        subclass.class_eval do
          # @param [Dragonfly::TempObject] temp_object
          # @return [Dragonfly::TempObject] the same object
          def transform(temp_object)
            persist temp_object, perform(temp_object)
            temp_object
          end
        end
      end

    private

      # Transforms a record.
      # @param [Dragonfly::TempObject] temp_object
      # @return [Hash] the transformed record
      def perform(temp_object)
        raise NotImplementedError
      end

      # Persists a transformed record.
      # @param [Dragonfly::TempObject] temp_object
      # @param arg a transformed record
      def persist(temp_object, arg)
        raise NotImplementedError
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unbreakable-0.0.1 lib/unbreakable/processors/transform.rb