Module: Lines

Extended by:
Lines
Included in:
Lines
Defined in:
lib/lines.rb,
lib/lines/common.rb,
lib/lines/parser.rb,
lib/lines/version.rb,
lib/lines/generator.rb

Overview

Lines is an opinionated structured log format

Defined Under Namespace

Modules: Error, Generator Classes: ParseError, Parser

Constant Summary collapse

LIT_TRUE =

class LogicError < RuntimeError; include Error; end

'#t'
LIT_FALSE =
'#f'
LIT_NIL =
'nil'
SPACE =
' '
EQUAL =
'='
OPEN_BRACE =
'{'
SHUT_BRACE =
'}'
OPEN_BRACKET =
'['
SHUT_BRACKET =
']'
SINGLE_QUOTE =
"'"
DOUBLE_QUOTE =
'"'
DOT_DOT_DOT =
'...'
BACKSLASH =
'\\'
ESCAPED_SINGLE_QUOTE =
"\\'"
ESCAPED_DOUBLE_QUOTE =
'\"'
NUM_MATCH =
/-?(?:0|[1-9])\d*(?:\.\d+)?(?:[eE][+-]\d+)?/
ISO8601_ZULU_CAPTURE =
/^(\d\d\d\d)-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z$/
NUM_CAPTURE =
/^(#{NUM_MATCH})$/
VERSION =
"0.9.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#generate_default_optionsObject (readonly)

The global default options for the Lines.dump method:

max_nesting: 4
max_size: 2048,


19
20
21
# File 'lib/lines.rb', line 19

def generate_default_options
  @generate_default_options
end

#generatorObject

Returns the value of attribute generator.



28
29
30
# File 'lib/lines.rb', line 28

def generator
  @generator
end

#parse_default_optionsObject (readonly)

The global default options for the Lines.parse and Lines.load method:

symbolize_names: false


11
12
13
# File 'lib/lines.rb', line 11

def parse_default_options
  @parse_default_options
end

#parserObject

Returns the value of attribute parser.



25
26
27
# File 'lib/lines.rb', line 25

def parser
  @parser
end

Instance Method Details

#dump(obj, anIO = nil, limit = nil) ⇒ Object

Dumps obj as a Lines string, i.e. calls generate on the object and returns the result.

If anIO (an IO-like object or an object that responds to the write method) was given, the resulting JSON is written to it.

If the number of nested arrays or objects exceeds limit, the object or array is replaced with … or […] respectively. This argument is similar (but not exactly the same!) to the limit argument in Marshal.dump.

The default options for the generator can be changed via the generate_default_options method.

This method is part of the implementation of the load/dump interface of Marshal and YAML.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lines.rb', line 111

def dump(obj, anIO = nil, limit = nil)
  if anIO and limit.nil?
    anIO = anIO.to_io if anIO.respond_to?(:to_io)
    unless anIO.respond_to?(:write)
      limit = anIO
      anIO = nil
    end
  end
  opts = {}
  opts[:max_nesting] = limit if limit
  result = generate(obj, opts)
  if anIO
    anIO.write result
    anIO
  else
    result
  end
end

#generate(obj, options = {}) ⇒ Object

Generate a Lines string from the Ruby data structure obj and return it.

options can have the following keys:

  • max_nesting: The maximum depth of nesting allowed in the data structures from which Lines is to be generated. It defaults to 4.

  • max_bytesize: The maximum number of bytes for a line to be constructed on. It defaults to 2048.



50
51
52
53
# File 'lib/lines.rb', line 50

def generate(obj, options = {})
  opts = Lines.generate_default_options.merge(options.to_hash)
  @generator.generate(obj.to_hash, opts)
end

#load(source, proc = nil, options = {}) ⇒ Object

Load a ruby data structure from a Lines source and return it. A source can either be a string-like object, an IO-like object, or an object responding to the read method. If proc was given, it will be called with any nested Ruby object as an argument recursively in depth first order. To modify the default options pass in the optional options argument as well.

The default options for the parser can be changed via the parse_default_options method.

This method is part of the implementation of the load/dump interface of Marshal and YAML.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/lines.rb', line 67

def load(source, proc = nil, options = {})
  if source.respond_to? :to_str
    source = source.to_str
  elsif source.respond_to? :to_io
    source = source.to_io.read
  elsif source.respond_to?(:read)
    source = source.read
  end
  result = parse(source, options)
  recurse_proc(result, &proc) if proc
  result
end

#parse(source, options = {}) ⇒ Object

Parse the Lines string source into a Ruby data structure and return it.

options can have the following keys:

  • symbolize_names: If set to true, returns symbols for the names (keys) in a Lines object. Otherwise strings are returned. Strings are the default.



37
38
39
40
# File 'lib/lines.rb', line 37

def parse(source, options = {})
  opts = Lines.parse_default_options.merge(options.to_hash)
  @parser.parse(source.to_str, opts)
end