Sha256: 57c791461379beca739601f37157b62054b86f9437b3614fac8475b3a666d79d

Contents?: true

Size: 1.27 KB

Versions: 1

Compression:

Stored size: 1.27 KB

Contents

module Mongoid #:nodoc
  # This module defines behaviour for fields.
  module Fields
    module ClassMethods #:nodoc
      def create_accessors(name, meth, options = {})
        generated_field_methods.module_eval do
          define_method(meth) { read_attribute(name) }
          define_method(meth) { read_attribute(name) }
          if options[:type] == Array && options[:geo]
            define_method(options[:lat] || "lat") { read_attribute(name)[0] }
            define_method(options[:lng] || "lng") { read_attribute(name)[1] }
          end
          define_method("#{meth}=") do |value|
            if options[:type] == Array && options[:geo]
              value = case value
              when String
                value.split(",").map(&:to_f)
              when Array
                value.compact.extend(Mongoid::Geo::Point).to_points
              else
                !value.nil? ? value.extend(Mongoid::Geo::Point).to_point : value
              end
              value = value[0..1] if !value.nil?
            end            
            write_attribute(name, value) 
          end
          define_method("#{meth}?") do
            attr = read_attribute(name)
            (options[:type] == Boolean) ? attr == true : attr.present?
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid_geo-0.1.7 lib/mongoid/geo/fields.rb