Class: Bluepine::Endpoints::Params

Inherits:
Attributes::ObjectAttribute show all
Includes:
Assertions, Resolvable
Defined in:
lib/bluepine/endpoints/params.rb

Overview

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)

Constant Summary collapse

InvalidType =
Bluepine::Error.create("Invalid params type")
NotBuilt =
Bluepine::Error.create("Params need to be built first")
DEFAULT_OPTIONS =
{
  exclude: false,
  schema: nil,
  built: false,
}.freeze

Constants included from Resolvable

Resolvable::ResolverRequired

Constants included from Assertions

Assertions::Error, Assertions::KeyError, Assertions::SubsetError

Instance Attribute Summary collapse

Attributes inherited from Attributes::ObjectAttribute

#attributes

Attributes inherited from Attributes::Attribute

#name

Instance Method Summary collapse

Methods included from Resolvable

#resolver

Methods included from Assertions

#assert, #assert_in, #assert_kind_of, #assert_not, #assert_subset_of, included

Methods inherited from Attributes::ObjectAttribute

#[], #[]=, #group, #keys, #method_missing, #native_type, #respond_to_missing?

Methods inherited from Attributes::Attribute

#attributes, #default, #deprecated, #description, #format, #if, #in, #match, #method, #native_type, #null, #of, #options, #private, #required, #serializable?, #spec, #spec_uri, #type, #unless, #value

Methods included from Validators::Validatable

#validators

Methods included from Validators::Normalizable

#normalize

Methods included from Serializers::Serializable

#serialize

Constructor Details

#initialize(action, params: false, **options, &block) ⇒ Params

Returns a new instance of Params.



30
31
32
33
34
35
36
37
38
39
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Bluepine::Attributes::ObjectAttribute

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



28
29
30
# File 'lib/bluepine/endpoints/params.rb', line 28

def action
  @action
end

#paramsObject (readonly)

Returns the value of attribute params.



28
29
30
# File 'lib/bluepine/endpoints/params.rb', line 28

def params
  @params
end

#schemaObject (readonly)

Returns the value of attribute schema.



28
29
30
# File 'lib/bluepine/endpoints/params.rb', line 28

def schema
  @schema
end

Instance Method Details

#build(default = {}, resolver = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 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?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/bluepine/endpoints/params.rb', line 72

def built?
  @built
end

#permit(params = {}) ⇒ Object

Build permitted params for ActionController::Params

Raises:



77
78
79
80
81
# File 'lib/bluepine/endpoints/params.rb', line 77

def permit(params = {})
  raise NotBuilt unless built?

  build_permitted_params(attributes, params)
end