Class: Html2rss::Selectors::PostProcessors::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/html2rss/selectors/post_processors/base.rb

Overview

All post processors must inherit from this base class and implement ‘self.validate_args!` and `#get`.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, context) ⇒ Base

Initializes the post processor

Parameters:

  • value (Object)

    the value to be processed

  • context (Selectors::Context)

    runtime selector context and options



59
60
61
62
63
64
65
66
# File 'lib/html2rss/selectors/post_processors/base.rb', line 59

def initialize(value, context)
  klass = self.class
  klass.assert_type(context, Selectors::Context, 'context', context:)
  klass.validate_args!(value, context)

  @value = value
  @context = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



68
69
70
# File 'lib/html2rss/selectors/post_processors/base.rb', line 68

def context
  @context
end

#valueObject (readonly)

Returns the value of attribute value.



68
69
70
# File 'lib/html2rss/selectors/post_processors/base.rb', line 68

def value
  @value
end

Class Method Details

.assert_type(value, types = [], name, context:) ⇒ void

This method returns an undefined value.

Asserts that the value is of the expected type(s)

Parameters:

  • value (Object)

    the value to check

  • types (Array<Class>, Class) (defaults to: [])

    the expected type(s)

  • name (String)

    the name of the option being checked

  • context (Selectors::Context)

    call-site context used for richer validation errors

Raises:

  • (InvalidType)

    if the value is not of the expected type(s)



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/html2rss/selectors/post_processors/base.rb', line 32

def self.assert_type(value, types = [], name, context:)
  return if Array(types).any? { |type| value.is_a?(type) }

  options = if context.respond_to?(:options)
              context.options
            else
              { file: File.basename(caller(1, 1).first.split(':').first) }
            end
  message = "The type of `#{name}` must be #{Array(types).join(' or ')}, " \
            "but is: #{value.class} in: #{options.inspect}"
  raise InvalidType, message, [], cause: nil
end

.expect_options(keys, context) ⇒ void

This method returns an undefined value.

Validates the presence of required options in the context

Parameters:

  • keys (Array<Symbol>)

    the keys to check for presence

  • context (Selectors::Context)

    the context containing options

Raises:



15
16
17
18
19
20
21
22
# File 'lib/html2rss/selectors/post_processors/base.rb', line 15

def self.expect_options(keys, context)
  keys.each do |key|
    unless (options = context[:options]).key?(key)
      raise MissingOption, "The `#{key}` option is missing in: #{options.inspect}", [],
            cause: nil
    end
  end
end

.validate_args!(_value, _context) ⇒ void

This method returns an undefined value.

This method validates the arguments passed to the post processor. Must be implemented by subclasses.

Parameters:

  • _value (Object)

    extracted selector value

  • _context (Selectors::Context)

    post-processor execution context

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/html2rss/selectors/post_processors/base.rb', line 51

def self.validate_args!(_value, _context)
  raise NotImplementedError, 'You must implement the `validate_args!` method in the post processor'
end

Instance Method Details

#getObject

Abstract method to be implemented by subclasses

Returns:

  • (Object)

    transformed value

Raises:

  • (NotImplementedError)

    if not implemented in subclass



74
75
76
# File 'lib/html2rss/selectors/post_processors/base.rb', line 74

def get
  raise NotImplementedError, 'You must implement the `get` method in the post processor'
end