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.



30
31
32
33
34
35
# File 'lib/receipt/pdf.rb', line 30

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

  @errors = {}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#after_receipt_box(&block) ⇒ Object



58
59
60
# File 'lib/receipt/pdf.rb', line 58

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

#before_receipt_box(&block) ⇒ Object



54
55
56
# File 'lib/receipt/pdf.rb', line 54

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

#dataObject



37
38
39
40
41
42
# File 'lib/receipt/pdf.rb', line 37

def data
  return unless valid?

  generate
  render
end

#fileObject



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

def file
  return unless valid?

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

#valid?Boolean

Returns:

  • (Boolean)


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

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

  @errors.size == 0
end