Sha256: e645879065e560188b78cb49fc0ff76244cc589c77fdc554ba8ce98879d77784

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 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: ' ')
      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

6 entries across 6 versions & 1 rubygems

Version Path
aleph_analytics-0.0.5 lib/pagination_search/search_conditions.rb
aleph_analytics-0.0.4 lib/pagination_search/search_conditions.rb
aleph_analytics-0.0.3 lib/pagination_search/search_conditions.rb
aleph_analytics-0.0.2 lib/pagination_search/search_conditions.rb
aleph_analytics-0.0.1.alpha lib/pagination_search/search_conditions.rb
aleph_analytics-0.0.0.alpha lib/pagination_search/search_conditions.rb