<commit_before>module Alf
  module Rest
    class Agent
      module ServiceMethods

        def get
          with_restricted_relvar do |rv|
            app.send_payload(mode==:tuple ? rv.tuple_extract : rv)
          end
        end

        def post
          raise NotSupportedError unless mode==:relation
          with_restricted_relvar do |rv|
            single  = TupleLike === self.body

            # project tuples and coerce them
            tuples  = Relation(self.body)
            attrs   = rv.heading.to_attr_list & tuples.attribute_list
            tuples  = tuples.project(attrs)
            tuples  = tuples.coerce(rv.heading.project(attrs))

            # insert them
            ids     = rv.insert(tuples)

            # get created tuples
            created = rv.restrict(Predicate.coerce(ids))
            created = created.tuple_extract if single

            # sets the location
            set_location(created)

            # yield if requested
            yield(created) if block_given?

            # app.send_payload now
            app.status 201
            201
          end
        end

        def no_post(tuple)
          set_location(tuple)
          app.status 204
          204
        end

        def delete
          with_restricted_relvar do |rv|
            rv.tuple_extract if mode==:tuple
            rv.delete
            yield if block_given?
          end
          204
        end

        def patch
          raise NotSupportedError unless mode==:tuple
          with_restricted_relvar do |rv|
            rv.tuple_extract

            # project tuple and coerce it
            tuple  = Tuple(self.body)
            attrs  = rv.heading.to_attr_list & tuple.keys
            tuple  = tuple.project(attrs)
            tuple  = tuple.coerce(rv.heading.project(attrs))

            # update it
            rv.update(tuple)<commit_msg>Apply coercions through tuple/relation types.<commit_after>module Alf
  module Rest
    class Agent
      module ServiceMethods

        def get
          with_restricted_relvar do |rv|
            app.send_payload(mode==:tuple ? rv.tuple_extract : rv)
          end
        end

        def post
          raise NotSupportedError unless mode==:relation
          with_restricted_relvar do |rv|
            single  = TupleLike === self.body

            # project tuples and coerce them
            tuples  = Relation(self.body)
            heading = rv.heading.project(tuples.to_attr_list)
            tuples  = Relation[heading].coerce(tuples.project(heading.to_attr_list))

            # insert them
            ids     = rv.insert(tuples)

            # get created tuples
            created = rv.restrict(Predicate.coerce(ids))
            created = created.tuple_extract if single

            # sets the location
            set_location(created)

            # yield if requested
            yield(created) if block_given?

            # app.send_payload now
            app.status 201
            201
          end
        end

        def no_post(tuple)
          set_location(tuple)
          app.status 204
          204
        end

        def delete
          with_restricted_relvar do |rv|
            rv.tuple_extract if mode==:tuple
            rv.delete
            yield if block_given?
          end
          204
        end

        def patch
          raise NotSupportedError unless mode==:tuple
          with_restricted_relvar do |rv|
            rv.tuple_extract

            # project tuple and coerce it
            tuple   = Tuple(self.body)
            heading = rv.heading.project(tuple.keys)
            tuple   = Tuple[heading].coerce(tuple.project(heading.to_attr_list))

            # update it
            rv.update(tuple)
{'fraction_non_alphanumeric': 0.06196581196581197, 'fraction_numerical': 0.008012820512820512, 'mean_word_length': 1.4573490813648293, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '350810', 'n_tokens_mistral': 1039, 'n_tokens_neox': 958, 'n_words': 243}