Module: PP::PPMethods

Included in:
PP, SingleLine
Defined in:
opal/stdlib/pp.rb

Instance Method Summary collapse

Instance Method Details

#check_inspect_key(id) ⇒ Object

Check whether the object_id +id+ is in the current buffer of objects to be pretty printed. Used to break cycles in chains of objects to be pretty printed.



133
134
135
136
137
# File 'opal/stdlib/pp.rb', line 133

def check_inspect_key(id)
  Thread.current[:__recursive_key__] &&
  Thread.current[:__recursive_key__][:inspect] &&
  Thread.current[:__recursive_key__][:inspect].include?(id)
end

#comma_breakableObject

A convenience method which is same as follows:

text ',' breakable



208
209
210
211
# File 'opal/stdlib/pp.rb', line 208

def comma_breakable
  text ','
  breakable
end

#guard_inspect_keyObject

Yields to a block and preserves the previous set of objects being printed.



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

def guard_inspect_key
  if Thread.current[:__recursive_key__] == nil
    Thread.current[:__recursive_key__] = {}.compare_by_identity
  end

  if Thread.current[:__recursive_key__][:inspect] == nil
    Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity
  end

  save = Thread.current[:__recursive_key__][:inspect]

  begin
    Thread.current[:__recursive_key__][:inspect] = {}.compare_by_identity
    yield
  ensure
    Thread.current[:__recursive_key__][:inspect] = save
  end
end

#object_address_group(obj, &block) ⇒ Object

A convenience method, like object_group, but also reformats the Object's object_id.



198
199
200
201
202
# File 'opal/stdlib/pp.rb', line 198

def object_address_group(obj, &block)
  str = Kernel.instance_method(:to_s).bind_call(obj)
  str = str.chomp('>')
  group(1, str, '>', &block)
end

#object_group(obj, &block) ⇒ Object

A convenience method which is same as follows:

group(1, '#<' + obj.class.name, '>') { ... }



192
193
194
# File 'opal/stdlib/pp.rb', line 192

def object_group(obj, &block) # :yield:
  group(1, '#<' + obj.class.name, '>', &block)
end

#pop_inspect_key(id) ⇒ Object

Removes an object from the set of objects being pretty printed.



146
147
148
# File 'opal/stdlib/pp.rb', line 146

def pop_inspect_key(id)
  Thread.current[:__recursive_key__][:inspect].delete id
end

#pp(obj = undefined) ⇒ Object

Adds +obj+ to the pretty printing buffer using Object#pretty_print or Object#pretty_print_cycle.

Object#pretty_print_cycle is used when +obj+ is already printed, a.k.a the object reference chain has a cycle.



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'opal/stdlib/pp.rb', line 155

def pp(obj = undefined)
  # Opal: consider JS-native variables:
  %x{
    if (obj === null) {
      #{text "null"}
      #{return}
    }
    else if (obj === undefined) {
      #{text "undefined"}
      #{return}
    }
    else if (obj.$$class === undefined) {
      #{text `Object.prototype.toString.apply(obj)`}
      #{return}
    }
  }

  # If obj is a Delegator then use the object being delegated to for cycle
  # detection
  obj = obj.__getobj__ if defined?(::Delegator) and obj.is_a?(::Delegator)

  if check_inspect_key(obj)
    group {obj.pretty_print_cycle self}
    return
  end

  begin
    push_inspect_key(obj)
    group {obj.pretty_print self}
  ensure
    pop_inspect_key(obj) unless PP.sharing_detection
  end
end

#pp_hash(obj) ⇒ Object

A pretty print for a Hash



267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'opal/stdlib/pp.rb', line 267

def pp_hash(obj)
  group(1, '{', '}') {
    seplist(obj, nil, :each_pair) {|k, v|
      group {
        pp k
        text '=>'
        group(1) {
          breakable ''
          pp v
        }
      }
    }
  }
end

#pp_object(obj) ⇒ Object

A present standard failsafe for pretty printing any given Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'opal/stdlib/pp.rb', line 251

def pp_object(obj)
  object_address_group(obj) {
    seplist(obj.pretty_print_instance_variables, lambda { text ',' }) {|v|
      breakable
      v = v.to_s if Symbol === v
      text v
      text '='
      group(1) {
        breakable ''
        pp(obj.instance_eval(v))
      }
    }
  }
end

#push_inspect_key(id) ⇒ Object

Adds the object_id +id+ to the set of objects being pretty printed, so as to not repeat objects.



141
142
143
# File 'opal/stdlib/pp.rb', line 141

def push_inspect_key(id)
  Thread.current[:__recursive_key__][:inspect][id] = true
end

#seplist(list, sep = nil, iter_method = :each) ⇒ Object

Adds a separated list. The list is separated by comma with breakable space, by default.

seplist iterates the +list+ using +iter_method+.

It yields each object to the block given for #seplist. The procedure +separator_proc+ is called between each yields.

If the iteration is zero times, +separator_proc+ is not called at all.

If +separator_proc+ is nil or not given, +lambda { comma_breakable }+ is used. If +iter_method+ is not given, :each is used.

For example, following 3 code fragments has similar effect.

q.seplist([1,2,3]) {|v| xxx v }

q.seplist([1,2,3], lambda { q.comma_breakable }, :each) {|v| xxx v }

xxx 1 q.comma_breakable xxx 2 q.comma_breakable xxx 3



237
238
239
240
241
242
243
244
245
246
247
248
# File 'opal/stdlib/pp.rb', line 237

def seplist(list, sep=nil, iter_method=:each) # :yield: element
  sep ||= lambda { comma_breakable }
  first = true
  list.__send__(iter_method) {|*v|
    if first
      first = false
    else
      sep.call
    end
    yield(*v)
  }
end