Class: ZAML

Inherits:
Object show all
Defined in:
lib/icss/serialization/zaml.rb

Defined Under Namespace

Classes: Comment, Label, Padding

Constant Summary collapse

VERSION =
"0.1.4m"
NUM_RE =
'[-+]?(0x)?\d+\.?\d*'
SIMPLE_STRING_RE =

unless defined?(::ZAML::SIMPLE_STRING_RE)

/\A(true|false|yes|no|on|null|off|#{ZAML::NUM_RE}(:#{ZAML::NUM_RE})*|!|=|~|>|\||\n+)\z/io
ZAML_ESCAPES =
%w{\x00 \x01 \x02 \x03 \x04 \x05 \x06 \a \x08 \t \n \v \f \r \x0e \x0f \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 \x18 \x19 \x1a \e \x1c \x1d \x1e \x1f }
HI_BIT_CHARS =
'\x80-\xFF'
HI_BIT_CHARS_RE =
/([#{ZAML::HI_BIT_CHARS}])/o
EXTENDED_CHARS_RE =
/[\x00-\x08\x0B\x0C\x0E-\x1F]/o

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ZAML

Instance Methods



35
36
37
38
# File 'lib/icss/serialization/zaml.rb', line 35

def initialize(options={})
  reset!
  self.valign = options[:valign]
end

Instance Attribute Details

#indentObject

Returns the value of attribute indent.



19
20
21
# File 'lib/icss/serialization/zaml.rb', line 19

def indent
  @indent
end

#resultObject

Returns the value of attribute result.



19
20
21
# File 'lib/icss/serialization/zaml.rb', line 19

def result
  @result
end

#valignObject

line up simple value tokens at this vertical column



21
22
23
# File 'lib/icss/serialization/zaml.rb', line 21

def valign
  @valign
end

Class Method Details

.dump(stuff, where = '', options = {}) ⇒ Object

Class Methods



26
27
28
29
30
# File 'lib/icss/serialization/zaml.rb', line 26

def self.dump(stuff, where='', options={})
  z = self.new(options)
  stuff.to_zaml(z)
  where << z.to_s
end

.padding(nlines) ⇒ Object



112
113
114
# File 'lib/icss/serialization/zaml.rb', line 112

def self.padding(nlines)
  Padding.new(nlines)
end

Instance Method Details

#emit(s) ⇒ Object



69
70
71
72
73
# File 'lib/icss/serialization/zaml.rb', line 69

def emit(s)
  @result << s
  @recent_nl = false unless s.kind_of?(Label)
  self.to_s
end

#first_time_only(obj) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/icss/serialization/zaml.rb', line 89

def first_time_only(obj)
  if label = Label.for(obj)
    emit(label.reference)
  else
    if @structured_key_prefix and not obj.is_a? String
      emit(@structured_key_prefix)
      @structured_key_prefix = nil
    end
    emit(new_label_for(obj))
    yield
  end
  self.to_s
end

#inspectObject



106
107
108
109
110
# File 'lib/icss/serialization/zaml.rb', line 106

def inspect
  res = to_s.inspect
  res = res[0..29]+"..."+res[-20..-1] if res.length > 50
  %Q{\#<ZAML ind=#{@indent.inspect} pfx=#{@structured_key_prefix} result='#{res}'>}
end

#nested(tail = ' ') ⇒ Object

for all code within the block, the cursor supplies



49
50
51
52
53
54
55
# File 'lib/icss/serialization/zaml.rb', line 49

def nested(tail='  ')
  old_indent = @indent
  # @indent    = "#{@indent || "\n"}#{tail}"
  @indent    = @indent ? "#{@indent}#{tail}" : "\n"
  yield
  @indent    = old_indent
end

#new_label_for(obj) ⇒ Object



86
87
88
# File 'lib/icss/serialization/zaml.rb', line 86

def new_label_for(obj)
  Label.new(obj,(Hash === obj || Array === obj) ? "#{@indent || "\n"}  " : ' ')
end

#nl(s = '') ⇒ Object



74
75
76
77
78
# File 'lib/icss/serialization/zaml.rb', line 74

def nl(s='')
  emit(@indent || "\n") unless @recent_nl
  emit(s)
  @recent_nl = true
end

#no_comment(elt) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/icss/serialization/zaml.rb', line 116

def no_comment(elt)
  if elt.is_a?(ZAML::Comment) || elt.is_a?(ZAML::Padding)
    elt.to_zaml(self)
  else
    yield
  end
end

#prefix_structured_keys(x) ⇒ Object



79
80
81
82
83
84
# File 'lib/icss/serialization/zaml.rb', line 79

def prefix_structured_keys(x)
  @structured_key_prefix = x
  yield
  nl unless @structured_key_prefix
  @structured_key_prefix = nil
end

#reset!Object



40
41
42
43
44
45
46
# File 'lib/icss/serialization/zaml.rb', line 40

def reset!
  @result = []
  @indent = nil
  @structured_key_prefix = nil
  Label.counter_reset
  emit('--- ')
end

#to_sObject



103
104
105
# File 'lib/icss/serialization/zaml.rb', line 103

def to_s
  @result.join.tap{|s| s << "\n" if s[-1..-1] != "\n" }
end

#vpad(sep, key) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/icss/serialization/zaml.rb', line 57

def vpad(sep, key)
  return emit("#{sep} ") unless valign
  if    key.is_a?(Symbol)  then str = key.inspect
  elsif key.is_a?(String)  then str = key
  elsif key.is_a?(Numeric) then str = key.to_s
  else  return emit("#{sep} ") ; end
  keylen = ((@indent||"\n").length - 1) + str.length + sep.length
  vlen   = valign - keylen
  pad  = (vlen > 1) ? (" "*vlen) : " "
  emit(sep+pad)
end