Class: Vivlio::PDF::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/vivlio/pdf/metadata.rb

Overview

Values written into the PDF document information dictionary.

Constant Summary collapse

FIELDS =
{ title: :Title, author: :Author, subject: :Subject,
keywords: :Keywords, creator: :Creator,
creation_date: :CreationDate }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title: nil, author: nil, subject: nil, keywords: nil, creator: nil, creation_date: nil) ⇒ Metadata

Returns a new instance of Metadata.



23
24
25
26
27
28
29
30
31
32
# File 'lib/vivlio/pdf/metadata.rb', line 23

def initialize(title: nil, author: nil, subject: nil, keywords: nil,
               creator: nil, creation_date: nil)
  @title = title
  @author = author
  @subject = subject
  @keywords = keywords
  @creator = creator
  @creation_date = creation_date
  freeze
end

Instance Attribute Details

#authorObject (readonly)

Returns the value of attribute author.



11
12
13
# File 'lib/vivlio/pdf/metadata.rb', line 11

def author
  @author
end

#creation_dateObject (readonly)

Returns the value of attribute creation_date.



11
12
13
# File 'lib/vivlio/pdf/metadata.rb', line 11

def creation_date
  @creation_date
end

#creatorObject (readonly)

Returns the value of attribute creator.



11
12
13
# File 'lib/vivlio/pdf/metadata.rb', line 11

def creator
  @creator
end

#keywordsObject (readonly)

Returns the value of attribute keywords.



11
12
13
# File 'lib/vivlio/pdf/metadata.rb', line 11

def keywords
  @keywords
end

#subjectObject (readonly)

Returns the value of attribute subject.



11
12
13
# File 'lib/vivlio/pdf/metadata.rb', line 11

def subject
  @subject
end

#titleObject (readonly)

Returns the value of attribute title.



11
12
13
# File 'lib/vivlio/pdf/metadata.rb', line 11

def title
  @title
end

Class Method Details

.coerce(value) ⇒ Object

Accepts a Metadata, a Hash of the fields above, or nil.



14
15
16
17
18
19
20
21
# File 'lib/vivlio/pdf/metadata.rb', line 14

def self.coerce(value)
  case value
  when Metadata then value
  when nil then new
  when Hash then new(**value)
  else raise ArgumentError, "cannot coerce #{value.class} into Metadata"
  end
end

Instance Method Details

#empty?Boolean

True when there is nothing to write, i.e. no reason to rewrite the PDF for metadata alone.

Returns:

  • (Boolean)


40
41
42
# File 'lib/vivlio/pdf/metadata.rb', line 40

def empty?
  to_h.empty?
end

#to_hObject



34
35
36
# File 'lib/vivlio/pdf/metadata.rb', line 34

def to_h
  FIELDS.keys.to_h { |name| [name, public_send(name)] }.compact
end

#with_creator(creator) ⇒ Object

Names what produced the PDF, unless the caller named it themselves. Deciding that string is Printer's job: only it knows which viewer actually did the rendering.



47
48
49
50
51
# File 'lib/vivlio/pdf/metadata.rb', line 47

def with_creator(creator)
  return self if @creator

  Metadata.new(**to_h, creator: creator)
end

#write_to(info) ⇒ Object

info is a HexaPDF document information dictionary.



54
55
56
57
58
59
60
# File 'lib/vivlio/pdf/metadata.rb', line 54

def write_to(info)
  FIELDS.each do |name, key|
    value = public_send(name)
    info[key] = value if value
  end
  info
end