Class: Rosette::Preprocessors::Preprocessor

Inherits:
Object
  • Object
show all
Defined in:
lib/rosette/preprocessors/preprocessor.rb

Overview

Base class for all of Rosette's pre-processors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Preprocessor

Creates a new pre-processor.

Parameters:

  • configuration (Object)

    An instance of this pre-processor's configurator.


17
18
19
# File 'lib/rosette/preprocessors/preprocessor.rb', line 17

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns an instance of this pre-processor's configurator.

Returns:

  • (Object)

    an instance of this pre-processor's configurator.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rosette/preprocessors/preprocessor.rb', line 10

class Preprocessor
  attr_reader :configuration

  # Creates a new pre-processor.
  #
  # @param [Object] configuration An instance of this pre-processor's
  #   configurator.
  def initialize(configuration)
    @configuration = configuration
  end

  # Processes the given object.
  #
  # @param [Object] object The object to process.
  # @return [Object] A copy of +object+, modified as per this
  #   pre-processor's functionality. If the object can be processed
  #   but doesn't need to be, the original object is returned without
  #   modifications.
  # @raise [UnsupportedObjectError] Raised if the object is not
  #   processable.
  def process(object)
    should_process = !configuration.applies_to_proc ||
      configuration.applies_to_proc.call(object)

    if should_process
      if method = method_for(object)
        send(method, object)
      else
        raise Rosette::Preprocessors::Errors::UnsupportedObjectError,
          "don't know how to preprocess a(n) #{object.class.name}"
      end
    else
      object
    end
  end
end

Instance Method Details

#process(object) ⇒ Object

Processes the given object.

Parameters:

  • object (Object)

    The object to process.

Returns:

  • (Object)

    A copy of object, modified as per this pre-processor's functionality. If the object can be processed but doesn't need to be, the original object is returned without modifications.

Raises:

  • (UnsupportedObjectError)

    Raised if the object is not processable.


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rosette/preprocessors/preprocessor.rb', line 30

def process(object)
  should_process = !configuration.applies_to_proc ||
    configuration.applies_to_proc.call(object)

  if should_process
    if method = method_for(object)
      send(method, object)
    else
      raise Rosette::Preprocessors::Errors::UnsupportedObjectError,
        "don't know how to preprocess a(n) #{object.class.name}"
    end
  else
    object
  end
end