class Rozi::NameSearchTextFile

A thin layer above File that handles reading and writing of names to name search text files

Public Instance Methods

write(enumerable) click to toggle source

Writes an enumerable of {Rozi::Name} objects to the file

@param [Enumerable] enumerable @return [nil]

# File lib/rozi/name_search.rb, line 72
def write(enumerable)
  enumerable.each { |name|
    write_name name
  }

  nil
end
write_name(name) click to toggle source

Writes a {Rozi::Name} to the file

@note If no properties have been written to the file before this method is

called, a default set of properties will be automatically written to the
file first

@param [Rozi::Name] name @return [nil]

# File lib/rozi/name_search.rb, line 89
def write_name(name)
  ensure_properties

  @file.write serialize_name(name)
  @file.write "\n"

  nil
end
write_properties(properties) click to toggle source

Writes name search properties to the file

The file must be empty when this method is called!

@raise [RuntimeError] if the file isn’t empty @param [NameSearchProperties] properties @return [nil]

# File lib/rozi/name_search.rb, line 107
def write_properties(properties)
  if @file.size > 0
    raise "Can't write file properties, file is not empty"
  end

  @file.write serialize_properties(properties)
  @file.write "\n"

  nil
end

Private Instance Methods

ensure_properties() click to toggle source

Ensures that properties have been written to the file

# File lib/rozi/name_search.rb, line 123
def ensure_properties
  return if @properties_written

  @properties_written = true

  if @file.size == 0
    write_properties NameSearchProperties.new
  end
end
serialize_name(name) click to toggle source
# File lib/rozi/name_search.rb, line 133
def serialize_name(name)
  if not name.name or not name.latitude or not name.longitude
    fail ArgumentError, "name, latitude and longitude must be set!"
  end

  feature_code = name.feature_code || ""

  if name.name.include?(",") or feature_code.include?(",")
    fail ArgumentError, "Text cannot contain commas"
  end

  "%s,%s,%s,%s,%s" % [
    name.name, name.feature_code, name.zone,
    name.latitude.round(6), name.longitude.round(6)
  ]
end
serialize_properties(properties) click to toggle source
# File lib/rozi/name_search.rb, line 150
def serialize_properties(properties)
  out = ""

  if properties.comment
    properties.comment.each_line { |line|
      out << ";#{line.chomp}\n"
    }
  end

  out << "#1,"

  if properties.utm
    out << "UTM,#{properties.utm_zone}"

    if properties.hemisphere
      out << ",#{properties.hemisphere}"
    end
  else
    out << ","
  end

  out << "\n#2,#{properties.datum}"

  return out
end