Module: Knuckles::Stages::Enhancer

Extended by:
Enhancer
Included in:
Enhancer
Defined in:
lib/knuckles/stages/enhancer.rb

Overview

The enhancer modifies rendered data using proc passed through options. The enhancer stage is critical to customizing the final output. For example, if staff should have confidential data that regular users can’t see you can enhance the final values. Another use of enhancers is personalizing an otherwise generic response.

Instance Method Summary collapse

Instance Method Details

#call(prepared, options) ⇒ Object

Modify all results using an ‘enhancer` proc.

Examples:

Removing tags unless the scope is staff


enhancer = lambda do |result, options|
  scope = options[:scope]

  unless scope.staff?
    result.delete_if { |key, _| key == "tags" }
  end

  result
end

prepared = [{result: {"posts" => [], "tags" => []}}]

Knuckles::Stages::Enhancer.call(prepared, enhancer: enhancer) #=>
 # [{result: {"posts" => []}}]

Parameters:

  • prepared (Enumerable)

    The prepared collection to be enhanced

  • [Proc] (Hash)

    a customizable set of options



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/knuckles/stages/enhancer.rb', line 37

def call(prepared, options)
  enhancer = options[:enhancer]

  if enhancer
    prepared.each do |hash|
      hash[:result] = enhancer.call(hash[:result], options)
    end
  end

  prepared
end