Sha256: 75ec0a886f23b60357e3d21a24fc7d50bfdb35096698e0a1f79b9e06928bcaeb

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

module Alf
  module Rest
    module Helpers
      extend Forwardable

      def alf_config
        env[Rest::RACK_CONFIG_KEY]
      end

      def db_conn
        alf_config.connection
      end

      def with_db_conn
        yield(db_conn)
      end

      def_delegators :db_conn, :relvar,
                               :query,
                               :tuple_extract

      def to_location(url, ids)
        ids = ids.matching_relation if ids.respond_to?(:matching_relation)
        ids = ids.tuple_extract     if ids.respond_to?(:tuple_extract)
        ids = ids.to_hash.values
        "#{url}/#{ids.join(',')}"
      end

      def location_set?
        response.headers["Location"]
      end

      def assert!(msg='an assertion failed', status=nil, &bl)
        db_conn.assert!(msg, &bl)
      rescue FactAssertionError => ex
        ex.http_error_status = status
        raise
      end

      def deny!(msg='an assertion failed', status=nil, &bl)
        db_conn.deny!(msg, &bl)
      rescue FactAssertionError => ex
        ex.http_error_status = status
        raise
      end

      def fact!(msg='an assertion failed', status=nil, &bl)
        db_conn.fact!(msg, &bl)
      rescue FactAssertionError => ex
        ex.http_error_status = status
        raise
      end

      def no_duplicate!(&bl)
        found = relvar(&bl)
        unless found.empty?
          ids = found.project(found.keys.first.to_attr_list)
          halt Alf::Rest::Response.new(env){|r|
            r.status = 200
            r.body = {'status' => 'success', 'message' => 'skipped'}
            r["Location"] = to_location(request.path, ids)
          }.finish
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
alf-rest-0.17.1 lib/alf/rest/helpers.rb
alf-rest-0.16.3 lib/alf/rest/helpers.rb
alf-rest-0.16.2 lib/alf/rest/helpers.rb
alf-rest-0.16.1 lib/alf/rest/helpers.rb
alf-rest-0.16.0 lib/alf/rest/helpers.rb
alf-rest-0.14.0 lib/alf/rest/helpers.rb