Module: Mongoid::Fields::ClassMethods
- Defined in:
- lib/mongoid/geo/fields.rb
Overview
:nodoc
Instance Method Summary collapse
Instance Method Details
#create_accessors(name, meth, options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mongoid/geo/fields.rb', line 5 def create_accessors(name, meth, = {}) generated_field_methods.module_eval do if [ Time, DateTime ].include?([:type]) define_method(meth) { Time.get(read_attribute(name)) } else define_method(meth) { read_attribute(name) } end if [:type] == Array && [:geo] lat_meth = [:lat] || "lat" lng_meth = [:lng] || "lng" define_method(lat_meth) { read_attribute(name).try(:[], Mongoid::Geo.lat_index) } define_method(lng_meth) { read_attribute(name).try(:[], Mongoid::Geo.lng_index) } define_method "#{lat_meth}=" do |value| write_attribute(name, [nil,nil]) if !read_attribute(name).present? send(name)[Mongoid::Geo.lat_index] = value end define_method "#{lng_meth}=" do |value| write_attribute(name, [nil,nil]) if !read_attribute(name).present? send(name)[Mongoid::Geo.lng_index] = value end end define_method("#{meth}=") do |value| if [:geo] self.class.send :field, :distance, :type => Float self.class.send :field, :fromHash, :type => String self.class.send :field, :fromPoint, :type => Array end if [:type] == Array && [: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 value.reverse! if Mongoid::Geo.spherical && value && value.kind_of?(Array) write_attribute(name, value) end define_method("#{meth}?") do attr = read_attribute(name) ([:type] == Boolean) ? attr == true : attr.present? end end end |