Class: Bluepine::Endpoints::Params
- Inherits:
-
Attributes::ObjectAttribute
- Object
- Attributes::Attribute
- Attributes::ObjectAttribute
- Bluepine::Endpoints::Params
- 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
Constants included from Assertions
Assertions::Error, Assertions::KeyError, Assertions::SubsetError
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Attributes inherited from Attributes::ObjectAttribute
Attributes inherited from Attributes::Attribute
Instance Method Summary collapse
- #build(default = {}, resolver = nil) ⇒ Object
- #built? ⇒ Boolean
-
#initialize(action, params: false, **options, &block) ⇒ Params
constructor
A new instance of Params.
-
#permit(params = {}) ⇒ Object
Build permitted params for ActionController::Params.
Methods included from Resolvable
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
Methods included from Validators::Normalizable
Methods included from Serializers::Serializable
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, **, &block) super(action, .except(DEFAULT_OPTIONS.keys)) = DEFAULT_OPTIONS.merge() @action = action.to_sym @exclude = [:exclude] @schema = [:schema] @params = block_given? ? block : params @built = [:built] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Bluepine::Attributes::ObjectAttribute
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
28 29 30 |
# File 'lib/bluepine/endpoints/params.rb', line 28 def action @action end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
28 29 30 |
# File 'lib/bluepine/endpoints/params.rb', line 28 def params @params end |
#schema ⇒ Object (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
72 73 74 |
# File 'lib/bluepine/endpoints/params.rb', line 72 def built? @built end |
#permit(params = {}) ⇒ Object
Build permitted params for ActionController::Params
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 |