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 https://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

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)

    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) ⇒ Object

Deprecated.

This will be removed in a future release. Use parse instead, and validate the result using #valid? and #errors.

Since:

  • 0.2.0



90
91
92
93
94
# File 'lib/nodaire/indental/indental.rb', line 90

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



123
124
125
# File 'lib/nodaire/indental/indental.rb', line 123

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

#inspectString Also known as: to_s

Returns a human-readable representation of this class.

Returns:

  • (String)

    a human-readable representation of this class.

Since:

  • UNRELEASED



100
101
102
# File 'lib/nodaire/indental/indental.rb', line 100

def inspect
  "\#<#{self.class.name} #{@data}>"
end

#to_h(*args) ⇒ Hash

Convert the document to a hash.

Returns:

  • (Hash)

Since:

  • 0.2.0



132
133
134
# File 'lib/nodaire/indental/indental.rb', line 132

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

#to_json(*args) ⇒ String

Convert the document to JSON.

Returns:

  • (String)

Since:

  • 0.2.0



141
142
143
# File 'lib/nodaire/indental/indental.rb', line 141

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



109
110
111
# File 'lib/nodaire/indental/indental.rb', line 109

def valid?
  @errors.empty?
end