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.81 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.80 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.79 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.78 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.77 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.76 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.75 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.74 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.73 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.72 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.71 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.70 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.69 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.68 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.67 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.66 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.65 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.64 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.63 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb
forest_admin_datasource_toolkit-1.0.0.pre.beta.62 lib/forest_admin_datasource_toolkit/components/query/condition_tree/transforms/pattern.rb