Class: Travlrmap::Points

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

Instance Method Summary collapse

Constructor Details

#initialize(types, sets, map) ⇒ Points

Returns a new instance of Points.



4
5
6
7
8
9
10
# File 'lib/travlrmap/points.rb', line 4

def initialize(types, sets, map)
  @types = types
  @sets = sets
  @map = map

  reset
end

Instance Method Details

#<<(point) ⇒ Object



38
39
40
41
42
# File 'lib/travlrmap/points.rb', line 38

def <<(point)
  raise("Can only store instances of Point") unless point.is_a?(Point)

  @points << point
end

#by_countryObject



48
49
50
51
52
# File 'lib/travlrmap/points.rb', line 48

def by_country
  @points.sort_by do |point|
    point[:country]
  end
end

#countriesObject



66
67
68
69
70
# File 'lib/travlrmap/points.rb', line 66

def countries
  @points.map do |point|
    point[:country]
  end.compact.sort.uniq
end

#eachObject



60
61
62
63
64
# File 'lib/travlrmap/points.rb', line 60

def each
  by_country.each do |point|
    yield(point)
  end
end

#files_for_set(set) ⇒ Object



140
141
142
143
# File 'lib/travlrmap/points.rb', line 140

def files_for_set(set)
  return Array(@sets[set][:data]) if set
  return Array(@map[:data])
end

#find_title(title) ⇒ Object



16
17
18
19
20
# File 'lib/travlrmap/points.rb', line 16

def find_title(title)
  @points.find_index do |point|
    point[:title] == title
  end
end

#has_title?(title) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/travlrmap/points.rb', line 12

def has_title?(title)
  !!find_title(title)
end

#load_directory(directory) ⇒ Object



155
156
157
158
159
# File 'lib/travlrmap/points.rb', line 155

def load_directory(directory)
  Dir.entries(directory).grep(/\.yaml$/).each do |points|
    points_from_data(load_file(File.join(directory, points)))
  end
end

#load_file(file) ⇒ Object



161
162
163
# File 'lib/travlrmap/points.rb', line 161

def load_file(file)
  YAML.load_file(file)
end

#load_points(set = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/travlrmap/points.rb', line 122

def load_points(set=nil)
  reset

  files_for_set(set).each do |file|
    if file.is_a?(Symbol)
      self.load_points(file)
    else
      file = point_file(file)

      if File.directory?(file)
        load_directory(file)
      else
        load_points_from_file(file)
      end
    end
  end
end

#load_points_from_file(file) ⇒ Object



118
119
120
# File 'lib/travlrmap/points.rb', line 118

def load_points_from_file(file)
  points_from_data(load_file(file))
end

#mapObject



54
55
56
57
58
# File 'lib/travlrmap/points.rb', line 54

def map
  by_country.map do |point|
    yield(point)
  end
end

#point_file(file) ⇒ Object



145
146
147
# File 'lib/travlrmap/points.rb', line 145

def point_file(file)
  File.join(File.join(APPROOT, "config", file))
end

#points_from_data(data) ⇒ Object



149
150
151
152
153
# File 'lib/travlrmap/points.rb', line 149

def points_from_data(data)
  data[:points].each do |point|
    @points << Point.new(point, @types)
  end
end

#replace!(title, point) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/travlrmap/points.rb', line 22

def replace!(title, point)
  raise("Can only store instances of Point") unless point.is_a?(Point)

  if idx = find_title(title)
    @points[idx] = point
  else
    raise("No point with title %s to replace" % title)
  end
end

#resetObject



44
45
46
# File 'lib/travlrmap/points.rb', line 44

def reset
  @points = []
end

#save_to_file(file) ⇒ Object



32
33
34
35
36
# File 'lib/travlrmap/points.rb', line 32

def save_to_file(file)
  File.open(file, "w") do |f|
    f.puts YAML.dump(:points => @points.map{|p| p.to_hash})
  end
end

#sizeObject Also known as: count



72
73
74
# File 'lib/travlrmap/points.rb', line 72

def size
  @points.size
end

#to_jsonObject



109
110
111
112
113
114
115
116
# File 'lib/travlrmap/points.rb', line 109

def to_json
  @points.map do |point|
    temp = point.to_hash
    temp[:popup_html] = point.to_html
    temp[:icon] = point.type[:icon]
    temp
  end.to_json
end

#to_kmlObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/travlrmap/points.rb', line 78

def to_kml
  require 'ruby_kml'

  kml = KMLFile.new
  document = KML::Document.new(:name => "Travlrmap Data")
  folder = KML::Folder.new(:name => "Countries")
  folders = {}

  @types.each do |k, t|
    document.styles << KML::Style.new(
      :id         => Util.kml_style_url(k, :type),
      :icon_style => KML::IconStyle.new(:icon => KML::Icon.new(:href => t[:icon]))
    )
  end

  self.each do |point|
    unless folders[point[:country]]
      folder.features << folders[point[:country]] = KML::Folder.new(:name => point[:country])
    end

    f = folders[point[:country]]

    f.features << point.to_placemark
  end

  document.features << folder
  kml.objects << document

  kml.render
end