Class: GoodData::Bricks::Middleware

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/gooddata/bricks/middleware/base_middleware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#returning

Constructor Details

#initialize(options = {}) ⇒ Middleware

Returns a new instance of Middleware.



52
53
54
# File 'lib/gooddata/bricks/middleware/base_middleware.rb', line 52

def initialize(options = {})
  @app = options[:app]
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



13
14
15
# File 'lib/gooddata/bricks/middleware/base_middleware.rb', line 13

def app
  @app
end

Instance Method Details

#call(params) ⇒ Object



48
49
50
# File 'lib/gooddata/bricks/middleware/base_middleware.rb', line 48

def call(params)
  load_defaults(params)
end

#load_defaults(params) ⇒ Object

Loads defaults to params from a json file in @config.

The idea is to have a set of parameter defaults for a middleware. The defaults are loaded from a json file. If a brick user wants to override a default, they can do that in runtime params which come to the method in 'params'.

A deep merge is done on the params. Arrays and other non-hash types are overwritten (params win).

Examples

A brick developer develops a SalesforceDownloaderMiddleware with default preset 'gse' having a configuration preset ["Acount", "Event", "OpportunityLineItem", "Opportunity", "User"]

The brick user only wants to use Opportunity, so he passes runtime parameter ["Opportunity"] which overrides the default. See spec/bricks/bricks_spec.rb for usage.



37
38
39
40
41
42
43
44
45
46
# File 'lib/gooddata/bricks/middleware/base_middleware.rb', line 37

def load_defaults(params)
  # if default params given, fill what's not given in runtime params
  if @config
    # load it from file and merge it
    defaults = { 'config' => MultiJson.load(File.read(@config)) }
    default_params = GoodData::Helpers::DeepMergeableHash[defaults]
    params = default_params.deep_merge(params)
  end
  params
end