class Bluepine::Endpoints::Params

Usage

Params.new(:create)
Params.new(:index, params: :list)
Params.new(:create, params: %i[amount currency])
Params.new(:create, params: %i[amount], exclude: true)
Params.new(:create, params: false)
Params.new :create, params: -> {
  integer :amount
}
Params.new(:index, schema: :user, as: :list)

Constants

DEFAULT_OPTIONS
InvalidType
NotBuilt

Attributes

action[R]
params[R]
schema[R]

Public Class Methods

new(action, params: false, **options, &block) click to toggle source
# File lib/bluepine/endpoints/params.rb, line 30
def initialize(action, params: false, **options, &block)
  super(action, options.except(DEFAULT_OPTIONS.keys))

  options  = DEFAULT_OPTIONS.merge(options)
  @action  = action.to_sym
  @exclude = options[:exclude]
  @schema  = options[:schema]
  @params  = block_given? ? block : params
  @built   = options[:built]
end

Public Instance Methods

build(default = {}, resolver = nil) click to toggle source
# File lib/bluepine/endpoints/params.rb, line 41
def build(default = {}, resolver = nil)
  # Flag as built
  @built = true

  case @params
  when Params
    @params
  when true
    # use default params
    default
  when false
    # use no params
    self
  when Proc
    instance_exec(&@params)
    self
  when Symbol
    # use params from other service
    resolver.endpoint(@params).params
  when Array
    assert_subset_of(default.keys, @params)

    # override default params by using specified symbol
    keys = @exclude ? default.keys - @params : @params
    keys.each { |name| self[name] = default[name] }
    self
  else
    raise InvalidType
  end
end
built?() click to toggle source
# File lib/bluepine/endpoints/params.rb, line 72
def built?
  @built
end
permit(params = {}) click to toggle source

Build permitted params for ActionController::Params

# File lib/bluepine/endpoints/params.rb, line 77
def permit(params = {})
  raise NotBuilt unless built?

  build_permitted_params(attributes, params)
end

Private Instance Methods

build_permitted_params(attrs, params = {}) click to toggle source
# File lib/bluepine/endpoints/params.rb, line 85
def build_permitted_params(attrs, params = {})
  attrs.map do |name, attr|
    # permit array
    # TODO: params.permit(:foo, array: [:key1, :key2])
    next { name => [] } if attr.kind_of?(Bluepine::Attributes::ArrayAttribute)

    # permit non-object
    next name unless attr.kind_of?(Bluepine::Attributes::ObjectAttribute)

    # Rails 5.0 doesn't support arbitary hash params
    # 5.1+ supports this, via `key: {}` empty hash.
    #
    # The work around is to get hash keys from params
    # and assign them to permit keys
    # params = params&.fetch(name, {})
    data = params&.fetch(name, {})
    keys = build_permitted_params(attr.attributes, data)

    { attr.name => normalize_permitted_params(keys, data) }
  end
end
normalize_permitted_params(keys, params = {}) click to toggle source
# File lib/bluepine/endpoints/params.rb, line 107
def normalize_permitted_params(keys, params = {})
  return keys unless keys.empty?
  return {} if params.empty?

  params.keys
end