Sha256: 876282e8eea271e32990e05aa6999541f4be7e9b9f802a1ad6a6bc482076bdcf

Contents?: true

Size: 1.63 KB

Versions: 9

Compression:

Stored size: 1.63 KB

Contents

module ForestLiana
  class OperatorValueParser

    def self.parse(value)
      operator = nil

      if value.first == '!'
        operator = '!='
        value.slice!(0)
      elsif value.first == '>'
        operator = '>'
        value.slice!(0)
      elsif value.first == '<'
        operator = '<'
        value.slice!(0)
      elsif value.include?('*')
        operator = 'ILIKE'
        value.gsub!('*', '%')
      elsif value === '$present'
        operator = 'IS NOT NULL'
        value = nil
      elsif value === '$blank'
        operator = 'IS NULL'
        value = nil
      else
        operator = '='
      end

      [operator, value]
    end

    def self.add_where(query, field, operator, value, resource)
      field_name = self.get_field_name(field, resource)

      operator_date_interval_parser = OperatorDateIntervalParser.new(value)
      if operator_date_interval_parser.is_interval_date_value()
        filter = operator_date_interval_parser.get_interval_date_filter()
        query = query.where("#{field_name} #{filter}")
      else
        # NOTICE: Set the integer value instead of a string if "enum" type
        if resource.defined_enums.has_key?(field)
          value = resource.defined_enums[field][value]
        end

        where = "#{field_name} #{operator}"
        where += " '#{value}'" if value
        query = query.where(where)
      end
    end

    def self.get_field_name(field, resource)
      if field.split(':').size < 2
        "\"#{resource.table_name}\".\"#{field}\""
      else
        association = field.split(':')[0].pluralize
        "\"#{association}\".\"#{field.split(':')[1]}\""
      end
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
forest_liana-1.3.28 app/services/forest_liana/operator_value_parser.rb
forest_liana-1.3.27 app/services/forest_liana/operator_value_parser.rb
forest_liana-1.3.26 app/services/forest_liana/operator_value_parser.rb
forest_liana-1.3.25 app/services/forest_liana/operator_value_parser.rb
forest_liana-1.3.24 app/services/forest_liana/operator_value_parser.rb
forest_liana-1.3.23 app/services/forest_liana/operator_value_parser.rb
forest_liana-1.3.22 app/services/forest_liana/operator_value_parser.rb
forest_liana-1.3.21 app/services/forest_liana/operator_value_parser.rb
forest_liana-1.3.20 app/services/forest_liana/operator_value_parser.rb