module Roda::Endpoints::Endpoint::ClassInterface

Attributes

attributes[RW]

@return [<Symbol>]

defaults[RW]

@return [{Symbol=>Object}]

statuses[RW]

@return [{Symbol=>{Symbol=>Symbol}}]

transactions[W]
verbs[RW]

@return [<Symbol>]

Public Instance Methods

container() click to toggle source

@return [Dry::Container]

# File lib/roda/endpoints/endpoint/class_interface.rb, line 29
def container
  @container ||= Roda::Endpoints.container
end
define_attribute(key) click to toggle source

@param [Symbol] key

# File lib/roda/endpoints/endpoint/class_interface.rb, line 24
def define_attribute(key)
  attr_reader key
end
inherited(child) click to toggle source

@param [Class] child

Calls superclass method
# File lib/roda/endpoints/endpoint/class_interface.rb, line 34
def inherited(child)
  child.attributes = attributes.dup
  child.defaults = defaults.dup
  child.statuses = statuses.dup
  child.verbs = verbs.dup
  child.transactions = transactions.dup
  child.route(&@route_block)
  super
end
ns() click to toggle source
# File lib/roda/endpoints/endpoint/class_interface.rb, line 44
def ns
  @ns ||= name.gsub(/^Roda::Endpoints::/, '').underscore.tr('/', '.')
end
route(&block) click to toggle source

@param [Proc] block @return [Proc]

# File lib/roda/endpoints/endpoint/class_interface.rb, line 113
def route(&block)
  @route_block = block if block_given?
  @route_block
end
transaction(name, &block) click to toggle source
# File lib/roda/endpoints/endpoint/class_interface.rb, line 118
def transaction(name, &block)
  transactions << [name, block]
end
transactions() click to toggle source

@return [(Symbol, Hash, Proc)]

# File lib/roda/endpoints/endpoint/class_interface.rb, line 123
def transactions
  @transactions ||= []
end
type() click to toggle source

@return [Symbol]

# File lib/roda/endpoints/endpoint/class_interface.rb, line 49
def type
  @type ||= Inflecto.underscore(
    Inflecto.demodulize(
      name || superclass.name
    )
  ).to_sym
end
validate(key = :default, &block) click to toggle source

@param [String, Symbol] key @param [Proc] block

@example

r.collection :articles do |articles|
  # register validation at 'validations.endpoints.articles.default'
  articles.validate do
    required(:title).filled?
    required(:contents).filled?
  end
  # redefine validation for patch method at
  # 'validations.endpoints.articles.patch'
  articles.validate(:patch) do
    required(:title).filled?
    required(:contents).filled?
    require(:confirm_changes).
  end
end
# File lib/roda/endpoints/endpoint/class_interface.rb, line 96
def validate(key = :default, &block)
  key = "validations.#{ns}.#{key}" if key.is_a?(Symbol)
  schema = Dry::Validation.Form(&block)
  container.register(key) do |params|
    validation = schema.call(params)
    if validation.success?
      Right(validation.output)
    else
      Left([:unprocessable_entity, {}, validation])
    end
  end
  schema
end
verb(verb, rescue_from: [], &block) click to toggle source

@param [Symbol] verb @param [Proc] block @return [Symbol] name of the defined method

# File lib/roda/endpoints/endpoint/class_interface.rb, line 60
def verb(verb, rescue_from: [], &block)
  self.verbs ||= superclass.verbs
  (self.verbs += [verb]).freeze
  rescue_from = Array(rescue_from).flatten
  if rescue_from.any?
    block = proc do |*args|
      begin
        instance_exec(*args, &block)
      rescue *rescue_from
        Left($ERROR_INFO)
      end
    end
  end
  define_method(verb, &block)
  key = "operations.#{type}.#{verb}"
  container.register key, block unless container.key? key
end