Class: ActiveRecordSpatial::SpatialFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord-spatial/spatial_function.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :column => ActiveRecordSpatial.default_column_name,
  :use_index => true
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ SpatialFunction

Returns a new instance of SpatialFunction.



9
10
11
# File 'lib/activerecord-spatial/spatial_function.rb', line 9

def initialize(klass)
  @klass = klass
end

Class Method Details

.additional_ordering(*args) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/activerecord-spatial/spatial_function.rb', line 180

def additional_ordering(*args)
  options = args.extract_options!

  desc = if args.first == :desc
    true
  else
    options[:desc]
  end

  ''.tap do |ret|
      if desc
        ret << ' DESC'
      end

      if options[:nulls]
        ret << " NULLS #{options[:nulls].to_s.upcase}"
      end
  end
end

.build!(klass, *args) ⇒ Object



13
14
15
# File 'lib/activerecord-spatial/spatial_function.rb', line 13

def self.build!(klass, *args)
  new(klass).build_function_call(*args)
end

Instance Method Details

#build_function_call(function, *args) ⇒ Object



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/activerecord-spatial/spatial_function.rb', line 17

def build_function_call(function, *args)
  options = default_options(args.extract_options!)

  geom = options.fetch(:geom_arg, args.first)
  args = Array.wrap(options.fetch(:args, args.from(1)))

  column_name = self.column_name(options[:column])
  first_geom_arg = self.wrap_column_or_geometry(
    @klass.arel_table[column_name],
    options[:column]
  )
  geom_args = [ first_geom_arg ]

  if geom.present?
    column_type = @klass.spatial_column_by_name(column_name).spatial_type
    column_srid = @klass.srid_for(column_name)

    unless geom.is_a?(Hash)
      geom_arg = read_geos(geom, column_srid)
      geom_srid = read_geom_srid(geom_arg, column_type)
      geom_args << self.set_srid_or_transform(column_srid, geom_srid, geom_arg, column_type)
    else
      klass = if geom[:class]
        geom[:class]
      elsif geom[:class_name]
        geom[:class_name].classify.constantize
      else
        raise ArgumentError.new("Need either a :class or :class_name option to determine the class.")
      end

      if geom[:value]
        geom_arg = read_geos(geom[:value], column_srid)
        geom_srid = read_geom_srid(geom_arg, column_type)
      else
        geom_arg = geom
        geom_srid = klass.srid_for(self.column_name(geom[:column]))
      end

      transformed_geom = self.set_srid_or_transform(column_srid, geom_srid, geom_arg, column_type)
      geom_args << self.wrap_column_or_geometry(transformed_geom, geom)
    end
  end

  if options[:invert] && geom_args.length > 1
    geom_args.reverse!
  end

  ret = Arel::Nodes::NamedFunction.new(
    function_name(function, options[:use_index]),
    geom_args + args
  )

  if options[:allow_null]
    ret = ret.or(first_geom_arg.eq(nil))
  end

  ret
end