Module: NestedText
- Defined in:
- lib/nestedtext.rb,
lib/nestedtext/error.rb,
lib/nestedtext/decode.rb,
lib/nestedtext/dumper.rb,
lib/nestedtext/encode.rb,
lib/nestedtext/parser.rb,
lib/nestedtext/version.rb,
lib/nestedtext/scanners.rb,
lib/nestedtext/constants.rb,
lib/nestedtext/inline_parser.rb,
lib/nestedtext/encode_helpers.rb,
lib/nestedtext/errors_internal.rb,
lib/nestedtext/core_ext_internal.rb
Overview
# NestedText A ruby library for the human friendly data format NestedText (nestedtext.org/).
Provided is support for decoding a NestedText file or string to Ruby data structures, as well as encoding Ruby objects to a NestedText file or string. Furthermore there is support for serialization and deserialization of custom classes.
See README for documentation on Types, Strict Mode and Custom Classes.
Defined Under Namespace
Modules: ToNTMixin Classes: Error
Constant Summary collapse
- VERSION =
The version of this library.
'4.2.2'
Class Method Summary collapse
-
.dump(obj, io: nil, indentation: 4, strict: false) ⇒ String?
Encode a Ruby object to a NestedText string.
-
.dump_file(obj, filename, **kwargs) ⇒ String?
Apart from ‘filename`, this method behaves exactly like dump.
-
.load(ntstring, top_class: Object, strict: false) ⇒ Object?
Decode a NestedText string to Ruby objects.
-
.load_file(filename, top_class: Object, strict: false) ⇒ Object?
Decode a NestedText stored in a given file.
Class Method Details
.dump(obj, io: nil, indentation: 4, strict: false) ⇒ String?
Encode a Ruby object to a NestedText string.
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/nestedtext/encode.rb', line 18 def self.dump(obj, io: nil, indentation: 4, strict: false) raise Errors::DumpBadIOError, io unless io.nil? || (io.respond_to?(:write) && io.respond_to?(:fsync)) dumper = Dumper.new(indentation, strict) result = dumper.dump obj unless io.nil? io.write(result) io.fsync end dumper.dump obj end |
.dump_file(obj, filename, **kwargs) ⇒ String?
Apart from ‘filename`, this method behaves exactly like dump.
42 43 44 45 46 47 48 |
# File 'lib/nestedtext/encode.rb', line 42 def self.dump_file(obj, filename, **kwargs) raise Errors::DumpFileBadPathError, filename unless filename.is_a? String File.open(filename, 'wt') do |file| dump(obj, io: file, **kwargs) end end |
.load(ntstring, top_class: Object, strict: false) ⇒ Object?
Decode a NestedText string to Ruby objects.
20 21 22 23 24 |
# File 'lib/nestedtext/decode.rb', line 20 def self.load(ntstring, top_class: Object, strict: false) raise Errors::WrongInputTypeError.new([String], ntstring) unless ntstring.nil? || ntstring.is_a?(String) Parser.new(StringIO.new(ntstring), top_class, strict:).parse end |
.load_file(filename, top_class: Object, strict: false) ⇒ Object?
Decode a NestedText stored in a given file.
37 38 39 40 41 42 43 44 |
# File 'lib/nestedtext/decode.rb', line 37 def self.load_file(filename, top_class: Object, strict: false) raise Errors::WrongInputTypeError.new([String], filename) unless !filename.nil? && filename.is_a?(String) # Open explicitly in text mode to detect \r as line ending. File.open(filename, 'rt') do |file| Parser.new(file, top_class, strict:).parse end end |