Class: Nodaire::Indental

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nodaire/indental/indental.rb,
lib/nodaire/indental/lexer.rb,
lib/nodaire/indental/parser.rb

Overview

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.

Examples:

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

Defined Under Namespace

Classes: Lexer, Parser

Constant Summary collapse

INDENT_CHARS_ERROR =
'Indented with non-space characters'
INDENT_LEVEL_ERROR =
'Unexpected indent level'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#categoriesArray<String> (readonly)

Returns the category names.

Returns:

  • (Array<String>)

    the category names.

Since:

  • 0.3.0



52
53
54
# File 'lib/nodaire/indental/indental.rb', line 52

def categories
  @categories
end

#dataHash (readonly)

Deprecated.

This will be removed in a future release. Use #to_h instead.

Returns:

  • (Hash)

Since:

  • 0.2.0



49
50
51
# File 'lib/nodaire/indental/indental.rb', line 49

def data
  @data
end

#errorsArray<String> (readonly)

Returns an array of zero or more error message strings.

Returns:

  • (Array<String>)

    an array of zero or more error message strings.

See Also:

Since:

  • 0.2.0



55
56
57
# File 'lib/nodaire/indental/indental.rb', line 55

def errors
  @errors
end

Class Method Details

.parse(source, symbolize_names: false) ⇒ Indental

Parse the document source.

Examples:

Read an Indental file

source = File.read('example.ndtl')

doc = Nodaire::Indental.parse(source)
puts doc['MY CATEGORY']

Read an Indental file and symbolize names

source = File.read('example.ndtl')

doc = Nodaire::Indental.parse(source, symbolize_names: true)
puts doc[:my_category]

Parameters:

  • source (String)

    The document source to parse.

  • symbolize_names (Boolean) (defaults to: false)

    If true, normalize category and key names and convert them to lowercase symbols. If false, convert category and key names to uppercase strings.

Returns:

Since:

  • 0.2.0



80
81
82
83
84
# 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!(source, symbolize_names: false) ⇒ Indental

Parse the document source, raising an exception if a parser error occurs.

Examples:

Error handling

begin
  doc = Nodaire::Indental.parse(source)
  puts doc['EXAMPLE']
rescue Nodaire::ParserError => error
  puts error
end

Parameters:

  • source (String)

    The document source to parse.

  • symbolize_names (Boolean) (defaults to: false)

    If true, normalize category and key names and convert them to lowercase symbols. If false, convert category and key names to uppercase strings.

Returns:

Raises:

Since:

  • 0.2.0



102
103
104
105
106
# 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

Instance Method Details

#[](category) ⇒ Hash

Returns the data for a given category.

Examples:

doc = Nodaire::Indental.parse(source)
puts doc['CATEGORY']

Returns:

  • (Hash)

    the data for category. If not found, returns nil.

Since:

  • 0.5.0



126
127
128
# File 'lib/nodaire/indental/indental.rb', line 126

def [](category)
  @data[category]
end

#each(&block) ⇒ Object

Enumerable

Since:

  • 0.2.0



150
151
152
# File 'lib/nodaire/indental/indental.rb', line 150

def each(&block)
  @data.each(&block)
end

#to_h(*args) ⇒ Hash

Convert the document to a hash.

Returns:

  • (Hash)

Since:

  • 0.2.0



135
136
137
# File 'lib/nodaire/indental/indental.rb', line 135

def to_h(*args)
  @data.to_h(*args)
end

#to_json(*args) ⇒ String

Convert the document to JSON.

Returns:

  • (String)

Since:

  • 0.2.0



144
145
146
# File 'lib/nodaire/indental/indental.rb', line 144

def to_json(*args)
  @data.to_json(*args)
end

#valid?Boolean

Returns whether the source was parsed without errors.

Returns:

  • (Boolean)

    whether the source was parsed without errors.

See Also:

Since:

  • 0.2.0



112
113
114
# File 'lib/nodaire/indental/indental.rb', line 112

def valid?
  @errors.empty?
end