lib/roda/endpoints/endpoint/collection.rb in roda-endpoints-0.1.0 vs lib/roda/endpoints/endpoint/collection.rb in roda-endpoints-0.2.0

- old
+ new

@@ -1,24 +1,23 @@ # frozen_string_literal: true -require 'roda/endpoints/endpoint' require 'roda/endpoints/endpoint/data' require 'roda/endpoints/endpoint/caching' +require 'roda/endpoints/endpoint' require 'inflecto' require 'rom/sql' class Roda module Endpoints class Endpoint # HTTP endpoint representing a collection of items of the same type. - class Collection < Endpoint - include Data + class Collection < Roda::Endpoints::Endpoint + prepend Data prepend Caching self.attributes += %i(item) self.defaults = defaults.merge( - type: :collection, last_modified: :last_modified ) # @return [{Symbol=>Object}] attr_reader :item @@ -26,39 +25,39 @@ # @return [Time] def last_modified @last_modified ? repository.public_send(@last_modified) : super end - def child(**params) - with( - type: Item, - name: resource_name, - **params - ) + # @param [Symbol] name + # @param [Hash] params + def child(name: item_name, type: Item, **params) + super(name: name, type: type, **params) end # @return [Symbol] - def resource_name - Inflecto.singularize(name).to_sym + def item_name + @item_name ||= Inflecto.singularize(name).to_sym end + # @route /{collection.name} route do |r, endpoint| # r.collection :articles do |articles| r.last_modified endpoint.last_modified if endpoint.last_modified endpoint.verbs.each do |verb| - # @route #{verb} /:name + # @route #{verb} /{collection.name} r.public_send(verb, transaction: verb) - - r.child **endpoint.item if endpoint.item # child by: :id end + + # @route #{verb} /{collection.name}/{id} + r.child **endpoint.item if endpoint.item # child by: :id end verb :get do |params| Right(repository.list(**params)) end verb :post do |params| - params = params[resource_name] || {} + params = params[item_name] || {} Right(repository.create(**params)) end # @route GET /{collection.name} transaction :get do |endpoint|