Class: Receipt::Pdf

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Prawn::View
Defined in:
lib/receipt/pdf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Pdf

Returns a new instance of Pdf.



28
29
30
31
32
33
# File 'lib/receipt/pdf.rb', line 28

def initialize(params)
  @params = OpenStruct.new(params)
  setup_i18n

  @errors = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#after_receipt_box(&block) ⇒ Object



56
57
58
# File 'lib/receipt/pdf.rb', line 56

def after_receipt_box(&block)
  @after ||= block
end

#before_receipt_box(&block) ⇒ Object



52
53
54
# File 'lib/receipt/pdf.rb', line 52

def before_receipt_box(&block)
  @before ||= block
end

#currencyObject



84
85
86
# File 'lib/receipt/pdf.rb', line 84

def currency
  params.currency || t('receipt.currency')
end

#dataObject



35
36
37
38
39
40
# File 'lib/receipt/pdf.rb', line 35

def data
  return unless valid?

  generate
  render
end

#fileObject



42
43
44
45
46
47
48
49
50
# File 'lib/receipt/pdf.rb', line 42

def file
  return unless valid?

  @file ||= lambda do
    generate
    save_as(path)
    path
  end.call
end

#filenameObject



76
77
78
# File 'lib/receipt/pdf.rb', line 76

def filename
  File.basename(path)
end

#mimetypeObject



80
81
82
# File 'lib/receipt/pdf.rb', line 80

def mimetype
  'application/pdf'
end

#valid?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/receipt/pdf.rb', line 60

def valid?
  [
    :id,
    :amount,
    :payer,
    :receiver,
    :description
  ].each do |p|
    if params.send(p).to_s.empty?
      @errors[p] = t('receipt.errors.required_param_not_found')
    end
  end

  @errors.size == 0
end