Class: PDF::Reader::RegisterReceiver

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/reader/register_receiver.rb

Overview

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:

PDF::Reader.open("somefile.pdf") do |reader|
  receiver = PDF::Reader::RegisterReceiver.new
  reader.page(1).walk(receiver)
  callback = receiver.first_occurance_of(:show_text)
  callback[:args].first.should == "Hellow World"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegisterReceiver

Returns a new instance of RegisterReceiver


25
26
27
# File 'lib/pdf/reader/register_receiver.rb', line 25

def initialize
  @callbacks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodname, *args) ⇒ Object


33
34
35
# File 'lib/pdf/reader/register_receiver.rb', line 33

def method_missing(methodname, *args)
  callbacks << {:name => methodname.to_sym, :args => args}
end

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks


23
24
25
# File 'lib/pdf/reader/register_receiver.rb', line 23

def callbacks
  @callbacks
end

Instance Method Details

#all(methodname) ⇒ Object

return the details for every time the specified callback was fired


43
44
45
# File 'lib/pdf/reader/register_receiver.rb', line 43

def all(methodname)
  callbacks.select { |cb| cb[:name] == methodname }
end

#all_args(methodname) ⇒ Object


47
48
49
# File 'lib/pdf/reader/register_receiver.rb', line 47

def all_args(methodname)
  all(methodname).map { |cb| cb[:args] }
end

#count(methodname) ⇒ Object

count the number of times a callback fired


38
39
40
# File 'lib/pdf/reader/register_receiver.rb', line 38

def count(methodname)
  callbacks.count { |cb| cb[:name] == methodname}
end

#final_occurance_of(methodname) ⇒ Object

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


57
58
59
# File 'lib/pdf/reader/register_receiver.rb', line 57

def final_occurance_of(methodname)
  all(methodname).last
end

#first_occurance_of(methodname) ⇒ Object

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


52
53
54
# File 'lib/pdf/reader/register_receiver.rb', line 52

def first_occurance_of(methodname)
  callbacks.find { |cb| cb[:name] == methodname }
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)

29
30
31
# File 'lib/pdf/reader/register_receiver.rb', line 29

def respond_to?(meth)
  true
end

#series(*methods) ⇒ Object

return the first occurance of a particular series of callbacks


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/pdf/reader/register_receiver.rb', line 62

def series(*methods)
  return nil if methods.empty?

  indexes = (0..(callbacks.size-1))
  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] && callbacks[idx+midx][:name] == methods[midx]
    end
    if count == 0
      return callbacks[idx, methods.size]
    end
  end
  nil
end