Class: Hash

Inherits:
Object show all
Defined in:
lib/cbor-pure.rb,
lib/cbor-diagnostic.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cbor_entriesObject

Returns the value of attribute cbor_entries.



39
40
41
# File 'lib/cbor-pure.rb', line 39

def cbor_entries
  @cbor_entries
end

Class Method Details

.cbor_from_entries(entries) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/cbor-pure.rb', line 40

def self.cbor_from_entries(entries)
  h = Hash[entries]
  if h.size != entries.size   # dupkeys found
    h.cbor_entries = entries
  end
  h
end

Instance Method Details

#cbor_diagnostic(options = {}) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/cbor-diagnostic.rb', line 132

def cbor_diagnostic(options = {})
  indent, indent2, indented_options = cbor__indent_helper(options)
  pieces = map {|x| x.map {|y| y.cbor_diagnostic(indented_options)}.join(": ")} # XXX key/value split
  one_line = "{#{"_ " if cbor_stream?}#{pieces.join(", ")}#{cbor_map_lost_warning}}"
  if !(wrap = options[:wrap]) || pieces == [] || one_line.length + indent.length < wrap # XXX
    one_line
  else
    open, close = options[:terminator] ? ["\n#{indent2}", ",\n#{indent}"] : [" ", " "]
    "{#{"_" if cbor_stream?}#{open}#{pieces.join(",\n#{indent2}")}#{close}#{cbor_map_lost_warning}}"
  end
end

#cbor_map_lost_warningObject



55
56
57
58
59
60
# File 'lib/cbor-pure.rb', line 55

def cbor_map_lost_warning
  if cbor_entries
    lost = cbor_entries.size - size
    " / #{lost} DUP KEY#{"S" if lost > 1} LOST / "
  end
end

#cbor_map_push(key, value, entries) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/cbor-pure.rb', line 47

def cbor_map_push(key, value, entries)
  entries << [key, value]
  n = size
  self[key] = value
  if n == size # key was overwritten
    self.cbor_entries = entries
  end
end