Sha256: e7489acfaf544a088b62b20001d0ba1b27fef35634a25768c2afa3129f92958e

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

module Hovercraft
  module Routes
    def generate_index(model_class, model_name, plural_model_name)
      get(%r{^/#{plural_model_name}(\.(?<format>[^\./?#]+))?$}) do
        model_instances = model_class.all
        status 200
        respond_with model_instances
      end
    end

    def generate_create(model_class, model_name, plural_model_name)
      post(%r{^/#{plural_model_name}(\.(?<format>[^\./?#]+))?$}) do
        model_instance = model_class.create(params[model_name.to_sym])
        status model_instance.valid? ? 201 : 400
        respond_with model_instance
      end
    end

    def generate_show(model_class, model_name, plural_model_name)
      get(%r{^/#{plural_model_name}/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}) do
        model_instance = model_class.find(params[:id])
        status 200
        respond_with model_instance
      end
    end

    def generate_update(model_class, model_name, plural_model_name)
      put(%r{^/#{plural_model_name}/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}) do
        model_instance = model_class.find(params[:id])
        model_instance.update_attributes(params[model_name.to_sym])
        status model_instance.valid? ? 204 : 400
        respond_with model_instance
      end
    end

    def generate_destroy(model_class, model_name, plural_model_name)
      delete(%r{^/#{plural_model_name}/(?<id>[^\./?#]+)(\.(?<format>[^\./?#]+))?$}) do
        model_instance = model_class.find(params[:id])
        model_instance.destroy
        status 204
        respond_with model_instance
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hovercraft-0.2.1 lib/hovercraft/routes.rb
hovercraft-0.2.0 lib/hovercraft/routes.rb
hovercraft-0.1.1 lib/hovercraft/routes.rb
hovercraft-0.1.0 lib/hovercraft/routes.rb