Class: Itinerary::Record
- Inherits:
-
HashStruct
- Object
- HashStruct
- Itinerary::Record
- Defined in:
- lib/itinerary/record.rb
Defined Under Namespace
Classes: Field
Constant Summary collapse
- MaxFieldNameLength =
@@fields.map { |k, f| f.name.length }.max
- @@fields =
include Geocoder::Model::Record
{}
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
- .define_field(key, options = {}) ⇒ Object
- .field(key) ⇒ Object
- .field_keys ⇒ Object
- .fields ⇒ Object
- .load(path, options = {}) ⇒ Object
Instance Method Summary collapse
- #city ⇒ Object
- #clean! ⇒ Object
- #contacted? ⇒ Boolean
- #convert ⇒ Object
- #coordinates ⇒ Object
- #country ⇒ Object
- #declined? ⇒ Boolean
- #edit(options = {}) ⇒ Object
- #fields_to_html(field_keys) ⇒ Object
- #geocode ⇒ Object
- #geocoded? ⇒ Boolean
- #latitude ⇒ Object
- #longitude ⇒ Object
- #make_path(root) ⇒ Object
- #match(query, itinerary) ⇒ Object
- #name ⇒ Object
- #near(coords, radius) ⇒ Object
- #open_in_editor_link ⇒ Object
- #print_diff(other) ⇒ Object
- #save! ⇒ Object
- #state ⇒ Object
- #string_to_key(str) ⇒ Object
- #to_html(options = {}) ⇒ Object
- #to_tab(options = {}) ⇒ Object
- #to_text(options = {}) ⇒ Object
- #to_visit? ⇒ Boolean
- #visited? ⇒ Boolean
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
39 40 41 |
# File 'lib/itinerary/record.rb', line 39 def path @path end |
Class Method Details
.define_field(key, options = {}) ⇒ Object
23 24 25 |
# File 'lib/itinerary/record.rb', line 23 def self.define_field(key, ={}) @@fields[key] = Field.new(key, ) end |
.field(key) ⇒ Object
27 28 29 |
# File 'lib/itinerary/record.rb', line 27 def self.field(key) @@fields[key] end |
.field_keys ⇒ Object
35 36 37 |
# File 'lib/itinerary/record.rb', line 35 def self.field_keys @@fields.keys end |
.fields ⇒ Object
31 32 33 |
# File 'lib/itinerary/record.rb', line 31 def self.fields @@fields end |
.load(path, options = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 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 |
# File 'lib/itinerary/record.rb', line 58 def self.load(path, ={}) io = path.open rec = new(:path => path) last_key = nil while !io.eof? && (line = io.readline) do case line.chomp when '' break when /^\s*(\w+):\s*(.*)\s*$/ field_name, value = $1, $2.strip next if value.empty? field = @@fields[field_name.to_sym] || @@fields.values.find { |f| f.name == field_name } \ or raise "#{path}: Unknown field: #{field_name.inspect}" if field.type == URI #FIXME: sometimes this field is a space-separated list # value = URI.parse(value) elsif field.type == Object value = eval(value) end rec[field.key] = value last_key = field.key when /^\s+(.+)\s*$/ raise "#{path}: Can't continue line without initial key or name" unless last_key if rec[last_key] rec[last_key] += ' ' + $1 else rec[last_key] = $1 end else warn "#{path}: Bad line: #{line.inspect}" next end end notes = io.read unless notes.empty? if rec.notes rec.notes += "\n\n" + notes else rec.notes = notes end end io.close rec end |
Instance Method Details
#city ⇒ Object
109 110 111 |
# File 'lib/itinerary/record.rb', line 109 def city geocoding[:city] if geocoded? end |
#clean! ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/itinerary/record.rb', line 208 def clean! @@fields.values.select { |f| f.type == String }.each do |field| if (value = self[field.key]) value = value.gsub(/[[:space:]]+/, ' ').strip end if value.nil? || value.empty? delete(field.key) else self[field.key] = value end end end |
#contacted? ⇒ Boolean
169 170 171 |
# File 'lib/itinerary/record.rb', line 169 def contacted? !contacted.nil? end |
#convert ⇒ Object
221 222 223 |
# File 'lib/itinerary/record.rb', line 221 def convert false end |
#coordinates ⇒ Object
129 130 131 |
# File 'lib/itinerary/record.rb', line 129 def coordinates [latitude, longitude] if geocoded? end |
#country ⇒ Object
117 118 119 |
# File 'lib/itinerary/record.rb', line 117 def country geocoding[:country] if geocoded? end |
#declined? ⇒ Boolean
173 174 175 |
# File 'lib/itinerary/record.rb', line 173 def declined? !declined.nil? end |
#edit(options = {}) ⇒ Object
319 320 321 322 323 324 |
# File 'lib/itinerary/record.rb', line 319 def edit(={}) system( 'subl', [:wait] ? '--wait' : (), @path.to_s) end |
#fields_to_html(field_keys) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/itinerary/record.rb', line 277 def fields_to_html(field_keys) fields_html = {} field_keys.each do |key| field = @@fields[key] or raise "Unknown field: #{key.inspect}" display_name = field.name if (value = self[field.key]) case field.key when :geocoding display_name = 'Location' value = [city, state].compact.join(', ') when :contacted, :declined, :visited value = value.strftime('%-d %b %Y') when :description html = Builder::XmlMarkup.new html.i(value) value = html when :uri display_name = 'Website' #FIXME: anything beyond first URI is ignored -- make into list of links value = URI.parse(value.split(/\s+/).first) if value.kind_of?(String) end html = Builder::XmlMarkup.new case value when URI html.a(value.to_s, :href => value.to_s) when Builder::XmlMarkup html << value else html.text!(value) end fields_html[display_name] = html.target! end end fields_html end |
#geocode ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/itinerary/record.rb', line 137 def geocode results = Geocoder.search(address) if (result = results.first) self.geocoding = { :city => result.city, :state => result.state_code, :country => result.country_code, :latitude => result.coordinates[0], :longitude => result.coordinates[1], } true else false end end |
#geocoded? ⇒ Boolean
133 134 135 |
# File 'lib/itinerary/record.rb', line 133 def geocoded? !geocoding.nil? end |
#latitude ⇒ Object
121 122 123 |
# File 'lib/itinerary/record.rb', line 121 def latitude geocoding[:latitude] if geocoded? end |
#longitude ⇒ Object
125 126 127 |
# File 'lib/itinerary/record.rb', line 125 def longitude geocoding[:longitude] if geocoded? end |
#make_path(root) ⇒ Object
198 199 200 201 202 203 204 205 206 |
# File 'lib/itinerary/record.rb', line 198 def make_path(root) path = root.dup raise "Not geocoded" unless geocoded? path += string_to_key(country) if country path += string_to_key(state) if state raise "Can't make key from empty name" unless name path += string_to_key(name) path end |
#match(query, itinerary) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/itinerary/record.rb', line 177 def match(query, itinerary) case query when Pathname query == path when String rpath = path.relative_path_from(itinerary.entries_path).to_s File.fnmatch(query, rpath) else raise "Don't know how to query on #{query.inspect}" end end |
#name ⇒ Object
105 106 107 |
# File 'lib/itinerary/record.rb', line 105 def name organization || person end |
#near(coords, radius) ⇒ Object
153 154 155 156 157 158 159 |
# File 'lib/itinerary/record.rb', line 153 def near(coords, radius) if geocoded? && (distance = Haversine.distance(*coords, latitude, longitude).to_miles) <= radius distance else nil end end |
#open_in_editor_link ⇒ Object
326 327 328 |
# File 'lib/itinerary/record.rb', line 326 def open_in_editor_link URI.parse("subl://open/?url=file://#{URI.escape(full_path)}") end |
#print_diff(other) ⇒ Object
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/itinerary/record.rb', line 330 def print_diff(other) if self != other puts puts "< #{self.path}" puts "> #{other.path}" (self.keys + other.keys).sort.uniq.each do |key| if self[key] != other[key] puts "\t" + "#{key}:" if self[key] && !other[key] puts "\t\t" + "- #{self[key].inspect}" elsif !self[key] && other[key] puts "\t\t" + "+ #{other[key].inspect}" elsif self[key] != other[key] puts "\t\t" + "< #{self[key].inspect}" puts "\t\t" + "> #{other[key].inspect}" end end end end end |
#save! ⇒ Object
313 314 315 316 317 |
# File 'lib/itinerary/record.rb', line 313 def save! raise "Record has no path" unless @path @path.dirname.mkpath unless @path.dirname.exist? @path.open('w') { |io| io.write(to_text) } end |
#state ⇒ Object
113 114 115 |
# File 'lib/itinerary/record.rb', line 113 def state geocoding[:state] if geocoded? end |
#string_to_key(str) ⇒ Object
189 190 191 192 193 194 195 196 |
# File 'lib/itinerary/record.rb', line 189 def string_to_key(str) key = str.dup key.downcase! key.gsub!(/[^\w]+/, '-') key.sub!(/^-+/, '') key.sub!(/-+$/, '') key end |
#to_html(options = {}) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/itinerary/record.rb', line 254 def to_html(={}) use_dl = [:use_dl] fields_html = fields_to_html([:field_keys] || self.field_keys) html = Builder::XmlMarkup.new html.h2(name) if use_dl html.dl do fields_html.each do |display_name, h| html.dt(display_name) html.dd << h end end else fields_html.each do |display_name, h| html.p do html.b("#{display_name}: ") html << h end end end html.target! end |
#to_tab(options = {}) ⇒ Object
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/itinerary/record.rb', line 243 def to_tab(={}) field_keys = [:field_keys] || self.field_keys field_keys.map { |k| @@fields[k] }.map do |field| value = self[field.key] || '' if value =~ /\n/ value = value.gsub(/\n+/, ' | ') end value end.join("\t") end |
#to_text(options = {}) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/itinerary/record.rb', line 225 def to_text(={}) t = StringIO.new keys = [:field_keys] || @@fields.keys keys.each do |key| next if key == :notes value = self[key] or next if value =~ /\n/ value = "\n" + value.gsub(/^/, "\t") end field = @@fields[key] t.puts "%-#{MaxFieldNameLength + 1}.#{MaxFieldNameLength + 1}s %s" % [(field ? field.name : key.to_s.capitalize) + ':', value] end t.puts t.puts notes if notes && keys.include?(:notes) t.rewind t.read end |
#to_visit? ⇒ Boolean
165 166 167 |
# File 'lib/itinerary/record.rb', line 165 def to_visit? !visited.nil? && visited >= DateTime.now end |
#visited? ⇒ Boolean
161 162 163 |
# File 'lib/itinerary/record.rb', line 161 def visited? !visited.nil? && visited < DateTime.now end |