Class: Travlrmap::Points
- Inherits:
-
Object
- Object
- Travlrmap::Points
- Defined in:
- lib/travlrmap/points.rb
Instance Method Summary collapse
- #<<(point) ⇒ Object
- #by_country ⇒ Object
- #countries ⇒ Object
- #each ⇒ Object
- #files_for_set(set) ⇒ Object
- #find_title(title) ⇒ Object
- #has_title?(title) ⇒ Boolean
-
#initialize(types, sets, map) ⇒ Points
constructor
A new instance of Points.
- #load_directory(directory) ⇒ Object
- #load_file(file) ⇒ Object
- #load_points(set = nil) ⇒ Object
- #load_points_from_file(file) ⇒ Object
- #map ⇒ Object
- #point_file(file) ⇒ Object
- #points_from_data(data) ⇒ Object
- #replace!(title, point) ⇒ Object
- #reset ⇒ Object
- #save_to_file(file) ⇒ Object
- #size ⇒ Object (also: #count)
- #to_json ⇒ Object
- #to_kml ⇒ Object
Constructor Details
#initialize(types, sets, map) ⇒ Points
Returns a new instance of Points.
3 4 5 6 7 8 9 |
# File 'lib/travlrmap/points.rb', line 3 def initialize(types, sets, map) @types = types @sets = sets @map = map reset end |
Instance Method Details
#<<(point) ⇒ Object
37 38 39 40 41 |
# File 'lib/travlrmap/points.rb', line 37 def <<(point) raise("Can only store instances of Point") unless point.is_a?(Point) @points << point end |
#by_country ⇒ Object
47 48 49 50 51 |
# File 'lib/travlrmap/points.rb', line 47 def by_country @points.sort_by do |point| point[:country] end end |
#countries ⇒ Object
65 66 67 68 69 |
# File 'lib/travlrmap/points.rb', line 65 def countries @points.map do |point| point[:country] end.compact.sort.uniq end |
#each ⇒ Object
59 60 61 62 63 |
# File 'lib/travlrmap/points.rb', line 59 def each by_country.each do |point| yield(point) end end |
#files_for_set(set) ⇒ Object
139 140 141 142 |
# File 'lib/travlrmap/points.rb', line 139 def files_for_set(set) return Array(@sets[set][:data]) if set return Array(@map[:data]) end |
#find_title(title) ⇒ Object
15 16 17 18 19 |
# File 'lib/travlrmap/points.rb', line 15 def find_title(title) @points.find_index do |point| point[:title] == title end end |
#has_title?(title) ⇒ Boolean
11 12 13 |
# File 'lib/travlrmap/points.rb', line 11 def has_title?(title) !!find_title(title) end |
#load_directory(directory) ⇒ Object
154 155 156 157 158 |
# File 'lib/travlrmap/points.rb', line 154 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
160 161 162 |
# File 'lib/travlrmap/points.rb', line 160 def load_file(file) YAML.load_file(file) end |
#load_points(set = nil) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/travlrmap/points.rb', line 121 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
117 118 119 |
# File 'lib/travlrmap/points.rb', line 117 def load_points_from_file(file) points_from_data(load_file(file)) end |
#map ⇒ Object
53 54 55 56 57 |
# File 'lib/travlrmap/points.rb', line 53 def map by_country.map do |point| yield(point) end end |
#point_file(file) ⇒ Object
144 145 146 |
# File 'lib/travlrmap/points.rb', line 144 def point_file(file) File.join(File.join(APPROOT, "config", file)) end |
#points_from_data(data) ⇒ Object
148 149 150 151 152 |
# File 'lib/travlrmap/points.rb', line 148 def points_from_data(data) data[:points].each do |point| @points << Point.new(point, @types) end end |
#replace!(title, point) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/travlrmap/points.rb', line 21 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 |
#reset ⇒ Object
43 44 45 |
# File 'lib/travlrmap/points.rb', line 43 def reset @points = [] end |
#save_to_file(file) ⇒ Object
31 32 33 34 35 |
# File 'lib/travlrmap/points.rb', line 31 def save_to_file(file) File.open(file, "w") do |f| f.puts YAML.dump(:points => @points.map{|p| p.to_hash}) end end |
#size ⇒ Object Also known as: count
71 72 73 |
# File 'lib/travlrmap/points.rb', line 71 def size @points.size end |
#to_json ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/travlrmap/points.rb', line 108 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_kml ⇒ Object
77 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 |
# File 'lib/travlrmap/points.rb', line 77 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 |