Skip to Content Skip to Search

Custom mongoid field type

Methods
C
D
E
M
N

Attributes

[R] city
[R] country
[R] latitude
[R] longitude
[R] state
[R] zip

Class Public methods

demongoize(attrs)

Get the object as it was stored in the database, and instantiate this custom class from it.

# File app/models/analytics/location.rb, line 8
def self.demongoize(attrs)
  Location.new(attrs.presence || {})
end

evolve(object)

Converts the object that was supplied to a criteria and converts it into a database friendly form.

# File app/models/analytics/location.rb, line 29
def self.evolve(object)
  case object
  when Location
    object.mongoize
  when nil
    {}
  else
    object.try(:to_h) || {}
  end
end

mongoize(object)

Takes any possible object and converts it to how it would be stored in the database.

# File app/models/analytics/location.rb, line 14
def self.mongoize(object)
  case object
  when Location
    object.mongoize
  when Hash
    Location.new(object).mongoize
  when nil
    Location.new({}).mongoize
  else
    object
  end
end

new(attrs = {})

# File app/models/analytics/location.rb, line 40
def initialize(attrs = {})
  attrs ||= {}

  @city = attrs.fetch(:city, "Unknown")
  @state = attrs.fetch(:state, "Unknown")
  @zip = attrs.fetch(:zip, "Unknown")
  @country = attrs.fetch(:country, "Unknown")
  @latitude = attrs.fetch(:coords, []).last || attrs.fetch(:latitude, 0.0)
  @longitude = attrs.fetch(:coords, []).first || attrs.fetch(:longitude, 0.0)
end

Instance Public methods

coords()

# File app/models/analytics/location.rb, line 51
def coords
  [longitude, latitude]
end

mongoize()

# File app/models/analytics/location.rb, line 55
def mongoize
  {
    city: city,
    state: state,
    zip: zip,
    country: country,
    coords: coords
  }
end