Class: GeoHex::Unit

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

Overview

A Unit is a class of Zones. It has a ‘level`, `width`, `height` and overall `size`. Dimensions vary for different levels.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level) ⇒ Unit

Returns a new instance of Unit.

Parameters:

  • level (Integer)


36
37
38
# File 'lib/geo_hex/unit.rb', line 36

def initialize(level)
  @level = level
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



33
34
35
# File 'lib/geo_hex/unit.rb', line 33

def level
  @level
end

Class Method Details

.[](level) ⇒ GeoHex::Unit

Returns for the given level.

Parameters:

  • level (Integer)

Returns:



27
28
29
# File 'lib/geo_hex/unit.rb', line 27

def [](level)
  cache? ? store[level] ||= new(level) : new(level)
end

.cache=(value) ⇒ Object

Parameters:

  • value (Boolean)

    set to true to enable caching (recommended)



16
17
18
# File 'lib/geo_hex/unit.rb', line 16

def cache=(value)
  @cache = value
end

.cache?Boolean

Returns true if unit caching is enabled, defaults to false.

Returns:

  • (Boolean)

    true if unit caching is enabled, defaults to false



11
12
13
# File 'lib/geo_hex/unit.rb', line 11

def cache?
  @cache == true
end

.storeHash

Returns cache store.

Returns:

  • (Hash)

    cache store



21
22
23
# File 'lib/geo_hex/unit.rb', line 21

def store
  @store ||= {}
end

Instance Method Details

#heightFloat

Returns unit’s mercator height.

Returns:

  • (Float)

    unit’s mercator height



51
52
53
# File 'lib/geo_hex/unit.rb', line 51

def height
  @height ||= width * H_K
end

#sizeFloat

Returns unit’s mercator size.

Returns:

  • (Float)

    unit’s mercator size



41
42
43
# File 'lib/geo_hex/unit.rb', line 41

def size
  @size ||= H_BASE / 3**(level+3)
end

#widthFloat

Returns unit’s mercator width.

Returns:

  • (Float)

    unit’s mercator width



46
47
48
# File 'lib/geo_hex/unit.rb', line 46

def width
  @width ||= 6.0 * size
end