Class: Kiba::Extend::Destinations::XmlDir

Inherits:
Object
  • Object
show all
Includes:
Destinationable
Defined in:
lib/kiba/extend/destinations/xml_dir.rb

Overview

TODO:

Add collision logic in case, for example, dir/1.xml and dir/sub/1.xml both exist.

Note:

If 2+ XMLDocument objects contain the same base filename in their .url attribute (e.g. same-named files from different subdirectories via recursive: true), the later outputs will always clobber the earlier ones.

Writes each “row” (XMLDocument) out as its own file in a directory.

If the “row” has a usable .url (Nokogiri stashes the original filename there when a Document is parsed from something path-like, e.g. by Sources::XmlDir), that filename is reused for the output.

However, any document constructed on the fly (e.g. a new document built from XPath queries on others) will NOT have a filename saved in .url.

To cover that case, we use a counter and do zero-padded filenames for XMLDocuments that lack a path in the .url attribute.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Destinationable

included, is_destination?

Constructor Details

#initialize(dirpath:) ⇒ XmlDir

Returns a new instance of XmlDir.

Parameters:

  • dirpath (String)

    path to destination directory



57
58
59
60
61
# File 'lib/kiba/extend/destinations/xml_dir.rb', line 57

def initialize(dirpath:)
  @dirpath = dirpath
  @counter = 0
  ensure_dir
end

Class Method Details

.as_source_classObject



31
32
33
# File 'lib/kiba/extend/destinations/xml_dir.rb', line 31

def as_source_class
  Kiba::Extend::Sources::XmlDir
end

.default_file_optionsObject



35
36
37
# File 'lib/kiba/extend/destinations/xml_dir.rb', line 35

def default_file_options
  nil
end

.options_keyObject



39
40
41
# File 'lib/kiba/extend/destinations/xml_dir.rb', line 39

def options_key
  nil
end

.path_keyObject



43
44
45
# File 'lib/kiba/extend/destinations/xml_dir.rb', line 43

def path_key
  :dirpath
end

.requires_path?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/kiba/extend/destinations/xml_dir.rb', line 47

def requires_path?
  true
end

.special_optionsObject



51
52
53
# File 'lib/kiba/extend/destinations/xml_dir.rb', line 51

def special_options
  []
end

Instance Method Details

#closeObject



70
71
# File 'lib/kiba/extend/destinations/xml_dir.rb', line 70

def close
end

#write(row) ⇒ Object

Parameters:

  • row

    any object responding to to_xml (e.g. Nokogiri::XML::Document, Nokogiri::XML::Node)



65
66
67
68
# File 'lib/kiba/extend/destinations/xml_dir.rb', line 65

def write(row)
  @counter += 1
  File.write(filepath(row), row.to_xml)
end