Class: PdfEditor::Resource

Inherits:
Object
  • Object
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

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

#fileObject (readonly)

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

#nameObject (readonly)

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_fileObject



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_countObject



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

Returns:

  • (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