lib/gooddata/bricks/middleware/base_middleware.rb in gooddata-0.6.10 vs lib/gooddata/bricks/middleware/base_middleware.rb in gooddata-0.6.11
- old
+ new
@@ -1,12 +1,48 @@
# encoding: UTF-8
+require 'hashie'
module GoodData
module Bricks
class Middleware
attr_accessor :app
include Bricks::Utils
+
+ # 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
+ # {"entities": ["Acount", "Event", "OpportunityLineItem", "Opportunity", "User"]}
+ #
+ # The brick user only wants to use Opportunity, so he passes
+ # runtime parameter {"entities": ["Opportunity"]} which overrides
+ # the default.
+ # See spec/bricks/bricks_spec.rb for usage.
+ 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 = defaults.extend(Hashie::Extensions::DeepMerge)
+ params = default_params.deep_merge(params)
+ end
+ params
+ end
+
+ def call(params)
+ load_defaults(params)
+ end
def initialize(options = {})
@app = options[:app]
end
end