Class: Helpstation::Fetchers::ByKeyFetcher

Inherits:
Processor show all
Defined in:
lib/helpstation/fetchers.rb

Direct Known Subclasses

ActiveRecordFetcher

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

#success

Methods inherited from Evaluator

call, #initialize

Constructor Details

This class inherits a constructor from Helpstation::Evaluator

Class Method Details

.build(input_key, output_key, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/helpstation/fetchers.rb', line 6

def self.build(input_key, output_key, &block)
  Class.new(self) do
    define_method :input_key do
      input_key
    end
    define_method :output_key do
      output_key
    end
    define_method :fetch do |input_value|
      block.call input_value, env
    end
  end
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
# File 'lib/helpstation/fetchers.rb', line 20

def call
  if input_value = input[input_key]
    success(input.merge(output_key => fetch(input_value)))
  else
    error("#{input_key} must be present")
  end
rescue NotFoundError
  error("#{Inflecto.humanize(output_key)} ##{input_value} not found")
end