class TypedParameter::Base

Public Class Methods

field(name, type, **kargs) click to toggle source
# File lib/typed_parameter/base.rb, line 4
def field(name, type, **kargs)
  initialize_permitted_fields(name, type) # Need to remove! we don't need strong_parameter's filter
  initialize_swagger_properties(name, type, kargs)
  initialize_constraints(name, type, kargs)
end
fields() click to toggle source
# File lib/typed_parameter/base.rb, line 16
def fields
  __fields.freeze
end
permit(params) click to toggle source
# File lib/typed_parameter/base.rb, line 10
def permit(params)
  raise ArgumentError unless params.class <= ActionController::Parameters

  use_constraints(params).permit! # will change to use_constraints(params).permit!
end
ref() click to toggle source
# File lib/typed_parameter/base.rb, line 26
def ref
  "#/components/schemas/#{key}"
end
swagger_properties() click to toggle source
# File lib/typed_parameter/base.rb, line 20
def swagger_properties
  __swagger_properties.freeze
end

Private Class Methods

__constraints() click to toggle source
# File lib/typed_parameter/base.rb, line 67
def __constraints
  @__constraints ||= []
end
__fields() click to toggle source
# File lib/typed_parameter/base.rb, line 63
def __fields
  @__fields ||= []
end
__swagger_properties() click to toggle source
# File lib/typed_parameter/base.rb, line 71
def __swagger_properties
  @__swagger_properties ||= {}
end
initialize_constraints(name, type, kargs) click to toggle source
# File lib/typed_parameter/base.rb, line 42
def initialize_constraints(name, type, kargs)
  options = kargs.slice(:enum, :required)

  __constraints << [name, type, options]
end
initialize_permitted_fields(name, type) click to toggle source
# File lib/typed_parameter/base.rb, line 38
def initialize_permitted_fields(name, type)
  __fields << PermitFieldGenerator.generate(name, type)
end
initialize_swagger_properties(name, type, kargs) click to toggle source
# File lib/typed_parameter/base.rb, line 32
def initialize_swagger_properties(name, type, kargs)
  swagger_type = TypedParameter::Swagger::TypeGenerator.generate(type)
  swagger_options = swagger_type.merge(kargs)
  __swagger_properties[name] = swagger_options
end
use_constraints(params) click to toggle source
# File lib/typed_parameter/base.rb, line 48
def use_constraints(params)
  paramters = ActionController::Parameters.new

  __constraints.each do |name, type, options|
    value = params[name]
    raise RequiredFieldError, "(#{self.name}) #{name} is required" if options[:required] && !value.present?
    next unless value.present?

    paramters[name] = TypeConstraint.value(type, value)
    paramters[name] = EnumConstraint.value(value, options[:enum]) if options[:enum]
  end

  paramters
end