class Nodaire::Indental
Interface for documents in Indental
format.
Indental
is a text file format which represents a 'dictionary-type database'. This format was created by Devine Lu Linvega – see wiki.xxiivv.com/#indental for more information.
@example
require 'nodaire/indental' source = <<~NDTL NAME KEY : VALUE LIST ITEM1 ITEM2 NDTL doc = Nodaire::Indental.parse(source) doc.valid? #=> true doc.categories #=> ["NAME"] doc['NAME']['KEY'] #=> "VALUE" doc.to_h #=> {"NAME" => {"KEY"=>"VALUE", "LIST"=>["ITEM1", "ITEM2"]}} doc.to_json #=> '{"NAME":{"KEY":"VALUE","LIST":["ITEM1","ITEM2"]}}'
@since 0.2.0
Constants
- INDENT_CHARS_ERROR
@private
- INDENT_LEVEL_ERROR
@private
Attributes
@return [Array<String>] the category names. @since 0.3.0
@deprecated This will be removed in a future release. Use {#to_h} instead. @return [Hash]
@return [Array<String>] an array of zero or more error message strings. @see valid?
Public Class Methods
# File lib/nodaire/indental/indental.rb, line 156 def initialize(parser) @data = parser.data @errors = parser.errors @categories = data.keys end
Parse the document source
.
@example Read an Indental
file
source = File.read('example.ndtl') doc = Nodaire::Indental.parse(source) puts doc['MY CATEGORY']
@example Read an Indental
file and symbolize names
source = File.read('example.ndtl') doc = Nodaire::Indental.parse(source, symbolize_names: true) puts doc[:my_category]
@param [String] source The document source to parse. @param [Boolean] symbolize_names
If +true+, normalize category and key names and convert them to lowercase symbols. If +false+, convert category and key names to uppercase strings.
@return [Indental]
# File lib/nodaire/indental/indental.rb, line 80 def self.parse(source, symbolize_names: false) parser = Parser.new(source, false, symbolize_names: symbolize_names) new(parser) end
Parse the document source
, raising an exception if a parser error occurs.
@example Error handling
begin doc = Nodaire::Indental.parse(source) puts doc['EXAMPLE'] rescue Nodaire::ParserError => error puts error end
@param (see .parse)
@return [Indental] @raise [ParserError]
# File lib/nodaire/indental/indental.rb, line 102 def self.parse!(source, symbolize_names: false) parser = Parser.new(source, true, symbolize_names: symbolize_names) new(parser) end
Public Instance Methods
Returns the data for a given category
.
@example
doc = Nodaire::Indental.parse(source) puts doc['CATEGORY']
@return [Hash] the data for category
. If not found, returns nil
. @since 0.5.0
# File lib/nodaire/indental/indental.rb, line 126 def [](category) @data[category] end
Enumerable @private
# File lib/nodaire/indental/indental.rb, line 150 def each(&block) @data.each(&block) end
Convert the document to a hash.
@return [Hash]
# File lib/nodaire/indental/indental.rb, line 135 def to_h(*args) @data.to_h(*args) end
Convert the document to JSON.
@return [String]
# File lib/nodaire/indental/indental.rb, line 144 def to_json(*args) @data.to_json(*args) end
@return [Boolean] whether the source was parsed without errors. @see errors
# File lib/nodaire/indental/indental.rb, line 112 def valid? @errors.empty? end