Module: ServiceObjects::Helpers::Parameters

Included in:
Base, Parameterized
Defined in:
lib/service_objects/helpers/parameters.rb

Overview

Note:

A target class should *include* the module

Features for whitelisting service options

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.whitelistArray<Symbol> (readonly)

Returns the list of allowed parameters

Returns:

  • (Array<Symbol>)

# File 'lib/service_objects/helpers/parameters.rb', line 13

Instance Attribute Details

#paramsHash (readonly)

Service object parameters

Returns:

  • (Hash)

23
24
25
# File 'lib/service_objects/helpers/parameters.rb', line 23

def params
  @params
end

Instance Method Details

#initialize(options = {}) ⇒ undefined

Service object initializer

Examples:

Normalizing options

class AddFoo
  include ServiceObjects::Helpers::Parameters

  @whitelist = [:foo]
end

service = AddFoo.new("foo" => { "bar" => "baz" })
service.params # => { foo: { bar: "baz" } }

Whitelisting options

class AddFoo
  include ServiceObjects::Helpers::Parameters

  @whitelist = [:foo]
end

service = AddFoo.new(foo: "bar", bar: "baz")
service.params # => { foo: "bar" }

Parameters:

  • options (Hash) (defaults to: {})

Returns:

  • (undefined)

50
51
52
# File 'lib/service_objects/helpers/parameters.rb', line 50

def initialize(options = {})
  @params = Utils::NormalHash.from(options).slice(*__whitelist__)
end