lib/hexapdf/document.rb in hexapdf-0.6.0 vs lib/hexapdf/document.rb in hexapdf-0.7.0
- old
+ new
@@ -1,12 +1,12 @@
-# -*- encoding: utf-8 -*-
+# -*- encoding: utf-8; frozen_string_literal: true -*-
#
#--
# This file is part of HexaPDF.
#
# HexaPDF - A Versatile PDF Creation and Manipulation Library For Ruby
-# Copyright (C) 2014-2017 Thomas Leitner
+# Copyright (C) 2014-2018 Thomas Leitner
#
# HexaPDF is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3 as
# published by the Free Software Foundation with the addition of the
# following permission added to Section 15 as permitted in Section 7(a):
@@ -127,15 +127,15 @@
def initialize(io: nil, decryption_opts: {}, config: {})
@config = Configuration.with_defaults(config)
@version = '1.2'
@revisions = Revisions.from_io(self, io)
- if encrypted? && @config['document.auto_decrypt']
- @security_handler = Encryption::SecurityHandler.set_up_decryption(self, decryption_opts)
- else
- @security_handler = nil
- end
+ @security_handler = if encrypted? && @config['document.auto_decrypt']
+ Encryption::SecurityHandler.set_up_decryption(self, decryption_opts)
+ else
+ nil
+ end
@listeners = {}
@cache = Hash.new {|h, k| h[k] = {} }
end
@@ -176,11 +176,11 @@
#
# Even though this method might return +true+ for some references, #object may return +nil+
# because this method takes *all* revisions into account. Also see the discussion on #each for
# more information.
def object?(ref)
- @revisions.any? {|rev| rev.object?(ref)}
+ @revisions.any? {|rev| rev.object?(ref) }
end
# :call-seq:
# doc.add(obj, revision: :current, **wrap_opts) -> indirect_object
#
@@ -239,11 +239,11 @@
def delete(ref, revision: :all, mark_as_free: true)
case revision
when :current
@revisions.current.delete(ref, mark_as_free: mark_as_free)
when :all
- @revisions.each {|rev| rev.delete(ref, mark_as_free: mark_as_free)}
+ @revisions.each {|rev| rev.delete(ref, mark_as_free: mark_as_free) }
else
raise ArgumentError, "Unsupported option revision: #{revision}"
end
end
@@ -316,18 +316,18 @@
if type.kind_of?(Class)
klass = type
type = (klass <= HexaPDF::Dictionary ? klass.type : nil)
else
type ||= deref(data.value[:Type]) if data.value.kind_of?(Hash)
- klass = GlobalConfiguration.constantize('object.type_map'.freeze, type) { nil } if type
+ klass = GlobalConfiguration.constantize('object.type_map', type) { nil } if type
end
if data.value.kind_of?(Hash)
subtype ||= deref(data.value[:Subtype]) || deref(data.value[:S])
end
if subtype
- klass = GlobalConfiguration.constantize('object.subtype_map'.freeze, type, subtype) { klass }
+ klass = GlobalConfiguration.constantize('object.subtype_map', type, subtype) { klass }
end
klass ||= if data.stream
HexaPDF::Stream
elsif data.value.kind_of?(Hash)
@@ -352,14 +352,14 @@
end
case object
when Hash
seen[object] = true
- object.each_with_object({}) {|(key, val), memo| memo[key] = unwrap(val, seen.dup)}
+ object.each_with_object({}) {|(key, val), memo| memo[key] = unwrap(val, seen.dup) }
when Array
seen[object] = true
- object.map {|inner_o| unwrap(inner_o, seen.dup)}
+ object.map {|inner_o| unwrap(inner_o, seen.dup) }
when HexaPDF::PDFData
seen[object] = true
unwrap(object.value, seen.dup)
else
object
@@ -413,11 +413,11 @@
callable
end
# Dispatches the message +name+ with the given arguments to all registered listeners.
def dispatch_message(name, *args)
- @listeners[name] && @listeners[name].each {|obj| obj.call(*args)}
+ @listeners[name]&.each {|obj| obj.call(*args) }
end
# Caches the value or the return value of the given block using the given Object::PDFData and
# key arguments as composite hash key. If a cached value already exists, it is just returned.
#
@@ -475,11 +475,11 @@
# Tasks provide an extensible way for performing operations on a PDF document without
# cluttering the Document interface.
#
# See Task for more information.
def task(name, **opts, &block)
- task = config.constantize('task.map'.freeze, name) do
+ task = config.constantize('task.map', name) do
raise HexaPDF::Error, "No task named '#{name}' is available"
end
task.call(self, **opts, &block)
end
@@ -499,18 +499,18 @@
# version has been set manually and the catalog's /Version key refers to a later version, the
# later version is used.
#
# See: PDF1.7 s7.2.2
def version
- catalog_version = (catalog[:Version] || '1.0'.freeze).to_s
+ catalog_version = (catalog[:Version] || '1.0').to_s
(@version < catalog_version ? catalog_version : @version)
end
# Sets the version of the PDF document. The argument must be a string in the format 'M.N'
# where M is the major version and N the minor version (e.g. '1.4' or '2.0').
def version=(value)
- raise ArgumentError, "PDF version must follow format M.N" unless value.to_s =~ /\A\d\.\d\z/
+ raise ArgumentError, "PDF version must follow format M.N" unless value.to_s.match?(/\A\d\.\d\z/)
@version = value.to_s
end
# Returns +true+ if the document is encrypted.
def encrypted?
@@ -613,10 +613,10 @@
end
dispatch_message(:before_write)
if file_or_io.kind_of?(String)
- File.open(file_or_io, 'w+') {|file| Writer.write(self, file)}
+ File.open(file_or_io, 'w+') {|file| Writer.write(self, file) }
else
Writer.write(self, file_or_io)
end
end