Module: SpatialAdapter::SpatialColumn

Included in:
ActiveRecord::ConnectionAdapters::SpatialMysqlColumn, ActiveRecord::ConnectionAdapters::SpatialPostgreSQLColumn
Defined in:
lib/spatial_adapter/common/spatial_column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#geometry_typeObject (readonly)

Returns the value of attribute geometry_type.



3
4
5
# File 'lib/spatial_adapter/common/spatial_column.rb', line 3

def geometry_type
  @geometry_type
end

#sridObject (readonly)

Returns the value of attribute srid.



3
4
5
# File 'lib/spatial_adapter/common/spatial_column.rb', line 3

def srid
  @srid
end

#with_mObject (readonly)

Returns the value of attribute with_m.



3
4
5
# File 'lib/spatial_adapter/common/spatial_column.rb', line 3

def with_m
  @with_m
end

#with_zObject (readonly)

Returns the value of attribute with_z.



3
4
5
# File 'lib/spatial_adapter/common/spatial_column.rb', line 3

def with_z
  @with_z
end

Instance Method Details

#geographic?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/spatial_adapter/common/spatial_column.rb', line 13

def geographic?
  false
end

#initialize(name, default, sql_type = nil, null = true, srid = -1,, with_z = false, with_m = false) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/spatial_adapter/common/spatial_column.rb', line 5

def initialize(name, default, sql_type = nil, null = true, srid=-1, with_z=false, with_m=false)
  super(name, default, sql_type, null)
  @geometry_type = geometry_simplified_type(@sql_type)
  @srid = srid
  @with_z = with_z
  @with_m = with_m
end

#klassObject

Redefines klass to add support for geometries alias_method :klass_without_spatial, :klass



43
44
45
46
47
48
49
# File 'lib/spatial_adapter/common/spatial_column.rb', line 43

def klass
  if @geometry_type.nil?
    super
  else
    GeoRuby::SimpleFeatures::Geometry
  end
end

#type_cast(value) ⇒ Object

Redefines type_cast to add support for geometries alias_method :type_cast_without_spatial, :type_cast



19
20
21
22
23
24
25
26
# File 'lib/spatial_adapter/common/spatial_column.rb', line 19

def type_cast(value)
  return nil if value.nil?
  if @geometry_type.nil?
    super
  else
    self.class.string_to_geometry(value)
  end
end

#type_cast_code(var_name) ⇒ Object

Redefines type_cast_code to add support for geometries.

WARNING : Since ActiveRecord keeps only the string values directly returned from the database, it translates from these to the correct types everytime an attribute is read (using the code returned by this method), which is probably ok for simple types, but might be less than efficient for geometries. Also you cannot modify the geometry object returned directly or your change will not be saved. alias_method :type_cast_code_without_spatial, :type_cast_code



32
33
34
35
36
37
38
# File 'lib/spatial_adapter/common/spatial_column.rb', line 32

def type_cast_code(var_name)
  if @geometry_type.nil?
    super
  else
    "#{self.class.name}.string_to_geometry(#{var_name})"
  end
end