class PhonemeMaps
Public Instance Methods
directory()
click to toggle source
What directory are we searching in?
# File lib/phoneme_maps.rb, line 38 def directory FILE_DIRECTORY end
load(symbol)
click to toggle source
Takes a symbol, converts it into a file name, And attempts to load its contents Returns a Hash
# File lib/phoneme_maps.rb, line 33 def load symbol load_file "#{FILE_DIRECTORY}/#{symbol.to_s}.json" end
open_file_safely(path)
click to toggle source
Opens a file with `File.open` Raises an informative error if the file cannot be found
# File lib/phoneme_maps.rb, line 54 def open_file_safely path dir = path[0..path.rindex('/')] filename = path[ (path.rindex('/') + 1)..path.length ] begin return File.open path, 'r' rescue Errno::ENOENT raise "Unknown list name. Could not find file `#{filename}` in directory `#{dir}`.\n Is the file name spelled correctly, or altered somewhere in your code?\n Contents of directory: #{Dir.new(dir).entries}" end end
validate_json(text)
click to toggle source
Parses a string into JSON Raises an informative error if the JSON is malformed
# File lib/phoneme_maps.rb, line 44 def validate_json text begin return JSON.parse text rescue JSON::ParserError raise "JSON is not formatted properly.\nTry validating it at JSONlint.com (Look out for missing braces and missing/extra commas)\n File contents: #{text}" end end
Private Instance Methods
load_file(path)
click to toggle source
Loads the file from the supplied path, and parses it with `JSON.parse` Returns a hash
# File lib/phoneme_maps.rb, line 72 def load_file path text = "" open_file_safely(path). each_line(){|line| text << line }. close validate_json text end