Module: ServiceObjects::Helpers::Parameterized

Includes:
Parameters
Included in:
Base
Defined in:
lib/service_objects/helpers/parameterized.rb

Overview

Note:

A target class should be *extended* by the module

Features for service parameters declaration

Instance Attribute Summary

Attributes included from Parameters

#params

Instance Method Summary collapse

Methods included from Parameters

#initialize

Instance Method Details

#allows_params(*list) ⇒ Array<Symbol>

Whitelists new options assigned to params

Mutates the current class by adding the corresponding attribute for every parameter.

Examples:

Whitelists options

class AddFoo
  extend ServiceObjects::Helpers::Parameterized

  allows_params :foo
end

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

Adds attributes as aliases to corresponding params

class AddFoo
  extend ServiceObjects::Helpers::Parameterized

  allows_params :foo
end

service = AddFoo.new foo: "foo"
service.foo           # => "foo"

service.params[:foo] = "bar"
service.foo           # => "bar"

service.foo = "baz"
service.foo           # => "baz"
service.params[:foo]  # => "baz"

Parameters:

  • list (Array<#to_sym>)

Returns:

  • (Array<Symbol>)

    the list of arguments


49
50
51
52
53
54
# File 'lib/service_objects/helpers/parameterized.rb', line 49

def allows_params(*list)
  @whitelist = list.flatten.map(&:to_sym).freeze
  __attr_params__

  whitelist
end