Parent

PDF::Reader::RegisterReceiver

An example receiver that just records all callbacks generated by parsing a PDF file.

Useful for testing the contents of a file in an rspec/test-unit suite.

Usage:

receiver = PDF::Reader::RegisterReceiver.new
PDF::Reader.file("somefile.pdf", receiver)
callback = receiver.first_occurance_of(:show_text)
callback[:args].first.should == "Hellow World"

Attributes

callbacks[RW]

Public Class Methods

new() click to toggle source
# File lib/pdf/reader/register_receiver.rb, line 22
def initialize
  @callbacks = []
end

Public Instance Methods

all(methodname) click to toggle source

return the details for every time the specified callback was fired

# File lib/pdf/reader/register_receiver.rb, line 42
def all(methodname)
  ret = []
  callbacks.each do |cb|
    ret << cb if cb[:name] == methodname
  end
  return ret
end
all_args(methodname) click to toggle source
# File lib/pdf/reader/register_receiver.rb, line 50
def all_args(methodname)
  all(methodname).map { |cb| cb[:args] }
end
count(methodname) click to toggle source

count the number of times a callback fired

# File lib/pdf/reader/register_receiver.rb, line 35
def count(methodname)
  counter = 0
  callbacks.each { |cb| counter += 1 if cb[:name] == methodname}
  return counter
end
final_occurance_of(methodname) click to toggle source

return the details for the final time the specified callback was fired

# File lib/pdf/reader/register_receiver.rb, line 63
def final_occurance_of(methodname)
  returnme = nil
  callbacks.each do |cb|
    returnme = cb if cb[:name] == methodname
  end
  return returnme
end
first_occurance_of(methodname) click to toggle source

return the details for the first time the specified callback was fired

# File lib/pdf/reader/register_receiver.rb, line 55
def first_occurance_of(methodname)
  callbacks.each do |cb|
    return cb if cb[:name] == methodname
  end
  return nil
end
method_missing(methodname, *args) click to toggle source
# File lib/pdf/reader/register_receiver.rb, line 30
def method_missing(methodname, *args)
  callbacks << {:name => methodname.to_sym, :args => args}
end
respond_to?(meth) click to toggle source
# File lib/pdf/reader/register_receiver.rb, line 26
def respond_to?(meth)
  true
end
series(*methods) click to toggle source

return the first occurance of a particular series of callbacks

# File lib/pdf/reader/register_receiver.rb, line 72
def series(*methods)
  return nil if methods.empty?

  indexes = (0..(callbacks.size-1-methods.size))
  method_indexes = (0..(methods.size-1))
  match = nil

  indexes.each do |idx|
    count = methods.size
    method_indexes.each do |midx|
      count -= 1 if callbacks[idx+midx][:name] == methods[midx]
    end
    match = idx and break if count == 0
  end

  if match 
    return callbacks[match, methods.size]
  else
    return nil
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.