Module: DocumentsWithFooterToPdf

Defined in:
lib/documents_with_footer_to_pdf.rb,
lib/documents_with_footer_to_pdf/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.6"

Class Method Summary collapse

Class Method Details

.create_selectors(str, item, alpha) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'lib/documents_with_footer_to_pdf.rb', line 385

def self.create_selectors( str, item, alpha )
  if str.index( '--' ) == 1
    item[:valid] = true
    a = str.split( '--' )
    item[:enumerator][:original] = a[ 0 ]
    case a.length
      when 2
        item[:heading] = a[ 1 ]
      when 3
        item[:heading] = a[ 1 ]
        item[:subheading] = a[ 2 ]
    end
  
    if /\A[-+]?[0-9]+\z/.match( item[:enumerator][:original] ).to_a.length != 0
      item[:enumerator][:style] = :integer
      item[:enumerator][:integer] = item[:enumerator][:original].to_i
      item[:enumerator][:char] = alpha[ item[:enumerator][:integer]-1 ]
      item[:enumerator][:roman] = self.roman_numerals( item[:enumerator][:integer] )
    else
      item[:enumerator][:style] = :char
      item[:enumerator][:char] = item[:enumerator][:original].upcase
      item[:enumerator][:integer] =alpha.index( item[:enumerator][:char] ) + 1
      item[:enumerator][:roman] = self.roman_numerals( alpha.index( item[:enumerator][:char] ) + 1 )
    end
  else
    item[:valid] = false
    item[:heading] = str
  end
  
  return item
end

.generate(folder, silent, options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/documents_with_footer_to_pdf.rb', line 145

def self.generate( folder, silent, options={} )
  
  hash = Marshal.load( Marshal.dump( self.get_options() ) )
  if self.validate_generate( folder, silent, options, hash )
          
    hash[:path][:root] = folder
    hash[:params][:console][:silent] = silent == :silent ? true : false
    hash[:params][:console][:mode] = !hash[:params][:console][:silent] ? silent : ''
     
    hash = self.options_update( options, hash, 'set_options' ) 
    hash[:path] = LocalPathBuilder.generate( hash[:path], :silent, Time.now.to_i.to_s )

    self.footer_image( hash )
    prepares = self.footer_prepare( hash )
    self.footer_generate( prepares, hash )
    self.footer_merge( hash )
    FileUtils.rm_rf( hash[:path][:children][:tmp][:full] )
  end
end

.get_optionsObject

Your code goes here…



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/documents_with_footer_to_pdf.rb', line 15

def self.get_options()
  return {
    path: {
      root: nil,
      name: '',
      children: {
        tmp: {
          name: 'tmp-{{SALT}}',
          children: {
            jpg: {
              name: '0-jpg'
            },
              pdf_single: {
                name: '1-pdf-single--w-footer'
            }
          }
        },
        pdf_combined: {
          name: '0-result-{{SALT}}',
          files: {
            result: {
              name: 'result.pdf'
            }
          }
        }
      }
    },
    footer: {
      position: {
        top: [ 0, 20 ],
        bottom: [ 0, 10 ]
      },
      table: {
        left: {
          top: {
            text: '<<--FILENAME-->>'
          },
          bottom: {
            text: ''
          }
        },
        center: {
          top: {
            text: ''
          },
          bottom: {
            text: ''
          }
        },
        right: {
          top: {
            text: '<<--TIMESTAMP-->>'
          },
          bottom: {
            text: '<<--PAGE_CURRENT-->> from <<--PAGE_TOTAL-->>'
          }
        }
      }
    },
    selectors: {
      timestamp: {
        gsub: '<<--TIMESTAMP-->>',
        key: :timestamp,
        strf: '%d.%m.%Y',
      },
      page_current: {
        gsub: '<<--PAGE_CURRENT-->>',
        key: :page__current
      },
      page_total: {
        gsub: '<<--PAGE_TOTAL-->>',
        key: :page__total
      },
      enumerator_original: {
        gsub: '<<--ENUMERATOR_ORIGINAL-->>',
        key: :enumerator__original
      },
      enumerator_integer: {
        gsub: '<<--ENUMERATOR_INTEGER-->>',
        key: :enumerator__integer
      },
      enumerator_char: {
        gsub: '<<--ENUMERATOR_CHAR-->>',
        key: :enumerator__char
      },
      enumerator_roman: {
        gsub: '<<--ENUMERATOR_ROMAN-->>',
        key: :enumerator__roman
      },
      filename: {
        gsub: '<<--FILENAME-->>',
        key: :filename
      },
      path: {
        gsub: '<<--PATH-->>',
        key: :path
      },
      heading: {
        gsub: '<<--HEADLINE-->>',
        key: :heading
      },
      subheading: {
        gsub: '<<--SUBHEADING-->>',
        key: :subheading
      }
    },
    params: {
      footer: {
        font_size: 9
      },
      document: {
        width: 500
      },
      image: {
        density: 300
      },
      search: {
        subfolders: false,
        suffixs: [ 'jpg', 'png', 'pdf' ]
      },
      console: {
        silent: nil,
        mode: nil,
        length: 50
      }
    }
  }
end

.next_lower_key(integer, values) ⇒ Object



350
351
352
353
354
# File 'lib/documents_with_footer_to_pdf.rb', line 350

def self.next_lower_key( integer, values )
  arabics = values.keys
  next_lower_index = ( arabics.push( integer ).sort.index( integer ) ) - 1
  arabics[next_lower_index]
end

.roman_numerals(integer) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/documents_with_footer_to_pdf.rb', line 349

def self.roman_numerals( integer )
  def self.next_lower_key( integer, values )
    arabics = values.keys
    next_lower_index = ( arabics.push( integer ).sort.index( integer ) ) - 1
    arabics[next_lower_index]
  end
  
  values = {
    1 => 'I',
    4 => 'IV',
    5 => 'V',
    9 => 'IX',
    10 => 'X',
    40 => 'XL',
    50 => 'L',
    90 => 'XC',
    100 => 'C',
    400 => 'CD',
    500 => 'D',
    900 => 'CM',
    1000 => 'M'
  }
  
  roman = ''
  while integer > 0
    if values[ integer ]
      roman += values[ integer ]
      return roman
    end

    roman += values[ self.next_lower_key( integer, values ) ]
    integer -= self.next_lower_key( integer, values )
  end
end

.str_difference(a, b) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/documents_with_footer_to_pdf.rb', line 175

def self.str_difference( a, b )
  a = a.to_s.downcase.split( '_' ).join( '' )
  b = b.to_s.downcase.split( '_' ).join( '' )
  longer = [ a.size, b.size ].max
  same = a
    .each_char
    .zip( b.each_char )
    .select { | a, b | a == b }
    .size
  ( longer - same ) / a.size.to_f
end