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



95
96
97
# File 'lib/rgeo/feature/mixins.rb', line 95

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_)


113
114
115
# File 'lib/rgeo/feature/mixins.rb', line 113

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)


104
105
106
# File 'lib/rgeo/feature/mixins.rb', line 104

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_)


123
124
125
# File 'lib/rgeo/feature/mixins.rb', line 123

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_)


133
134
135
# File 'lib/rgeo/feature/mixins.rb', line 133

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