<commit_before>module Liquid
  class Load < Tag
    Syntax = /(one|all)\s([^\s]+)\sas\s([^\s]+)/
    
    def initialize(tag_name, markup, tokens)
      if markup =~ Syntax
        @attributes = {}
        @quantity   = $1
        @reference  = $2
        @var_name   = $3
        markup.scan(TagAttributes) do |key, value|
          @attributes[key] = value
        end
        @limit = [[(@attributes['limit'] || 100).to_i, 100].min, 0].max
      else
        raise SyntaxError.new("Syntax Error in 'load' - Valid syntax: load <one|all> <reference> as <variable_name> [where: '<field> < ==|!=|>|< > <value>'] [limit: <quantity>]")
      end
      
      super
    end
    
    def render(context)
      context[@var_name] = var_value(context)
      ''
    end
    
    private
    def var_value(context)
      # load the taxonomy instances relation
      instances = taxonomy(context).instances
      # if we have conditions, enforce them
      instances = instances.where(conditions(context)) if conditions?
      # if we only want one
      if @quantity == 'one'
        Rails.logger.debug "Finding first #{@reference} instance, using conditions: #{conditions(context).inspect}"
        # load the first
        instance = instances.first
        # if we found it, return it
        return NestedDb::InstanceDrop.new(instance, taxonomy_drop(context)) if instance
        # otherwise return nil
        nil
      # if we want many
      else
        # return an array of instances found
        return instances.limit(@limit).map { |instance|
          NestedDb::InstanceDrop.new(instance, taxonomy_drop(context))
        }
      end
    end
    
    # load the value from the context
    def value(context)
      context[@value]
    end
    
    # load the taxonomy from it's drop
    def taxonomy(context)
      taxonomy_drop(context).taxonomy
    end
    
    # load the taxonomy drop based on the reference
    def taxonomy_drop(context)
      drop = context["taxonomies.#{@reference}"]
      unless drop
        raise StandardError, "No taxonomy found with reference: #{@reference}"
      end
      drop
    end
    
    def conditions?
      @attributes.has_key?('where')
    end
    
    def conditions(context)
      if @attributes['where'] =~ /^['|"](.*)\s(==|!=|>|<)\s(.*)['|"]$/i
        field, operand, value = $1, $2, $2
        
        # process the value
        case value
        # when it's the keywords blank or nil
        when 'blank', 'nil'
          # ensure the operand is == or !=
          unless ['!=', '=='].includes?(operand)
            raise SyntaxError.new("Syntax Error in 'load' where condition - Valid syntax: you can't use blank with any operand other than == or !=")
          end
          # update it to an exists/not_exists
          operand = '!=' == operand ? 'not_exists' : 'exists'
        # if it's surrounded with quotes - we want the raw value
        when /^['|"](.*)['|"]$/
          # do nothing
        else
          value = context[value]
        end
        
        # look up the operand and process
        case operand
        when '=='
          { field.to_sym => value }
        when '>'
          { field.to_sym.gt => value }
        when '<'
          { field.to_sym.lt => value }
        when 'exists'
          { field.to_sym.exists => true }
        when 'not_exists'
          { field.to_sym.exists => false }
        end
      else
        {}
      end<commit_msg>Updated conditions in {% load %}<commit_after>module Liquid
  class Load < Tag
    Syntax = /(one|all)\s([^\s]+)\sas\s([^\s]+)/
    
    def initialize(tag_name, markup, tokens)
      if markup =~ Syntax
        @attributes = {}
        @quantity   = $1
        @reference  = $2
        @var_name   = $3
        markup.scan(TagAttributes) do |key, value|
          @attributes[key] = value
        end
        @limit = [[(@attributes['limit'] || 100).to_i, 100].min, 0].max
      else
        raise SyntaxError.new("Syntax Error in 'load' - Valid syntax: load <one|all> <reference> as <variable_name> [where: '<field> < ==|!=|>|< > <value>'] [limit: <quantity>]")
      end
      
      super
    end
    
    def render(context)
      context[@var_name] = var_value(context)
      ''
    end
    
    private
    def var_value(context)
      # load the taxonomy instances relation
      instances = taxonomy(context).instances
      # if we have conditions, enforce them
      instances = apply_conditions(instances, context) if conditions?
      # if we only want one
      if @quantity == 'one'
        Rails.logger.debug "Finding first #{@reference} instance, using conditions: #{conditions(context).inspect}"
        # load the first
        instance = instances.first
        # if we found it, return it
        return NestedDb::InstanceDrop.new(instance, taxonomy_drop(context)) if instance
        # otherwise return nil
        nil
      # if we want many
      else
        # return an array of instances found
        return instances.limit(@limit).map { |instance|
          NestedDb::InstanceDrop.new(instance, taxonomy_drop(context))
        }
      end
    end
    
    # load the value from the context
    def value(context)
      context[@value]
    end
    
    # load the taxonomy from it's drop
    def taxonomy(context)
      taxonomy_drop(context).taxonomy
    end
    
    # load the taxonomy drop based on the reference
    def taxonomy_drop(context)
      drop = context["taxonomies.#{@reference}"]
      unless drop
        raise StandardError, "No taxonomy found with reference: #{@reference}"
      end
      drop
    end
    
    def conditions?
      @attributes.has_key?('where')
    end
    
    def apply_conditions(object, context)
      conditions(context).each { |set|
        object = object.send(set[0], set[1])
      }
      object
    end
    
    def conditions(context)
      if @attributes['where'] =~ /^['|"](.*)\s(==|!=|>|<)\s(.*)['|"]$/i
        field, operand, value = $1, $2, $2
        
        # process the value
        case value
        # when it's the keywords blank or nil
        when 'blank', 'nil'
          # ensure the operand is == or !=
          unless ['!=', '=='].includes?(operand)
            raise SyntaxError.new("Syntax Error in 'load' where condition - Valid syntax: you can't use blank with any operand other than == or !=")
          end
          # update it to an exists/not_exists
          operand = '!=' == operand ? 'not_exists' : 'exists'
        # if it's surrounded with quotes - we want the raw value
        when /^['|"](.*)['|"]$/
          value = $1
        # otherwise pull it as a variable from the context
        else
          value = context[value]
        end
        
        # look up the operand and process
        case operand
        when '=='
          return [:where, { field.to_sym => value }]
        when '>'
          return [:where, { field.to_sym.gt => value }]
        when '<'
          return [:where, { field.to_sym.lt => value }]
        when 'exists'
          return [:where, { field.to_sym.exists => true }]
        when 'not_exists'
          return [:any_of, [{ field.to_sym.exists => false }, { field.to_sym => '' }, { field.to_sym => 0 }]]
        end
      end
      # default to empty array
      []
{'fraction_non_alphanumeric': 0.12401818933443572, 'fraction_numerical': 0.00413393964448119, 'mean_word_length': 1.9868312757201647, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 35, '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': '226242', 'n_tokens_mistral': 2144, 'n_tokens_neox': 2106, 'n_words': 617}