Sha256: 0f29e4fe8b690ef9322ba1a9167e0d0f1dc6f979d3fce4ccab0e5f653b0ecc40

Contents?: true

Size: 1.77 KB

Versions: 50

Compression:

Stored size: 1.77 KB

Contents

module ForestAdminDatasourceToolkit
  module Components
    module Query
      module ConditionTree
        module Transforms
          class Pattern
            def self.likes(get_pattern, case_sensitive)
              operator = case_sensitive ? Operators::LIKE : Operators::I_LIKE

              {
                depends_on: [operator],
                for_types: ['String'],
                replacer: proc { |leaf| leaf.override(operator: operator, value: get_pattern.call(leaf.value)) }
              }
            end

            def self.match(case_sensitive)
              {
                depends_on: [Operators::MATCH],
                for_types: ['String'],
                replacer: proc { |leaf|
                  regex = leaf.value.gsub(/([\.\\\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:\-])/, '\\\\\1')
                  regex.gsub!('%', '.*')
                  regex.tr!('_', '.')

                  leaf.override(operator: Operators::MATCH, value: "/^#{regex}$/#{case_sensitive ? "" : "i"}")
                }
              }
            end

            def self.transforms
              {
                Operators::CONTAINS => [likes(proc { |value| "%#{value}%" }, true)],
                Operators::STARTS_WITH => [likes(proc { |value| "#{value}%" }, true)],
                Operators::ENDS_WITH => [likes(proc { |value| "%#{value}" }, true)],
                Operators::I_CONTAINS => [likes(proc { |value| "%#{value}%" }, false)],
                Operators::I_STARTS_WITH => [likes(proc { |value| "#{value}%" }, false)],
                Operators::I_ENDS_WITH => [likes(proc { |value| "%#{value}" }, false)],
                Operators::I_LIKE => [match(false)],
                Operators::LIKE => [match(true)]
              }
            end
          end
        end
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
forest_admin_datasource_toolkit-1.0.0.pre.beta.35 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.34 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.33 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.32 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.31 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.30 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.29 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.28 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.27 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.26 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb