Class: SearchEngines::ResultMapper
Overview
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
-
.qattr(*keys) ⇒ Array<Symbol>
A method that dynamically creates setter methods for the given keys.
Instance Method Summary collapse
-
#initialize(item, &block) ⇒ ResultMapper
constructor
A new instance of ResultMapper.
-
#map ⇒ Result
Evaluates the given block in the context of the current instance.
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>
This method is used to create the syntactic sugar for ‘url`, `title`, and `snippet` methods to more easily map results.
‘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.
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
#map ⇒ Result
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`.
248 249 250 251 |
# File 'lib/dsl.rb', line 248 def map instance_exec(@item, &@mapping_block) Result.new(@url, @title, @snippet) end |