Sha256: 27cd1f02e9f9c445fa9a2785c7e189c9e35b4b2825fbea962667be6dca546918

Contents?: true

Size: 774 Bytes

Versions: 2

Compression:

Stored size: 774 Bytes

Contents

#
# Mongoid fields extension
#
# field :foo, :delegate => {opts
#
Mongoid::Fields.option :delegate do |model, field, options|
  options = {} unless options.is_a?(Hash)
  x_meth = options[:x] || :x
  y_meth = options[:y] || :y

  model.instance_eval do
    define_method x_meth do
      self[field.name][0]
    end

    define_method y_meth do
      self[field.name][1]
    end

    define_method "#{x_meth}=" do |arg|
      # HACK: Mongoid has detecting an Array changed
      # self[field.name][0] = arg
      send("#{field.name}=", [arg, self[field.name][1]])
    end

    define_method "#{y_meth}=" do |arg|
      # self[field.name][1] = arg
      # self[field.name] = [self[field.name][0], arg]
      send("#{field.name}=", [self[field.name][0], arg])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongoid-geospatial-5.1.0 lib/mongoid/geospatial/helpers/delegate.rb
mongoid-geospatial-5.0.0 lib/mongoid/geospatial/helpers/delegate.rb