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

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

def initialize(field_description)
  field_description.each_line do |line|
    case line
    when /FieldStateOption:\s*(.*?)\s*$/
      (@options ||= []) << $1
    else
      line.strip!
      key, value = line.split(": ")
      key.gsub!(/Field/, "")
      key = key.split(/(?=[A-Z])/).map(&:downcase).join('_').split(":")[0]
      
      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
  end
end

Instance Method Details

#to_hashObject



35
36
37
38
39
40
41
42
# File 'lib/pdf_forms/field.rb', line 35

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