File: register_receiver.rb

package info (click to toggle)
ruby-pdf-reader 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,512 kB
  • sloc: ruby: 11,959; sh: 46; makefile: 11
file content (95 lines) | stat: -rw-r--r-- 2,544 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# coding: utf-8
# typed: strict
# frozen_string_literal: true

# Copyright (C) 2010 James Healy (jimmy@deefa.com)

class PDF::Reader

  # 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
  #
  class RegisterReceiver

    #: Array[Hash[Symbol, untyped]]
    attr_accessor :callbacks

    #: () -> void
    def initialize
      @callbacks = [] #: Array[Hash[Symbol, untyped]]
    end

    #: (untyped) -> bool
    def respond_to?(meth)
      true
    end

    #: (Symbol, *untyped) -> void
    def method_missing(methodname, *args)
      callbacks << {:name => methodname.to_sym, :args => args}
    end

    # count the number of times a callback fired
    #: (Symbol) -> Integer
    def count(methodname)
      callbacks.count { |cb| cb[:name] == methodname}
    end

    # return the details for every time the specified callback was fired
    #: (Symbol) -> Array[Hash[Symbol, untyped]]
    def all(methodname)
      callbacks.select { |cb| cb[:name] == methodname }
    end

    #: (Symbol) -> Array[Array[untyped]]
    def all_args(methodname)
      all(methodname).map { |cb| cb[:args] }
    end

    # return the details for the first time the specified callback was fired
    #: (Symbol) -> Hash[Symbol, untyped]?
    def first_occurance_of(methodname)
      callbacks.find { |cb| cb[:name] == methodname }
    end

    # return the details for the final time the specified callback was fired
    #: (Symbol) -> Hash[Symbol, untyped]?
    def final_occurance_of(methodname)
      all(methodname).last
    end

    # return the first occurance of a particular series of callbacks
    #: (*Symbol) -> Array[Hash[Symbol, untyped]]?
    def series(*methods)
      return nil if methods.empty?

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

      indexes.each do |idx|
        count = methods.size
        method_indexes.each do |midx|
          res = callbacks[idx+midx]
          if res && res[:name] == methods[midx]
            count -= 1
          end
        end
        if count == 0
          return callbacks[idx, methods.size]
        end
      end
      nil
    end
  end
end