Class: Terraformer::Primitive

Inherits:
Object
  • Object
show all
Defined in:
lib/terraformer.rb

Overview

abstract base class for terraformer objects. implements bbox and envelope.

Instance Method Summary collapse

Constructor Details

#initialize(*args) {|arg| ... } ⇒ Primitive

handles basic JSON parsing for terraformer object constructors.

Yields:

  • (arg)


74
75
76
77
78
79
80
81
# File 'lib/terraformer.rb', line 74

def initialize *args
  arg = args[0]
  arg = JSON.parse(arg) if String === arg

  raise ArgumentError.new "invalid argument(s): #{args}" unless Hash === arg
  raise ArgumentError.new "invalid type: #{arg['type']}" unless arg['type'] == self.type
  yield arg if block_given?
end

Instance Method Details

#bbox(type = :bbox) ⇒ Object

returns a bounding box array of values, with minimum axis values followed by maximum axis values.



99
100
101
# File 'lib/terraformer.rb', line 99

def bbox type = :bbox
  Bounds.bounds self.respond_to?(:geometry) ? self.geometry : self, type
end

#envelopeObject

returns a bounding envelope as a Hash of the geometry. the envelope has keys for coordinates x and y, and dimensions w and h.



92
93
94
# File 'lib/terraformer.rb', line 92

def envelope
  Bounds.envelope self.respond_to?(:geometry) ? self.geometry : self
end

#to_json(*args) ⇒ Object

base to_json implementation for all terraformer objects.



105
106
107
108
109
# File 'lib/terraformer.rb', line 105

def to_json *args
  h = self.to_hash *args
  args.pop if Hash === args.last
  h.to_json *args
end

#typeObject

terraformer object type as a String.



85
86
87
# File 'lib/terraformer.rb', line 85

def type
  self.class.to_s.sub 'Terraformer::', ''
end