Module: Clumpy::ClusterBehavior

Included in:
Cluster
Defined in:
lib/clumpy/cluster_behavior.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#boundsObject

Returns the value of attribute bounds.



3
4
5
# File 'lib/clumpy/cluster_behavior.rb', line 3

def bounds
  @bounds
end

#latitudeObject

Returns the value of attribute latitude.



3
4
5
# File 'lib/clumpy/cluster_behavior.rb', line 3

def latitude
  @latitude
end

#longitudeObject

Returns the value of attribute longitude.



3
4
5
# File 'lib/clumpy/cluster_behavior.rb', line 3

def longitude
  @longitude
end

#pointsObject

Returns the value of attribute points.



3
4
5
# File 'lib/clumpy/cluster_behavior.rb', line 3

def points
  @points
end

Instance Method Details

#as_jsonObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/clumpy/cluster_behavior.rb', line 24

def as_json(*)
  bounds =  {
    northeast: {
      latitude: @points.map(&:latitude).max,
      longitude: @points.map(&:longitude).max,
    },
    southwest: {
      latitude: @points.map(&:latitude).min,
      longitude: @points.map(&:longitude).min,
    }
  }

  {
    latitude:  latitude,
    longitude: longitude,
    size:      @points.size,
    bounds:    bounds,
    values:    value_list,
  }
end

#contains?(point) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/clumpy/cluster_behavior.rb', line 14

def contains?(point)
  @bounds.latitude.include?(point.latitude) &&
  @bounds.longitude.include?(point.longitude)
end

#initialize(point, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/clumpy/cluster_behavior.rb', line 5

def initialize(point, options = {})
  @latitude  = point.latitude
  @longitude = point.longitude
  @points    = [point]
  @options   = options

  @bounds    = Bounds.new(@latitude, @longitude, options[:width], options[:length])
end

#repositionObject



19
20
21
22
# File 'lib/clumpy/cluster_behavior.rb', line 19

def reposition
  @latitude  = @points.inject(0.0) { |m, p| m + p.latitude } / @points.size
  @longitude = @points.inject(0.0) { |m, p| m + p.longitude } / @points.size
end

#value_listObject



45
46
47
48
49
50
51
52
53
# File 'lib/clumpy/cluster_behavior.rb', line 45

def value_list
  case @options[:include_values]
  when true  then @points
  when false then []
  else
    values_threshold = @options[:values_threshold] || 10
    @points.size <= values_threshold ? @points : []
  end
end