Class: Nguyen::Xfdf

Inherits:
Object
  • Object
show all
Defined in:
lib/nguyen/xfdf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fields = {}, options = {}) ⇒ Xfdf

Returns a new instance of Xfdf.



8
9
10
11
12
13
14
# File 'lib/nguyen/xfdf.rb', line 8

def initialize(fields = {}, options = {})
  @fields = fields
  @options = {
    file: nil,
    id: nil
  }.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/nguyen/xfdf.rb', line 6

def options
  @options
end

Instance Method Details

#save_to(path) ⇒ Object

write fdf content to path



39
40
41
# File 'lib/nguyen/xfdf.rb', line 39

def save_to(path)
  (File.open(path, 'w') << to_xfdf).close
end

#to_xfdfObject

generate XFDF content



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nguyen/xfdf.rb', line 17

def to_xfdf
  builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.xfdf('xmlns' => 'http://ns.adobe.com/xfdf/', 'xml:space' => 'preserve') {
      xml.f(href: options[:file]) if options[:file]
      xml.ids(original: options[:id], modified: options[:id]) if options[:id]
      xml.fields {
        @fields.each do |field, value|
          xml.field(name: field) {
            if value.is_a? Array
              value.each { |item| xml.value(item.to_s) }
            else
              xml.value(value.to_s)
            end
          }
        end
      }
    }
  end
  builder.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::AS_XML)
end