Module: XMLable::Mixins::DocumentStorage::ClassMethods

Defined in:
lib/xmlable/mixins/document_storage.rb

Instance Method Summary collapse

Instance Method Details

#__document_handlerXMLable::Handlers::Document

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get document’s handler



109
110
111
# File 'lib/xmlable/mixins/document_storage.rb', line 109

def __document_handler
  @__document_handler ||= Handlers::Document.new(self)
end

#__nokogiri_export_optionsNokogiri::XML::ParseOptions

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Nokogiri’s options for exporting

Returns:

  • (Nokogiri::XML::ParseOptions)


98
99
100
# File 'lib/xmlable/mixins/document_storage.rb', line 98

def __nokogiri_export_options
  @__nokogiri_export_options ||= Options::NokogiriExport.new
end

#__nokogiri_parse_optionsNokogiri::XML::ParseOptions

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Nokogiri’s options for parsing

Returns:

  • (Nokogiri::XML::ParseOptions)


87
88
89
# File 'lib/xmlable/mixins/document_storage.rb', line 87

def __nokogiri_parse_options
  @__nokogiri_parse_options ||= Nokogiri::XML::ParseOptions.new.recover
end

#document(opts = {}) ⇒ Object

Define document params

Parameters:

  • opts (Hash) (defaults to: {})


57
58
59
# File 'lib/xmlable/mixins/document_storage.rb', line 57

def document(opts = {})
  opt(opts) if opts.size > 0
end

#from_xml(xml, _opts = {}) ⇒ XMLable::Document

Initialize document from XML

Parameters:

  • xml (Nokogiri::XML::Document, String)
  • _opts (Hash) (defaults to: {})

Returns:



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/xmlable/mixins/document_storage.rb', line 121

def from_xml(xml, _opts = {})
  xml = '' unless xml
  if xml.is_a?(String)
    doc = Nokogiri::XML(xml) do |config|
      config.options  = __nokogiri_parse_options.to_i
    end
  elsif xml.is?(Nokogiri::XML::Document)
    doc = xml
  else raise "Don't know how to parse '#{xml.class}'"
  end
  __document_handler.from_xml_document(doc)
end

#nokogiri_export(*args) ⇒ Object

Set the Nokogiri’s export params



73
74
75
76
77
78
# File 'lib/xmlable/mixins/document_storage.rb', line 73

def nokogiri_export(*args)
  __nokogiri_export_options.tap do |opts|
    opts.merge!(args.pop) if args.last.is_a?(Hash)
    args.each { |opt| opts.save_with.send(opt) if opts.save_with.respond_to?(opt) }
  end
end

#nokogiri_parse(*args) ⇒ Object

Set the Nokogiri’s parsing params



64
65
66
67
68
# File 'lib/xmlable/mixins/document_storage.rb', line 64

def nokogiri_parse(*args)
  args.each do |opt|
    __nokogiri_parse_options.send(opt) if __nokogiri_parse_options.respond_to?(opt)
  end
end