<commit_before>require 'jbuilder'
require 'digest/md5'
require 'action_view'
require 'bath/digestor'
require 'bath/handler'
require 'bath/partial_extension'
require 'bath/deferment_extension'
require 'bath/search_extension'

module Bath
  class Template < ::Jbuilder
    include PartialDigestor

    prepend PartialExtension
    prepend SearchExtension
    prepend DefermentExtension

    class << self
      attr_accessor :template_lookup_options
    end

    self.template_lookup_options = { handlers: [:bath] }


    class Digest
      def initialize(digest)
        @digest = "Relax.cache(\"#{digest}\")"
      end

      def to_json(*)
        @digest
      end

      def as_json(*)
        self
      end

      def encode_json(*)
        @digest
      end
    end

    def initialize(context, *args)
      @context = context
      @js = []
      @path = []
      super(*args)
    end

    def empty!
      attributes = @attributes
      @attributes = {}
      attributes
    end

    def _relax_visit_current(path)
      uri = ::URI.parse(@request_path)
      qry = ::URI.decode_www_form(uri.query || '') << ["_relax_filter", path.join('.')]
      uri.query = ::URI.encode_www_form(qry)
      "Relax.visit('#{uri}', {async: true, pushState: false});"
    end

    def _blank
      BLANK
    end

    def method_missing(*args)
      key = args[0]
      @path.push(key)
      if ::Kernel.block_given?
        args = _args_for_set_with_block(*args)
        set!(*args, &::Proc.new)
      else
        args = _args_for_set(*args)
        set!(*args)
      end
    ensure
      @path.pop
    end

    def set!(key, value = BLANK, *args)
      options = args.first || {}
      options = _normalize_options(options)

      return super if !_cache_options?(options) && !_deferment_options?(options)

      result = if ::Kernel.block_given?
        _ensure_valid_key(key)
        _cache(*options[:cache]) { _scope { yield self } }
      elsif ::Jbuilder === value
        # json.age 32
        # json.person another_jbuilder
        # { "age": 32, "person": { ...  }
        _ensure_valid_key(key)
        _cache(*options[:cache]) { value.attributes! }
      else
        # json.age 32
        # { "age": 32 }
        _ensure_valid_key(key)
        _cache(*options[:cache]) { value }
      end

      _set_value key, result
    end

    def child!(options = {})
      return super(&::Proc.new) if !_cache_options?(options)
      options = _normalize_options(options)

      @attributes = [] unless ::Array === @attributes
      @attributes << _cache(*options[:cache]) {
        _scope { yield self }
      }
    end

    def array!(collection = [], *attributes)
      options = attributes.first || {}
      options = _normalize_options(options)

      collection = [] if collection.nil?
      collection = _prepare_collection_for_map(collection)
      array = if ::Kernel.block_given?
        _map_collection(collection, options, &::Proc.new)
      elsif attributes.any?
        _map_collection(collection, options) { |element| extract! element, *attributes }
      else
        collection.to_a
      end

      merge! array #remove this depednacy
    end

    def target!
      js = _relax_return(@attributes)
      @js.push(js)
      "(function(){#{@js.join}})()"
    end

    private
      def _prepare_collection_for_map(collection)
        collection
      end

      def _args_for_set_with_block(*args)
        key = args[0]
        if ::Hash === args[1] && _extended_options?(args[1])
          options = args[1]
          [key, BLANK, options]
        else
          [key, BLANK]
        end
      end

      def _args_for_set(*args)
        if args.length == 3
          key, value, options = args

          [key, value, options]
        elsif args.length == 2 && _extended_options?(args[1])
          options = args[1]
          key = args[0]
<commit_msg>Remove merge behavior by always using the update value.<commit_after>require 'jbuilder'
require 'digest/md5'
require 'action_view'
require 'bath/digestor'
require 'bath/handler'
require 'bath/partial_extension'
require 'bath/deferment_extension'
require 'bath/search_extension'

module Bath
  class Template < ::Jbuilder
    include PartialDigestor

    prepend PartialExtension
    prepend SearchExtension
    prepend DefermentExtension

    class << self
      attr_accessor :template_lookup_options
    end

    self.template_lookup_options = { handlers: [:bath] }


    class Digest
      def initialize(digest)
        @digest = "Relax.cache(\"#{digest}\")"
      end

      def to_json(*)
        @digest
      end

      def as_json(*)
        self
      end

      def encode_json(*)
        @digest
      end
    end

    def initialize(context, *args)
      @context = context
      @js = []
      @path = []
      super(*args)
    end

    def empty!
      attributes = @attributes
      @attributes = {}
      attributes
    end

    def _relax_visit_current(path)
      uri = ::URI.parse(@request_path)
      qry = ::URI.decode_www_form(uri.query || '') << ["_relax_filter", path.join('.')]
      uri.query = ::URI.encode_www_form(qry)
      "Relax.visit('#{uri}', {async: true, pushState: false});"
    end

    def _blank
      BLANK
    end

    def method_missing(*args)
      key = args[0]
      @path.push(key)
      if ::Kernel.block_given?
        args = _args_for_set_with_block(*args)
        set!(*args, &::Proc.new)
      else
        args = _args_for_set(*args)
        set!(*args)
      end
    ensure
      @path.pop
    end

    def set!(key, value = BLANK, *args)
      options = args.first || {}
      options = _normalize_options(options)

      return super if !_cache_options?(options) && !_deferment_options?(options)

      result = if ::Kernel.block_given?
        _ensure_valid_key(key)
        _cache(*options[:cache]) { _scope { yield self } }
      elsif ::Jbuilder === value
        # json.age 32
        # json.person another_jbuilder
        # { "age": 32, "person": { ...  }
        _ensure_valid_key(key)
        _cache(*options[:cache]) { value.attributes! }
      else
        # json.age 32
        # { "age": 32 }
        _ensure_valid_key(key)
        _cache(*options[:cache]) { value }
      end

      _set_value key, result
    end

    def child!(options = {})
      return super(&::Proc.new) if !_cache_options?(options)
      options = _normalize_options(options)

      @attributes = [] unless ::Array === @attributes
      @attributes << _cache(*options[:cache]) {
        _scope { yield self }
      }
    end

    def array!(collection = [], *attributes)
      options = attributes.first || {}
      options = _normalize_options(options)

      collection = [] if collection.nil?
      collection = _prepare_collection_for_map(collection)
      array = if ::Kernel.block_given?
        _map_collection(collection, options, &::Proc.new)
      elsif attributes.any?
        _map_collection(collection, options) { |element| extract! element, *attributes }
      else
        collection.to_a
      end

      merge! array #remove this depednacy
    end

    def target!
      js = _relax_return(@attributes)
      @js.push(js)
      "(function(){#{@js.join}})()"
    end

    private
      def _merge_values(current_value, updates)
        # Always use the new values. This is because cached values
        # are no longer a Ruby object. They are JS values and can't
        # be merged.

        updates
      end

      def _prepare_collection_for_map(collection)
        collection
      end

      def _args_for_set_with_block(*args)
        key = args[0]
        if ::Hash === args[1] && _extended_options?(args[1])
          options = args[1]
          [key, BLANK, options]
        else
          [key, BLANK]
        end
      end

      def _args_for_set(*args)
        if args.length == 3
          key, value, options = args

          [key, value, options]
        elsif args.length == 2 && _extended_options?(args[1])
          options = args[1]
          key = args[0]

{'fraction_non_alphanumeric': 0.11656671664167916, 'fraction_numerical': 0.004747626186906547, 'mean_word_length': 2.244831779489258, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '4170329', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2426, 'n_words': 563}