Class: Lines::Dumper

Inherits:
Object
  • Object
show all
Defined in:
lib/lines.rb

Overview

Some opinions here as well on the format:

We really want to never fail at dumping because you know, they’re logs. It’s better to get a slightly less readable log that no logs at all.

We’re trying to be helpful for humans. It means that if possible we want to make things shorter and more readable. It also means that ideally we would like the parsing to be isomorphic but approximations are alright. For example a symbol might become a string.

Basically, values are either composite (dictionaries and arrays), quoted strings or litterals. Litterals are strings that can be parsed to something else depending if the language supports it or not. Litterals never contain white-spaces or other weird (very precise !) characters.

the true litteral is written as “#t” the false litteral is written as “#f” the nil / null litteral is written as “nil”

dictionary keys are always strings or litterals.

Pleaaase, keep units with numbers. And we provide a way for this: a tuple of (number, litteral) can be concatenated. Eg: (3, ‘ms’) => 3ms alternatively if your language supports a time range it could be serialized to the same value (and parsed back as well).

if we don’t know how to serialize something we provide a language-specific string of it and encode is at such.

The output ought to use the UTF-8 encoding.

This dumper has been inspired by the OkJSON gem (both formats look alike after all).

Constant Summary collapse

SPACE =
' '
LIT_TRUE =
'#t'
LIT_FALSE =
'#f'
LIT_NIL =
'nil'
OPEN_BRACE =
'{'
SHUT_BRACE =
'}'
OPEN_BRACKET =
'['
SHUT_BRACKET =
']'
SINGLE_QUOTE =
"'"
DOUBLE_QUOTE =
'"'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#max_depthObject

After a certain depth, arrays are replaced with […] and objects with …. Default is 4.



285
286
287
# File 'lib/lines.rb', line 285

def max_depth
  @max_depth
end

Instance Method Details

#dump(obj) ⇒ Object

> String



263
264
265
# File 'lib/lines.rb', line 263

def dump(obj) #=> String
  objenc_internal(obj)
end

#map(klass, &rule) ⇒ Object

Used to introduce new ruby litterals.

Usage:

Point = Struct.new(:x, :y)
Lines.dumper.map(Point) do |p|
  "#{p.x}x#{p.y}"
end

Lines.log msg: Point.new(3, 5)
# logs: msg=3x5


279
280
281
# File 'lib/lines.rb', line 279

def map(klass, &rule)
  @mapping[klass] = rule
end