Class: ActiveRecord::ConnectionAdapters::MysqlSpatialAdapter::SpatialColumn

Inherits:
ConnectionAdapters::MysqlColumn
  • Object
show all
Defined in:
lib/active_record/connection_adapters/mysqlspatial_adapter/spatial_column.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_, default_, sql_type_ = nil, null_ = true) ⇒ SpatialColumn

Returns a new instance of SpatialColumn.



49
50
51
52
53
54
55
56
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/spatial_column.rb', line 49

def initialize(name_, default_, sql_type_=nil, null_=true)
  super(name_, default_, sql_type_, null_)
  @geometric_type = ::RGeo::ActiveRecord.geometric_type_from_name(sql_type_)
  if type == :spatial
    @limit = {:type => @geometric_type.type_name.underscore}
  end
  @ar_class = ::ActiveRecord::Base
end

Instance Attribute Details

#geometric_type ⇒ Object (readonly)

Returns the value of attribute geometric_type.



64
65
66
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/spatial_column.rb', line 64

def geometric_type
  @geometric_type
end

Class Method Details

.convert_to_geometry(input_, ar_class_, column_) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/spatial_column.rb', line 94

def self.convert_to_geometry(input_, ar_class_, column_)
  case input_
  when ::RGeo::Feature::Geometry
    factory_ = ar_class_.rgeo_factory_for_column(column_, :srid => input_.srid)
    ::RGeo::Feature.cast(input_, factory_)
  when ::String
    marker_ = input_[4,1]
    if marker_ == "\x00" || marker_ == "\x01"
      factory_ = ar_class_.rgeo_factory_for_column(column_, :srid => input_[0,4].unpack(marker_ == "\x01" ? 'V' : 'N').first)
      ::RGeo::WKRep::WKBParser.new(factory_).parse(input_[4..-1])
    else
      factory_ = ar_class_.rgeo_factory_for_column(column_)
      ::RGeo::WKRep::WKTParser.new(factory_, :support_ewkt => true).parse(input_)
    end
  else
    nil
  end
end

Instance Method Details

#klass ⇒ Object



72
73
74
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/spatial_column.rb', line 72

def klass
  type == :spatial ? ::RGeo::Feature::Geometry : super
end

#set_ar_class(val_) ⇒ Object



59
60
61
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/spatial_column.rb', line 59

def set_ar_class(val_)
  @ar_class = val_
end

#spatial? ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/spatial_column.rb', line 67

def spatial?
  type == :spatial
end

#type_cast(value_) ⇒ Object



77
78
79
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/spatial_column.rb', line 77

def type_cast(value_)
  type == :spatial ? SpatialColumn.convert_to_geometry(value_, @ar_class, name) : super
end

#type_cast_code(var_name_) ⇒ Object



82
83
84
# File 'lib/active_record/connection_adapters/mysqlspatial_adapter/spatial_column.rb', line 82

def type_cast_code(var_name_)
  type == :spatial ? "::ActiveRecord::ConnectionAdapters::MysqlSpatialAdapter::SpatialColumn.convert_to_geometry(#{var_name_}, self.class, #{name.inspect})" : super
end