Class: PdfExtract::Receiver
- Inherits:
-
Object
- Object
- PdfExtract::Receiver
- Defined in:
- lib/pdf.rb
Instance Method Summary collapse
- #add_spatial_objects(default_type, objs) ⇒ Object
- #after(&block) ⇒ Object
- #before(&block) ⇒ Object
- #call_after ⇒ Object
- #call_before ⇒ Object
- #call_object_listeners(spatial_objects) ⇒ Object
- #expand_listeners_to_callback_methods ⇒ Object
- #for(callback_name, &block) ⇒ Object
- #for_calls? ⇒ Boolean
-
#initialize(pdf) ⇒ Receiver
constructor
A new instance of Receiver.
- #invoke_calls(filename, spatial_options) ⇒ Object
- #object_calls? ⇒ Boolean
- #objects(type_name, &block) ⇒ Object
Constructor Details
#initialize(pdf) ⇒ Receiver
Returns a new instance of Receiver.
54 55 56 57 58 |
# File 'lib/pdf.rb', line 54 def initialize pdf @pdf = pdf @listeners = {} @object_listeners = {} end |
Instance Method Details
#add_spatial_objects(default_type, objs) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/pdf.rb', line 113 def add_spatial_objects default_type, objs if objs.class != Array objs = [objs] unless objs.nil? objs = [] if objs.nil? end objs.each do |obj| type = obj.delete(:group) || default_type @pdf.spatial_objects[type] ||= [] @pdf.spatial_objects[type] << obj end end |
#after(&block) ⇒ Object
73 74 75 |
# File 'lib/pdf.rb', line 73 def after &block @after = {:type => @pdf., :fn => block} end |
#before(&block) ⇒ Object
69 70 71 |
# File 'lib/pdf.rb', line 69 def before &block @before = block end |
#call_after ⇒ Object
97 98 99 |
# File 'lib/pdf.rb', line 97 def call_after self.add_spatial_objects @after[:type], @after[:fn].call unless @after.nil? end |
#call_before ⇒ Object
101 102 103 |
# File 'lib/pdf.rb', line 101 def call_before @before.call unless @before.nil? end |
#call_object_listeners(spatial_objects) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/pdf.rb', line 89 def call_object_listeners spatial_objects @object_listeners.each_pair do |type, listeners| listeners.each do |listener| spatial_objects[type].each { |obj| listener.call obj } end end end |
#expand_listeners_to_callback_methods ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/pdf.rb', line 77 def # TODO merge on callback_name @listeners.each_pair do |callback_name, callback_handler| p = proc do |*args| spatial_objects = callback_handler[:fn].call args self.add_spatial_objects callback_handler[:type], spatial_objects end self.class.send :define_method, callback_name, p end end |
#for(callback_name, &block) ⇒ Object
60 61 62 |
# File 'lib/pdf.rb', line 60 def for callback_name, &block @listeners[callback_name] = {:type => @pdf., :fn => block} end |
#for_calls? ⇒ Boolean
105 106 107 |
# File 'lib/pdf.rb', line 105 def for_calls? @listeners.size > 0 end |
#invoke_calls(filename, spatial_options) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/pdf.rb', line 126 def invoke_calls filename, if [:paged] paged_objs = {} @object_listeners.each_pair do |type, _| @pdf.paged_objects(type).each_pair do |page, objs| paged_objs[page] ||= {} paged_objs[page][type] = objs end end paged_objs.each_pair do |page, objs| call_before if object_calls? @object_listeners.each_pair do |type, listeners| listeners.each do |listener| if objs[type].nil? raise "#{@pdf.} is missing a dependency on #{type}" end objs[type].each { |obj| listener.call obj } end end end call_after end else call_before if object_calls? call_object_listeners @pdf.spatial_objects end call_after end if for_calls? #PDF::Reader.file filename, self, :raw_text => true reader = PDF::Reader.new filename, :raw_text => true reader.pages.each do |page| begin_page page page.walk self end_page page end end end |
#object_calls? ⇒ Boolean
109 110 111 |
# File 'lib/pdf.rb', line 109 def object_calls? @object_listeners.size > 0 end |
#objects(type_name, &block) ⇒ Object
64 65 66 67 |
# File 'lib/pdf.rb', line 64 def objects type_name, &block @object_listeners[type_name] ||= [] @object_listeners[type_name] << block end |