Class: PdfParadise::Base

Inherits:
Object
  • Object
show all
Includes:
Colours, Colours::HtmlColoursMethods
Defined in:
lib/pdf_paradise/base/base.rb,
lib/pdf_paradise/base/colours.rb

Overview

PdfParadise::Base

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect

Instance Method Summary collapse

Constructor Details

#initializeBase

#

initialize

#


32
33
34
# File 'lib/pdf_paradise/base/base.rb', line 32

def initialize
  reset
end

Instance Method Details

#basename(i) ⇒ Object

#

basename

#


281
282
283
# File 'lib/pdf_paradise/base/base.rb', line 281

def basename(i)
  File.basename(i)
end

#be_verbose?Boolean

#

be_verbose?

#

Returns:

  • (Boolean)


64
65
66
# File 'lib/pdf_paradise/base/base.rb', line 64

def be_verbose?
  @be_verbose
end

#change_directory(i) ⇒ Object Also known as: cd

#

change_directory (cd tag)

#


151
152
153
# File 'lib/pdf_paradise/base/base.rb', line 151

def change_directory(i)
  Dir.chdir(i) if File.directory?(i)
end

#commandline_arguments?Boolean Also known as: input?

#

commandline_arguments?

#

Returns:

  • (Boolean)


108
109
110
# File 'lib/pdf_paradise/base/base.rb', line 108

def commandline_arguments?
  @commandline_arguments
end

#copy_file(from, to = (Dir.pwd+'/').squeeze('/')) ⇒ Object

#

copy_file

#


140
141
142
143
144
145
146
# File 'lib/pdf_paradise/base/base.rb', line 140

def copy_file(
    from, to = (Dir.pwd+'/').squeeze('/')
  )
  if File.exist? from
    FileUtils.cp(from, to)
  end
end

#delete_file(i) ⇒ Object Also known as: delete_this_file

#

delete_file

This method can be used to delete a local file.

#


290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/pdf_paradise/base/base.rb', line 290

def delete_file(i)
  begin
    File.delete(i) if File.file?(i)
  rescue Errno::EACCES => error
    e 'Could not delete the file at '+i+' - reason '\
      'being insufficient permission.'
    e 'The error was:'
    e
    pp error
    e
  end
end

#e(i = '') ⇒ Object



18
19
20
# File 'lib/pdf_paradise/base/colours.rb', line 18

def e(i = '')
  puts i
end

#ecomment(i) ⇒ Object

#

ecomment

#


56
57
58
# File 'lib/pdf_paradise/base/colours.rb', line 56

def ecomment(i)
  ::Colours.ecomment(i)
end

#esystem(i) ⇒ Object

#

esystem

This method can be used to execute system-commands.

#


190
191
192
193
# File 'lib/pdf_paradise/base/base.rb', line 190

def esystem(i)
  e steelblue(i)
  system(i)
end

#first_argument?Boolean

#

first_argument?

#

Returns:

  • (Boolean)


115
116
117
# File 'lib/pdf_paradise/base/base.rb', line 115

def first_argument?
  @commandline_arguments.first
end

#gold(i) ⇒ Object

#

gold

#


42
43
44
# File 'lib/pdf_paradise/base/colours.rb', line 42

def gold(i)
  ::Colours::HtmlColoursMethods.gold(i)
end

#infer_the_namespaceObject

#

infer_the_namespace

This will assume the true namespace from the inspectable name.

#


229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/pdf_paradise/base/base.rb', line 229

def infer_the_namespace
  _ = inspect
  if _.include?(':0x')
    _ = _.split(':0x').first.to_s.strip
  end
  _ = _.to_s.delete('<')
  if _.include? ' '
    _ = _.split(' ').first.delete('#')
    if _.include? ':'
      _ = _.split(':')[0 .. -2].reject {|entry| entry.empty? }.join('::')
    end
  end
  @namespace = _.delete('#') # And assign it here.
end

#input_without_leading_hyphens?Boolean

#

input_without_leading_hyphens?

This method will return the input that will not begin with –.

#

Returns:

  • (Boolean)


124
125
126
# File 'lib/pdf_paradise/base/base.rb', line 124

def input_without_leading_hyphens?
  @commandline_arguments.reject {|entry| entry.start_with? '--' }
end

#internal_hash?Boolean Also known as: ihash?

#

internal_hash?

#

Returns:

  • (Boolean)


316
317
318
# File 'lib/pdf_paradise/base/base.rb', line 316

def internal_hash?
  @internal_hash
end

#is_an_image_file?(i) ⇒ Boolean

#

is_an_image_file?

#

Returns:

  • (Boolean)


205
206
207
208
209
# File 'lib/pdf_paradise/base/base.rb', line 205

def is_an_image_file?(i)
  i.end_with?(
    '.jpg','.png','.gif'
  )
end

#is_on_roebe?Boolean

#

is_on_roebe?

#

Returns:

  • (Boolean)


82
83
84
# File 'lib/pdf_paradise/base/base.rb', line 82

def is_on_roebe?
  ::PdfParadise.is_on_roebe?
end

#lightsteelblue(i) ⇒ Object

#

lightsteelblue

#


49
50
51
# File 'lib/pdf_paradise/base/colours.rb', line 49

def lightsteelblue(i)
  ::Colours::HtmlColoursMethods.lightsteelblue(i)
end

#log_dir?Boolean Also known as: log_directory?, temp_dir?

#

log_dir?

#

Returns:

  • (Boolean)


323
324
325
# File 'lib/pdf_paradise/base/base.rb', line 323

def log_dir?
  ::PdfParadise.log_dir?
end

#mkdir(i) ⇒ Object

#

mkdir

#


331
332
333
# File 'lib/pdf_paradise/base/base.rb', line 331

def mkdir(i)
  FileUtils.mkdir_p(i)
end

#mv(old, new) ⇒ Object Also known as: move_file

#

mv (mv tag, move tag, rename tag)

#


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/pdf_paradise/base/base.rb', line 254

def mv(
    old,
    new
  )
  # ======================================================================= #
  # We must check that both files exist, before we delete the new file,
  # as we may otherwise end up without any .pdf file. This has happened
  # to me in August 2019 when qpdf failed to do a conversion.
  # ======================================================================= #
  if File.file?(new) and File.file?(old)
    begin
      File.delete(new)
    rescue Exception => error
      pp error
      e 'Error trying to delete the file '+new+'.'
    end
  end
  if File.exist? old
    FileUtils.mv(old, new)
  else
    e 'No file exists at '+sfile(old)+'.'
  end
end

#n_pages?(i) ⇒ Boolean

#

n_pages?

This method will return how many pages are in the given .pdf file at hand.

#

Returns:

  • (Boolean)


178
179
180
181
182
183
# File 'lib/pdf_paradise/base/base.rb', line 178

def n_pages?(i)
  unless PdfParadise.const_defined? :PdfFileNTotalPages
    require 'pdf_paradise/utility_scripts/pdf_file_n_total_pages.rb'
  end
  ::PdfParadise.n_pdf_pages?(i) 
end

#namespace?Boolean

#

namespace?

#

Returns:

  • (Boolean)


198
199
200
# File 'lib/pdf_paradise/base/base.rb', line 198

def namespace?
  @namespace
end

#no_file_at(i) ⇒ Object Also known as: no_file_exists_at

#

no_file_at

This method (or its alias) can be used to notify the user that the given file does not exist at that given location (denoted by the argument called i).

#


75
76
77
# File 'lib/pdf_paradise/base/base.rb', line 75

def no_file_at(i)
  e "No file called `#{sfile(i)}` exists at that target."
end

#opne(i = '') ⇒ Object

#

opne

#


247
248
249
# File 'lib/pdf_paradise/base/base.rb', line 247

def opne(i = '')
  opnn; e i
end

#opnn(i = namespace?, , &block) ⇒ Object

#

opnn

#


214
215
216
217
218
219
220
221
222
# File 'lib/pdf_paradise/base/base.rb', line 214

def opnn(
    i = namespace?,
    &block
  )
  if i.is_a? String
    i = { namespace: i }
  end
  Opn.opn(i, &block)
end

#orange(i) ⇒ Object

#

orange

#


28
29
30
# File 'lib/pdf_paradise/base/colours.rb', line 28

def orange(i)
  ::Colours.orange(i)
end

#resetObject

#

reset (reset tag)

#


39
40
41
42
43
44
# File 'lib/pdf_paradise/base/base.rb', line 39

def reset
  # ======================================================================= #
  # === @be_verbose
  # ======================================================================= #
  @be_verbose = true
end

#reset_the_internal_hashObject

#

reset_the_internal_hash

#


306
307
308
309
310
311
# File 'lib/pdf_paradise/base/base.rb', line 306

def reset_the_internal_hash
  # ======================================================================= #
  # === @internal_hash
  # ======================================================================= #
  @internal_hash = {}
end

#return_commandline_arguments_starting_with_hyphens(i = @commandline_arguments) ⇒ Object Also known as: return_hyphen_arguments

#

return_commandline_arguments_starting_with_hyphens

#


131
132
133
134
135
# File 'lib/pdf_paradise/base/base.rb', line 131

def return_commandline_arguments_starting_with_hyphens(
    i = @commandline_arguments
  )
  [i].flatten.select {|entry| entry.start_with? '--' }
end

#return_files_from_the_commandline_argumentsObject

#

return_files_from_the_commandline_arguments

This method will return all existing files from the commandline arguments.

#


92
93
94
95
96
# File 'lib/pdf_paradise/base/base.rb', line 92

def return_files_from_the_commandline_arguments
  @commandline_arguments.select {|entry|
    File.file?(entry.to_s)
  }
end

#return_pwdObject

#

return_pwd

#


101
102
103
# File 'lib/pdf_paradise/base/base.rb', line 101

def return_pwd
  "#{Dir.pwd}/".squeeze('/')
end

#revObject

#

rev

#


63
64
65
# File 'lib/pdf_paradise/base/colours.rb', line 63

def rev
  ::Colours.rev
end

#set_be_quietObject

#

set_be_quiet

#


57
58
59
# File 'lib/pdf_paradise/base/base.rb', line 57

def set_be_quiet
  @be_verbose = false
end

#set_commandline_arguments(i) ⇒ Object

#

set_commandline_arguments

#


49
50
51
52
# File 'lib/pdf_paradise/base/base.rb', line 49

def set_commandline_arguments(i)
  i = [i].flatten.compact
  @commandline_arguments = i
end

#steelblue(i) ⇒ Object

#

steelblue

#


35
36
37
# File 'lib/pdf_paradise/base/colours.rb', line 35

def steelblue(i)
  ::Colours::HtmlColoursMethods.steelblue(i)
end

#try_to_ensure_that_this_directory_exists(i) ⇒ Object

#

try_to_ensure_that_this_directory_exists

#


338
339
340
341
342
# File 'lib/pdf_paradise/base/base.rb', line 338

def try_to_ensure_that_this_directory_exists(i)
  unless File.directory?(i)
    mkdir(i)
  end
end

#write_what_into(what, into) ⇒ Object Also known as: save_what_into

#

write_what_into

#


158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/pdf_paradise/base/base.rb', line 158

def write_what_into(
    what, into
  )
  begin
    require 'save_file/module' unless Object.const_defined?(:SaveFile)
  rescue LoadError; end
  if Object.const_defined?(:SaveFile)
    SaveFile.write_what_into(what, into)
  else
    e 'Can not save into '+into+' as the save_file gem '\
      'is not available/installed.'
  end
end