Class: PdfEditor::Resource
- Inherits:
-
Object
- Object
- PdfEditor::Resource
show all
- Includes:
- Errors
- Defined in:
- lib/pdf_editor/resource.rb
Constant Summary
Constants included
from Errors
Errors::ArgumentMissingError, Errors::InvalidInputError, Errors::InvalidPDFError, Errors::PageCountError, Errors::PageOrderInvalidError, Errors::PageRangeError, Errors::ResourcesEmptyError, Errors::TitlePageTitleError
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Object that contains a tempfile and other pertinent info about that file, such as it’s name and page count.
-
#name ⇒ Object
readonly
Object that contains a tempfile and other pertinent info about that file, such as it’s name and page count.
Instance Method Summary
collapse
Constructor Details
#initialize(file, name = nil) ⇒ Resource
Returns a new instance of Resource.
17
18
19
20
|
# File 'lib/pdf_editor/resource.rb', line 17
def initialize(file, name=nil)
@file = file
@name = name
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &b) ⇒ Object
54
55
56
|
# File 'lib/pdf_editor/resource.rb', line 54
def method_missing(method, *args, &b)
file.send(method, *args, &b)
end
|
Instance Attribute Details
#file ⇒ Object
Object that contains a tempfile and other pertinent info about that file, such as it’s name and page count. All methods are delegated to file that aren’t found in Resource.
15
16
17
|
# File 'lib/pdf_editor/resource.rb', line 15
def file
@file
end
|
#name ⇒ Object
Object that contains a tempfile and other pertinent info about that file, such as it’s name and page count. All methods are delegated to file that aren’t found in Resource.
15
16
17
|
# File 'lib/pdf_editor/resource.rb', line 15
def name
@name
end
|
Instance Method Details
#open_file ⇒ Object
29
30
31
32
33
34
35
36
|
# File 'lib/pdf_editor/resource.rb', line 29
def open_file
begin
file.open if file.closed?
yield file
ensure
file.close
end
end
|
#page_count ⇒ Object
42
43
44
45
46
47
48
|
# File 'lib/pdf_editor/resource.rb', line 42
def page_count
open_file do |f|
::PDF::Reader.new(f).page_count
end
rescue PDF::Reader::MalformedPDFError => e
raise Errors::InvalidPDFError, e.message
end
|
#read(length = nil) ⇒ Object
22
23
24
25
26
27
|
# File 'lib/pdf_editor/resource.rb', line 22
def read(length=nil)
open_file do |f|
f.rewind
f.read(length)
end
end
|
#respond_to_missing?(method_sym, include_private = false) ⇒ Boolean
50
51
52
|
# File 'lib/pdf_editor/resource.rb', line 50
def respond_to_missing?(method_sym, include_private=false)
file.respond_to?(method_sym, include_private) || super
end
|
#write(contents) ⇒ Object
38
39
40
|
# File 'lib/pdf_editor/resource.rb', line 38
def write(contents)
file.write(contents)
end
|