Class: Nodaire::Tablatal
- Inherits:
-
Object
- Object
- Nodaire::Tablatal
- 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.
Instance Attribute Summary collapse
-
#data ⇒ Array<Hash>
readonly
deprecated
Deprecated.
This will be removed in a future release. Use #to_a instead.
-
#errors ⇒ Array<String>
readonly
An array of zero or more error message strings.
-
#keys ⇒ Array
readonly
The keys from the first line of the source.
Class Method Summary collapse
-
.parse(source, symbolize_names: false) ⇒ Tablatal
Parse the document
source. -
.parse!(source, symbolize_names: false) ⇒ Object
deprecated
Deprecated.
This will be removed in a future release. Use Tablatal.parse instead, and validate the result using #valid? and #errors.
Instance Method Summary collapse
-
#[](index) ⇒ Hash
Returns the data for a given row
index. -
#inspect ⇒ String
(also: #to_s)
A human-readable representation of this class.
-
#to_a(*args) ⇒ Array<Hash>
Convert the document to an array of hashes.
-
#to_csv ⇒ String
Convert the document to CSV.
-
#to_json(*args) ⇒ String
Convert the document to JSON.
-
#valid? ⇒ Boolean
Whether the source was parsed without errors.
Instance Attribute Details
#data ⇒ Array<Hash> (readonly)
This will be removed in a future release. Use #to_a instead.
52 53 54 |
# File 'lib/nodaire/tablatal/tablatal.rb', line 52 def data @data end |
#errors ⇒ Array<String> (readonly)
Returns an array of zero or more error message strings.
58 59 60 |
# File 'lib/nodaire/tablatal/tablatal.rb', line 58 def errors @errors end |
#keys ⇒ Array (readonly)
Returns the keys from the first line of the source.
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.
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
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.
129 130 131 |
# File 'lib/nodaire/tablatal/tablatal.rb', line 129 def [](index) @data[index] end |
#inspect ⇒ String Also known as: to_s
Returns a human-readable representation of this class.
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.
138 139 140 |
# File 'lib/nodaire/tablatal/tablatal.rb', line 138 def to_a(*args) @data.to_a(*args) end |
#to_csv ⇒ String
Convert the document to CSV.
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.
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.
114 115 116 |
# File 'lib/nodaire/tablatal/tablatal.rb', line 114 def valid? @errors.empty? end |