Module: PDF::Toolkit::ClassMethods

Included in:
PDF::Toolkit
Defined in:
lib/pdf/toolkit/class_methods.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_input_passwordObject



9
10
11
# File 'lib/pdf/toolkit/class_methods.rb', line 9

def default_input_password
  @default_input_password ||= (superclass.default_input_password rescue nil)
end

#default_owner_passwordObject



14
15
16
# File 'lib/pdf/toolkit/class_methods.rb', line 14

def default_owner_password
  @default_owner_password ||= (superclass.default_owner_password rescue nil)
end

#default_permissionsObject



4
5
6
# File 'lib/pdf/toolkit/class_methods.rb', line 4

def default_permissions
  @default_permissions ||= (superclass.default_permissions rescue [])
end

#default_user_passwordObject



20
21
22
# File 'lib/pdf/toolkit/class_methods.rb', line 20

def default_user_password
  @default_user_password ||= (superclass.default_user_password rescue nil)
end

Instance Method Details

#camelize_key(key) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/pdf/toolkit/class_methods.rb', line 69

def camelize_key(key)
  if key.to_s.respond_to?(:camelize)
    key.to_s.camelize
  else
    key.to_s.gsub(/_+([^_])/) {$1.upcase}.sub(/^./) {|l|l.upcase}
  end
end

#info_accessor(accessor_name, info_key = nil) ⇒ Object

Add an accessor for a key. If the key is omitted, defaults to a camelized version of the accessor (foo_bar becomes FooBar). The example below illustrates the defaults.

class MyDocument < PDF::Toolkit
  info_accessor :created_at, "CreationDate"
  info_accessor :updated_at, "ModDate"
  info_accessor :author
  [:subject, :title, :keywords, :producer, :creator].each do |key|
    info_accessor key
  end
end

MyDocument.open("document.pdf").created_at


58
59
60
61
62
63
64
65
66
67
# File 'lib/pdf/toolkit/class_methods.rb', line 58

def info_accessor(accessor_name, info_key = nil)
  info_key ||= camelize_key(accessor_name)
  info_accessors[accessor_name] = info_key
  define_method accessor_name do
    self[info_key]
  end
  define_method "#{accessor_name}=" do |value|
    self[info_key] = value
  end
end

#info_accessorsObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pdf/toolkit/class_methods.rb', line 26

def info_accessors
  @info_accessors ||= begin
    if superclass.respond_to?(:info_accessors)
      superclass.info_accessors.dup
    else
      Hash.new{|h,k|
        if h.has_key?(k.to_s.to_sym)
          h[k.to_s.to_sym]
        elsif k.kind_of?(Symbol)
          camelize_key(k)
        else
          k.dup
        end
      }
    end
  end
end