Sha256: fc3c8ddb1780384cef7616477bd9ee4f4820c0c4525af1012a07aaa48eb77a12

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

module Graticule
  
  # Used to compare the precision of different geocoded locations
  class Precision
    include Comparable
    attr_reader :name
    
    NAMES = [
      :unknown,
      :country,
      :region,
      :locality,
      :postal_code,
      :street,
      :address,
      :premise
    ]
    
    def initialize(name)
      @name = name.to_sym
      raise ArgumentError, "#{name} is not a valid precision. Use one of #{NAMES.inspect}" unless NAMES.include?(@name)
    end
    
    Unknown    = Precision.new(:unknown)
    Country    = Precision.new(:country)
    Region     = Precision.new(:region)
    Locality   = Precision.new(:locality)
    PostalCode = Precision.new(:postal_code)
    Street     = Precision.new(:street)
    Address    = Precision.new(:address)
    Premise    = Precision.new(:premise)
    
    def to_s
      @name.to_s
    end
    
    def <=>(other)
      other = Precision.new(other) unless other.is_a?(Precision)
      NAMES.index(self.name) <=> NAMES.index(other.name)
    end
    
    def ==(other)
      (self <=> other) == 0
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graticule-2.0.1 lib/graticule/precision.rb
graticule-2.0.0 lib/graticule/precision.rb
graticule-1.0.0.pre2 lib/graticule/precision.rb