lib/roda/endpoints/endpoint.rb in roda-endpoints-0.1.0 vs lib/roda/endpoints/endpoint.rb in roda-endpoints-0.2.0
- old
+ new
@@ -1,11 +1,11 @@
# frozen_string_literal: true
require 'roda/endpoints'
require 'roda/endpoints/endpoint/class_interface'
+require 'roda/endpoints/endpoint/lookup'
require 'roda/endpoints/endpoint/namespace'
-require 'roda/endpoints/endpoint/operations'
require 'roda/endpoints/endpoint/transactions'
require 'roda/endpoints/endpoint/validations'
require 'roda/endpoints/endpoint/verbs'
require 'dry/monads'
@@ -16,10 +16,11 @@
extend ClassInterface
include Dry::Monads::Either::Mixin
autoload :Collection, 'roda/endpoints/endpoint/collection'
autoload :Item, 'roda/endpoints/endpoint/item'
+ autoload :Singleton, 'roda/endpoints/endpoint/singleton'
self.attributes = %i(container type).freeze
self.defaults = EMPTY_HASH
self.statuses = {
get: { success: :ok, failure: :not_found },
@@ -40,36 +41,40 @@
end
prepend Namespace
prepend Verbs
prepend Validations
- prepend Operations
include Transactions
+ prepend Lookup
- # @return [Dry::Container::Mixin]
- def container
- @container || parent&.container
- end
+ attr_accessor :captures
# @return [Symbol]
- attr_reader :type
+ def type
+ self.class.type
+ end
- # @param [{Symbol=>Object}] attributes
- # @param [:collection, :item] type
- # @param [{Symbol=>Object}] attributes
+ # @param type [:collection, :item]
+ # @param attributes [{Symbol=>Object}]
def with(type: self.class, **attributes)
type.new to_hash.merge(attributes).merge(inheritable_attributes)
end
+ # @param [Class(Endpoint)] type
+ # @param [Hash] params
+ def child(type: Singleton, **params)
+ with(type: type, **params)
+ end
+
+ # @return [{Symbol=>Object}]
def inheritable_attributes
- {
- parent: self,
- container: container
- }
+ { parent: self, container: container }
end
# @return [Proc]
def route
+ prepare_validations!
+ prepare_verbs!
self.class.route
end
# @return [{Symbol=>Object}]
def to_hash