Class: PdfForms::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf_forms/field.rb

Constant Summary collapse

ATTRS =

Common Fields

[:name, :type, :options, :flags, :justification, :value, :value_default, :name_alt, :max_length]

Instance Method Summary collapse

Constructor Details

#initialize(field_description) ⇒ Field

FieldType: Button FieldName: Sprachoptionen_Inverssuche_Widerspruch FieldFlags: 0 FieldJustification: Left FieldStateOption: Ja FieldStateOption: Off

Representation of a PDF Form Field



13
14
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
# File 'lib/pdf_forms/field.rb', line 13

def initialize(field_description)
  last_value = nil
  field_description.each_line do |line|
    line.chomp!

    if line =~ /^Field([A-Za-z]+):\s+(.*)/
      _, key, value = *$~

      if key == 'StateOption'
        (@options ||= []) << value

      else
        value.chomp!
        last_value = value
        key = key.split(/(?=[A-Z])/).map(&:downcase).join('_')
        instance_variable_set("@#{key}", value)

        # dynamically add in fields that we didn't anticipate in ATTRS
        unless self.respond_to?(key.to_sym)
          proc = Proc.new { instance_variable_get("@#{key}".to_sym) }
          self.class.send(:define_method, key.to_sym, proc)
        end
      end

    else
      # pdftk returns a line that doesn't start with "Field"
      # It happens when a text field has multiple lines
      last_value << "\n" << line
    end
  end
end

Instance Method Details

#to_hashObject



45
46
47
48
49
50
51
52
# File 'lib/pdf_forms/field.rb', line 45

def to_hash
  hash = {}
  ATTRS.each do |attribute|
    hash[attribute] = self.send(attribute)
  end

  hash
end