Sha256: 278c916d5dfd8d650d947aece6dc57239a0995d6cb312803fe958107ffa2651f

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

require 'active_support/core_ext/string'

module Carmen
  module Querying
    # Find a region by code.
    #
    # code - The String code to search for
    #
    # Returns a region with the supplied code, or nil if none is found.
    def coded(code)
      return nil if code.nil?
      attribute = attribute_to_search_for_code(code)
      if attribute.nil?
        fail "could not find an attribute to search for code '#{code}'"
      end
      code = code.downcase # Codes are all ASCII
      query_collection.find do |region|
        region.send(attribute).downcase == code
      end
    end

    # Find a region by name.
    #
    # name - The String name to search for.
    # options - The Hash options used to modify the search (default:{}):
    #           :fuzzy - Whether to use fuzzy matching when finding a
    #                    matching name (optional, default: false)
    #           :case  - Whether or not the match is case-sensitive
    #                    (optional, default: false)
    #
    # Returns a region with the supplied name, or nil if none if found.
    def named(name, options={})
      case_fold = !options[:case] && name.respond_to?(:each_codepoint)
      # These only need to be built once
      name = case_fold ? name.mb_chars.downcase.normalize : name
      # For now, "fuzzy" just means substring, optionally case-insensitive (the second argument looks for nil, not falseness)
      regexp = options[:fuzzy] ? Regexp.new(name, options[:case] ? nil : true) : nil

      query_collection.find do |region|
        found_literal = name === (case_fold && region.name ? region.name.mb_chars.downcase.normalize : region.name)
        found_literal || options[:fuzzy] && regexp === region.name
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 3 rubygems

Version Path
carmen-1.1.0 lib/carmen/querying.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/carmen-1.0.2/lib/carmen/querying.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/carmen-1.0.2/lib/carmen/querying.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/carmen-1.0.2/lib/carmen/querying.rb
carmen-1.0.2 lib/carmen/querying.rb
carmens-1.0.1 lib/carmen/querying.rb