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:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegisterReceiver

Returns a new instance of RegisterReceiver.



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

def initialize
  @callbacks = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodname, *args) ⇒ Object



31
32
33
# File 'lib/pdf/reader/register_receiver.rb', line 31

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

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



21
22
23
# File 'lib/pdf/reader/register_receiver.rb', line 21

def callbacks
  @callbacks
end

Instance Method Details

#all(methodname) ⇒ Object

return the details for every time the specified callback was fired



43
44
45
46
47
48
49
# File 'lib/pdf/reader/register_receiver.rb', line 43

def all(methodname)
  ret = []
  callbacks.each do |cb|
    ret << cb if cb[:name] == methodname
  end
  return ret
end

#all_args(methodname) ⇒ Object



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

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

#count(methodname) ⇒ Object

count the number of times a callback fired



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

def count(methodname)
  counter = 0
  callbacks.each { |cb| counter += 1 if cb[:name] == methodname}
  return counter
end

#final_occurance_of(methodname) ⇒ Object

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



64
65
66
67
68
69
70
# File 'lib/pdf/reader/register_receiver.rb', line 64

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) ⇒ Object

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



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

def first_occurance_of(methodname)
  callbacks.each do |cb|
    return cb if cb[:name] == methodname
  end
  return nil
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/pdf/reader/register_receiver.rb', line 27

def respond_to?(meth)
  true
end

#series(*methods) ⇒ Object

return the first occurance of a particular series of callbacks



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pdf/reader/register_receiver.rb', line 73

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