Sha256: ffa75ac9bbe8a0154191c47f738e18ddef198a2239fbb6c37a1e96248c27e9e6

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 KB

Contents

module Commute
  module Extension

    # Public: Defines standard Restful CRUD actions.
    #
    # create  -> POST
    # read    -> GET
    # update  -> PUT
    # destroy -> DELETE
    #
    # Note: Implement "one" and "list" methods to
    # hook on different CRUD selectors.
    #
    # Examples:
    #
    #   gist = gists.find(1).run!
    #   gist[:description] = "Testing CRUD"
    #   gists.update(gist).where(id: 1).run
    #   gists.destroy(1).run
    #
    #   # Making use of contexts.
    #   handle = gists.where(id: 1)
    #   gist = handle.find.run!
    #   gist[:description] = "Testing CRUD"
    #   handle.update(gist).run
    #   handle.destroy
    #
    module Crud
      METHODS = [:all, :find, :update, :create, :destroy].freeze

      # Public: An Extended CRUD extension that uses PATCH for updates.
      #
      # create  -> POST
      # read    -> GET
      # update  -> PATCH
      # replace -> PUT
      # destroy -> DELETE
      #
      module Extended
        include Crud
        METHODS = [:all, :find, :update, :replace, :create, :destroy].freeze

        # Public: Replaces a resource
        def replace body = nil
          put.body(body).try :one
        end

        # Public: Updates a resource.
        def update body = nil
          patch.body(body).try :one
        end
      end

      # Public: Get all resources.
      def all
        get.try :list
      end

      # Public: Find a resource using an id.
      #
      # id - The id of the resource (optional).
      #
      def find id = nil
        with id: id
        get.try :one
      end

      # Public: Updates a resource.
      def update body = nil
        put.body(body).try :one
      end

      # Public: Creates a resource.
      def create body = nil
        post.body(body).try :one
      end

      # Public: Destroys a resource.
      #
      # id - The id of the resource to destroy (optional).
      #
      def destroy id = nil
        with id: id
        delete.try :one
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
commute-0.3.0.pre.2 lib/commute/extensions/crud.rb
commute-0.3.0.pre lib/commute/extensions/crud.rb