Class: Nguyen::Xfdf
- Inherits:
-
Object
- Object
- Nguyen::Xfdf
- Defined in:
- lib/nguyen/xfdf.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(fields = {}, options = {}) ⇒ Xfdf
constructor
A new instance of Xfdf.
-
#save_to(path) ⇒ Object
write fdf content to path.
-
#to_xfdf ⇒ Object
generate XFDF content.
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 = {}, = {}) @fields = fields @options = { file: nil, id: nil }.merge() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/nguyen/xfdf.rb', line 6 def @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_xfdf ⇒ Object
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: [:file]) if [:file] xml.ids(original: [:id], modified: [:id]) if [: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 |