Module: Lines::Generator
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’) => 3:ms 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
- SINGLE_QUOTE_MATCH =
/'/- STRING_ESCAPE_MATCH =
/[\s"=:{}\[\]]/
Instance Method Summary collapse
-
#generate(obj, opts = {}) ⇒ Object
- max_nesting
-
After a certain depth, arrays are replaced with […] and objects with ….
Instance Method Details
#generate(obj, opts = {}) ⇒ Object
- max_nesting
-
After a certain depth, arrays are replaced with […] and objects with …. Default is 4
- max_bytesize
-
After a certain lenght the root object is interrupted by the … notation. Default is 4096.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/lines/generator.rb', line 51 def generate(obj, opts={}) #=> String max_nesting = opts[:max_nesting] || 4 max_bytesize = opts[:max_bytesize] || 4096 depth = max_nesting - 1 bytesize = 0 line = obj.inject([]) do |acc, (k, v)| if bytesize + (acc.size - 1) > max_bytesize break acc end str = "#{keyenc(k)}=#{valenc(v, depth)}" bytesize += str.bytesize acc.push(str) acc end if bytesize + (line.size - 1) > max_bytesize if bytesize + (line.size - 1) - (line[-1].bytesize - 3) > max_bytesize line.pop end line[-1] = DOT_DOT_DOT end line.join(SPACE) end |