Class: SearchEngines::ResultMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl.rb

Overview

Note:

You will probably never directly touch this class. It is primarily used by the SearchEngineBuilder in order to provide syntax sugar for mapping result json (or whatever format you specified) to Results.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, &block) ⇒ ResultMapper

Returns a new instance of ResultMapper.



221
222
223
224
# File 'lib/dsl.rb', line 221

def initialize(item, &block)
  @item = item
  @mapping_block = block
end

Class Method Details

.qattr(*keys) ⇒ Array<Symbol>

Note:

This method is used to create the syntactic sugar for ‘url`, `title`, and `snippet` methods to more easily map results.

Note:

‘qattr` means quick attribute, fwiw

A method that dynamically creates setter methods for the given keys. These could use attr_writer, but this method ensures that the method will not need the = sign, since that just creates unnecessary pitfalls and footguns, and just looks uglier.

Parameters:

  • keys (Array<Symbol>)

    The keys to create setter methods for.

Returns:



234
235
236
237
238
# File 'lib/dsl.rb', line 234

def self.qattr(*keys)
  keys.map do |key|
    define_method(key) { |any| instance_variable_set("@#{key}", any) }
  end
end

Instance Method Details

#mapResult

Note:

This method is not meant to be called directly.

Evaluates the given block in the context of the current instance. The block is expected to contain a mapping logic that assigns values to instance variables, usually by using the ‘url`, `title`, and `snippet` methods. After evaluating the block, the method creates a new `Result` object with the values assigned to `@url`, `@title`, and `@snippet`.

Returns:

  • (Result)

    The newly created ‘Result` object.



248
249
250
251
# File 'lib/dsl.rb', line 248

def map
  instance_exec(@item, &@mapping_block)
  Result.new(@url, @title, @snippet)
end