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



28
29
30
31
32
33
34
35
36
# File 'lib/clumpy/cluster_behavior.rb', line 28

def as_json(*)
  {
    latitude: latitude,
    longitude: longitude,
    size: points.size,
    bounds: bounds_as_json,
    values: value_list
  }
end

#bounds_as_jsonObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/clumpy/cluster_behavior.rb', line 48

def bounds_as_json
  {
    northeast: {
      latitude: bounds.latitude.max,
      longitude: bounds.longitude.max
    },
    southwest: {
      latitude: bounds.latitude.min,
      longitude: bounds.longitude.min
    }
  }
end

#contains?(point) ⇒ Boolean

Returns:

  • (Boolean)


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

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
13
14
15
16
# File 'lib/clumpy/cluster_behavior.rb', line 5

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

  side_length = options.fetch(:side_length) { 10 }
  @bounds    = OpenStruct.new(
    latitude: (latitude - side_length)..(latitude + side_length),
    longitude: (longitude - side_length)..(longitude + side_length)
  )
end

#repositionObject



23
24
25
26
# File 'lib/clumpy/cluster_behavior.rb', line 23

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



38
39
40
41
42
43
44
45
46
# File 'lib/clumpy/cluster_behavior.rb', line 38

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