Sha256: fe6d81cff68fb6007ae63884c71f507b920fa9dbae3cd1d5c93c8cf7edf264ba

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module Mongoid
  module Geospatial
    def self.from_array(ary)
      ary[0..1].map(&:to_f)
    end

    def self.from_hash(hsh)
      fail 'Hash must have at least 2 items' if hsh.size < 2
      [from_hash_x(hsh), from_hash_y(hsh)]
    end

    def self.from_hash_y(hsh)
      v = (Mongoid::Geospatial.lat_symbols & hsh.keys).first
      return hsh[v].to_f if !v.nil? && hsh[v]
      if Mongoid::Geospatial.lng_symbols.index(hsh.keys[1])
        fail "Hash cannot contain #{Mongoid::Geospatial.lng_symbols.inspect} "\
             "as second arg without #{Mongoid::Geospatial.lat_symbols.inspect}"
      end
      hsh.values[1].to_f
    end

    def self.from_hash_x(hsh)
      v = (Mongoid::Geospatial.lng_symbols & hsh.keys).first
      return hsh[v].to_f if !v.nil? && hsh[v]
      if Mongoid::Geospatial.lat_symbols.index(keys[0])
        fail "Hash cannot contain #{Mongoid::Geospatial.lat_symbols.inspect} "\
             "as first arg without #{Mongoid::Geospatial.lng_symbols.inspect}"
      end
      values[0].to_f
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-geospatial-5.0.0 spec/mongoid/geospatial/helpers/core_spec.rb