Sha256: 9e351b9de2d27cda2cbbaaaf60ef606b036d78b85b44506d58ee315408fde3f0

Contents?: true

Size: 1.51 KB

Versions: 10

Compression:

Stored size: 1.51 KB

Contents

require 'csv'

module PaginationSearch
  class SearchConditions
    ALL_TRAIT_SEARCH_KEY  = ''.freeze
    MATCH_TYPE_KEY        = 'match_type'.freeze
    EXACT_MATCH           = 'exact'.freeze

    Result = Struct.new(:trait_term_hash) do
      def single_trait_searches
        trait_term_hash.reject { |trait, _term| [ALL_TRAIT_SEARCH_KEY, MATCH_TYPE_KEY].any? { |t| t == trait } }
      end

      def any_trait_search_terms
        trait_term_hash.fetch(ALL_TRAIT_SEARCH_KEY, [])
      end

      def match_type
        match_type_term = trait_term_hash[MATCH_TYPE_KEY]
        match_type_term.present? ? match_type_term.first : nil
      end

      def match_exact?
        match_type == EXACT_MATCH
      end
    end

    class << self
      def process(search_string)
        wrapped attribute_cleaned grouped separated search_string
      end

      private

      def wrapped(cleaned_grouped_hash)
        return nil if cleaned_grouped_hash.nil?
        Result.new(cleaned_grouped_hash)
      end

      def separated(search_string)
        CSV::parse_line(search_string, col_sep: ' ').compact
      rescue CSV::MalformedCSVError => mce
        nil
      end

      def grouped(terms)
        return nil if terms.nil?
        terms.group_by { |item| /.+?(?=:)/.match(item).to_s }.to_hash
      end

      def attribute_cleaned(grouped_hash)
        return nil if grouped_hash.nil?
        grouped_hash.each do |key, values|
          values.each { |value|  value.gsub!("#{key}:", '') }.compact
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
aleph_analytics-0.4.9.pre.dev lib/pagination_search/search_conditions.rb
aleph_analytics-0.4.8 lib/pagination_search/search_conditions.rb
aleph_analytics-0.4.7 lib/pagination_search/search_conditions.rb
aleph_analytics-0.4.4 lib/pagination_search/search_conditions.rb
aleph_analytics-0.4.2 lib/pagination_search/search_conditions.rb
aleph_analytics-0.4.1 lib/pagination_search/search_conditions.rb
aleph_analytics-0.3.0 lib/pagination_search/search_conditions.rb
aleph_analytics-0.2.0 lib/pagination_search/search_conditions.rb
aleph_analytics-0.1.0 lib/pagination_search/search_conditions.rb
aleph_analytics-0.0.6 lib/pagination_search/search_conditions.rb