Class: TraceableString

Inherits:
String
  • Object
show all
Includes:
Traceable
Defined in:
lib/traceable.rb

Overview

A string that remembers the source of its value, and can output it again when serialised to YAML.

Instance Attribute Summary

Attributes included from Traceable

#byte, #column, #file, #line

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.debugObject



104
105
106
# File 'lib/traceable.rb', line 104

def self.debug
  @@debug ||= false
end

.debug=(maybe) ⇒ Object



100
101
102
# File 'lib/traceable.rb', line 100

def self.debug=(maybe)
  @@debug = maybe
end

Instance Method Details

#encode_with(coder) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/traceable.rb', line 67

def encode_with(coder)
  coder.tag = nil
  coder.scalar = self.to_str
  if debug?
    # LibYAML, the C YAML library which underlies Ruby's Psych
    # library, does not handle comments at all. The parser ignores
    # them and the emitter cannot write them. Thus, to output the
    # sourcing information in a manner that is at least semi-sane,
    # we need to put it in the actual value, behind some sort of
    # separator.
    #
    # The separator needs to be representable in Latin-1, otherwise
    # LibYAML quotes it. It needs to be non-breaking, otherwise
    # LibYAML will break here to wrap at 80 characters. It cannot
    # have any special meaning in YAML (including the quote
    # character), otherwise LibYAML quotes it. It shouldn't appear
    # in real data, sine we'll need to substitute all instances of
    # it.
    #
    # Also, I like cats, even ASCII ones.
    coder.scalar += " =^.^= #{@file} (line #{@line}, column #{@column})"
  end
end

#inspectObject



91
92
93
94
95
96
97
98
# File 'lib/traceable.rb', line 91

def inspect
  if debug?
    '"%s" (%s, line: %i, col: %i, byte: %i)' %
      [self.to_str, @file, @line, @column, @byte]
  else
    super
  end
end