Class: RGeo::Feature::MixinCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/rgeo/feature/mixins.rb

Overview

MixinCollection is a mechanism for adding arbitrary methods to geometry objects.

Normally, geometry objects respond to the methods defined in the feature interface for that type of geometry; e.g. RGeo::Feature::Geometry, RGeo::Feature::Point, etc. Some implementations include additional methods specific to the implementation. However, occasionally it is desirable also to include custom methods allowing objects to function in different contexts. To do so, provide those methods in a mixin Module, and add it to an appropriate MixinCollection. A MixinCollection is simply a collection of mixin Modules, connected to geometry types, that are included in objects of that type.

There is a global collection, MixinCollection::GLOBAL, which manages mixins to be added to all implementations. In addition, individual implementation factories may provide additional local MixinCollection objects for mixins specific to objects created by that factory.

Each mixin module added to a MixinCollection is connected to a specific type, which controls to which objects that mixin is added. For example, a mixin connected to Point is added only to Point objects. A mixin connected to GeometryCollection is added to GeometryCollection objects as well as MultiPoint, MultiLineString, and MultiPolygon, since those are subtypes of GeometryCollection. To add a mixin to all objects, connect it to the Geometry base type.

Defined Under Namespace

Classes: TypeData

Constant Summary collapse

GLOBAL =

The global MixinCollection. Mixins added to this collection are added to all geometry objects for all implementations.

MixinCollection.new

Instance Method Summary collapse

Constructor Details

#initializeMixinCollection

Create a new empty MixinCollection


140
141
142
# File 'lib/rgeo/feature/mixins.rb', line 140

def initialize
  @types = {}
end

Instance Method Details

#add(type_, module_) ⇒ Object

Add a module connected to the given type.

Shorthand for:

for_type(type_).add(module_)

160
161
162
# File 'lib/rgeo/feature/mixins.rb', line 160

def add(type_, module_)
  for_type(type_).add(module_)
end

#for_type(type_) ⇒ Object

Returns a TypeData for the given type.

e.g. to add a module for point types, you can call:

for_type(::RGeo::Feature::Point).add(module)

150
151
152
# File 'lib/rgeo/feature/mixins.rb', line 150

def for_type(type_)
  (@types[type_] ||= TypeData.new(self, type_))
end

#include_in_class(type_, klass_, include_ancestry_ = false) ⇒ Object

A class that implements this type should call this method to get the appropriate mixins.

Shorthand for:

for_type(type_).include_in_class(klass_, include_ancestry_)

171
172
173
# File 'lib/rgeo/feature/mixins.rb', line 171

def include_in_class(type_, klass_, include_ancestry_=false)
  for_type(type_).include_in_class(klass_, include_ancestry_)
end

#include_in_object(type_, obj_, include_ancestry_ = false) ⇒ Object

An object that implements this type should call this method to get the appropriate mixins.

Shorthand for:

for_type(type_).include_in_object(obj_, include_ancestry_)

182
183
184
# File 'lib/rgeo/feature/mixins.rb', line 182

def include_in_object(type_, obj_, include_ancestry_=false)
  for_type(type_).include_in_object(obj_, include_ancestry_)
end