Module: Thailand::Querying

Included in:
District, Province, RegionCollection
Defined in:
lib/thailand/querying.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#case_foldObject (readonly)

Find a region by code Returns a region with the supplied code, or nil if none is found



8
9
10
# File 'lib/thailand/querying.rb', line 8

def case_fold
  @case_fold
end

Instance Method Details

#coded(code) ⇒ Object



10
11
12
13
14
15
# File 'lib/thailand/querying.rb', line 10

def coded(code)
  return nil if code.nil?

  code = code.to_s.downcase # Codes are all ASCII
  query_collection.find { |region| region.send(:code).downcase == code }
end

#named(name, options = {}) ⇒ Object

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.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/thailand/querying.rb', line 25

def named(name, options = {})
  case_fold = case_insensitive?(name, options[:case])
  # These only need to be built once
  name = transform_downcase name, case_fold
  # 'fuzzy' just means substring, optionally case-insensitive (the second argument looks for nil, not falseness)
  regexp = options[:fuzzy] ? create_regexp(name, options[:case]) : nil

  query_collection.find do |region|
    found_literal = name === transform_downcase(region.name, case_fold)
    found_literal || options[:fuzzy] && regexp === region.name
  end
end