Sha256: e627f53f60deb2e080a516d68d53fc2bdf41916cae13ffb6a88de7da381cfc48
Contents?: true
Size: 1.33 KB
Versions: 1
Compression:
Stored size: 1.33 KB
Contents
require 'json' require 'zlib' module BNF class Tree def initialize(version) if version == :bnf filename = File.expand_path('../../../data/bnf.json.gz', __FILE__) elsif version == :bnfc filename = File.expand_path('../../../data/bnfc.json.gz', __FILE__) else raise 'Must use :bnf or :bnfc to choose BNF version to load' end gzipped_file = File.open(filename) file = Zlib::GzipReader.new(gzipped_file) @tree = JSON.parse(file.read) end def each @tree['entries'].each do |h| yield h end end def find(match) if match.is_a? String return find_by_string(match) elsif match.is_a? Regexp return find_by_regex(match) else raise 'Tree.find(match) accepts a string or a regex' end end private def find_by_string(string) string = string.downcase.strip.gsub(/\s+/, ' ') @tree['entries'].each do |uri,entry| if entry['title'] && entry['title'].downcase.strip.gsub(/\s+/, ' ') == string return Heading.new(uri, entry) end end nil end def find_by_regex(regex) @tree['entries'].each do |uri,entry| if entry['title'] && entry['title'] =~ regex return Heading.new(uri, entry) end end nil end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
BNF-0.0.1 | lib/bnf/tree.rb |