Module: Calyx::Format
- Defined in:
- lib/calyx/format.rb
Overview
Helper methods for loading and initializing grammars from static files on disk.
Class Method Summary collapse
-
.load(filename) ⇒ Calyx::Grammar
Reads a file and parses its format, based on the given extension.
-
.load_json(data) ⇒ Calyx::Grammar
Converts the given string of JSON data to a grammar instance.
-
.load_yml(data) ⇒ Calyx::Grammar
Converts the given string of YAML data to a grammar instance.
Class Method Details
.load(filename) ⇒ Calyx::Grammar
Reads a file and parses its format, based on the given extension.
Accepts a JSON or YAML file path, identified by its extension (‘.json` or `.yml`).
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/calyx/format.rb', line 12 def self.load(filename) file = File.read(filename) extension = File.extname(filename) if extension == ".yml" self.load_yml(file) elsif extension == ".json" self.load_json(file) else raise Errors::UnsupportedFormat.new(filename) end end |
.load_json(data) ⇒ Calyx::Grammar
Converts the given string of JSON data to a grammar instance.
37 38 39 40 |
# File 'lib/calyx/format.rb', line 37 def self.load_json(data) require 'json' self.build_grammar(JSON.parse(data)) end |
.load_yml(data) ⇒ Calyx::Grammar
Converts the given string of YAML data to a grammar instance.
28 29 30 31 |
# File 'lib/calyx/format.rb', line 28 def self.load_yml(data) require 'yaml' self.build_grammar(YAML.load(data)) end |