Class: Nodaire::Tablatal

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

Overview

Interface for documents in Tablatal format.

Tablatal is a text file format which represents a 'list-type database'. This format was created by Devine Lu Linvega -- see https://wiki.xxiivv.com/#tablatal for more information.

Examples:

require 'nodaire/tablatal'

source = <<~TBTL
  NAME    AGE   COLOR
  Erica   12    Opal
  Alex    23    Cyan
  Nike    34    Red
  Ruca    45    Grey
TBTL

doc = Nodaire::Tablatal.parse(source)

doc.valid?
#=> true

doc.keys
#=> ["NAME", "AGE", "COLOR"]

doc[0]['NAME']
#=> "Erica"

doc.to_a
#=> [{"NAME"=>"Erica", "AGE"=>"12", "COLOR"=>"Opal"}, ...]

doc.to_json
#=> '[{"NAME":"Erica","AGE":"12","COLOR":"Opal"},...]'

doc.to_csv
#=> "NAME,AGE,COLOR\nErica,12,Opal\nAlex,23,Cyan\n..."

Since:

  • 0.1.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataArray<Hash> (readonly)

Deprecated.

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

Returns:

  • (Array<Hash>)

Since:

  • 0.1.0



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

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



58
59
60
# File 'lib/nodaire/tablatal/tablatal.rb', line 58

def errors
  @errors
end

#keysArray (readonly)

Returns the keys from the first line of the source.

Returns:

  • (Array)

    the keys from the first line of the source.

Since:

  • 0.1.0



54
55
56
# File 'lib/nodaire/tablatal/tablatal.rb', line 54

def keys
  @keys
end

Class Method Details

.parse(source, symbolize_names: false) ⇒ Tablatal

Parse the document source.

Examples:

Read a Tablatal file

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

doc = Nodaire::Tablatal.parse(source)
puts doc.first['NAME']

Read a Tablatal file and symbolize names

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

doc = Nodaire::Tablatal.parse(source, symbolize_names: true)
puts doc.first[:name]

Parameters:

  • source (String)

    The document source to parse.

  • symbolize_names (Boolean)

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

Returns:

Since:

  • 0.2.0



83
84
85
86
87
# File 'lib/nodaire/tablatal/tablatal.rb', line 83

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



94
95
96
97
98
# File 'lib/nodaire/tablatal/tablatal.rb', line 94

def self.parse!(source, symbolize_names: false)
  parser = Parser.new(source, true, symbolize_names: symbolize_names)

  new(parser)
end

Instance Method Details

#[](index) ⇒ Hash

Returns the data for a given row index.

Examples:

doc = Nodaire::Tablatal.parse(source)
puts doc[0]

Returns:

  • (Hash)

    the data for the given row index. If not found, returns nil.

Since:

  • 0.5.0



129
130
131
# File 'lib/nodaire/tablatal/tablatal.rb', line 129

def [](index)
  @data[index]
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



104
105
106
# File 'lib/nodaire/tablatal/tablatal.rb', line 104

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

#to_a(*args) ⇒ Array<Hash>

Convert the document to an array of hashes.

Returns:

  • (Array<Hash>)

Since:

  • 0.1.0



138
139
140
# File 'lib/nodaire/tablatal/tablatal.rb', line 138

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

#to_csvString

Convert the document to CSV.

Returns:

  • (String)

Since:

  • 0.1.0



157
158
159
160
161
162
163
164
# File 'lib/nodaire/tablatal/tablatal.rb', line 157

def to_csv
  CSV.generate do |csv|
    csv << keys
    data.each do |row|
      csv << keys.map { |key| row[key] }
    end
  end
end

#to_json(*args) ⇒ String

Convert the document to JSON.

Returns:

  • (String)

Since:

  • 0.5.0



148
149
150
# File 'lib/nodaire/tablatal/tablatal.rb', line 148

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



114
115
116
# File 'lib/nodaire/tablatal/tablatal.rb', line 114

def valid?
  @errors.empty?
end