Class: XmlSplit
- Inherits:
-
Object
- Object
- XmlSplit
- Includes:
- Enumerable
- Defined in:
- lib/xml_split.rb,
lib/xml_split/version.rb
Constant Summary collapse
- POSSIBLE_BIN_NAMES =
%w{ sgrep sgrep2 }
- MAGIC_START =
'n8frNy6J'
- MAGIC_STOP =
'H6py5pxG'
- CHUNK_SIZE =
65536
- VERSION =
"0.0.2"
Instance Attribute Summary collapse
-
#caching ⇒ Object
readonly
Returns the value of attribute caching.
-
#element ⇒ Object
readonly
Returns the value of attribute element.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #each(&blk) ⇒ Object
-
#initialize(path, element, options = {}) ⇒ XmlSplit
constructor
A new instance of XmlSplit.
Constructor Details
#initialize(path, element, options = {}) ⇒ XmlSplit
Returns a new instance of XmlSplit.
21 22 23 24 25 26 27 28 |
# File 'lib/xml_split.rb', line 21 def initialize(path, element, = {}) @nodes = [] @cache_full = false @path = File. path @element = element @caching = .fetch :caching, false end |
Instance Attribute Details
#caching ⇒ Object (readonly)
Returns the value of attribute caching.
19 20 21 |
# File 'lib/xml_split.rb', line 19 def caching @caching end |
#element ⇒ Object (readonly)
Returns the value of attribute element.
18 19 20 |
# File 'lib/xml_split.rb', line 18 def element @element end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
17 18 19 |
# File 'lib/xml_split.rb', line 17 def path @path end |
Class Method Details
.sgrep_bin ⇒ Object
5 6 7 |
# File 'lib/xml_split.rb', line 5 def sgrep_bin @sgrep_bin ||= POSSIBLE_BIN_NAMES.detect { |bin| `which #{bin}`; $?.success? } or raise("Can't find any of #{POSSIBLE_BIN_NAMES.inspect} in your PATH") end |
Instance Method Details
#each(&blk) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/xml_split.rb', line 30 def each(&blk) if caching and @cache_full @nodes.each(&blk) else caching = caching leftover = '' IO.popen([ XmlSplit.sgrep_bin, '-n', '-o', "#{MAGIC_START}%r#{MAGIC_STOP}", %{"#{start}" .. "#{stop}"}, path ]) do |io| while additional = io.read(CHUNK_SIZE) buffer = leftover + additional while (start = buffer.index(MAGIC_START)) and (stop = buffer.index(MAGIC_STOP)) node = buffer[(start+MAGIC_START.length)...stop] + '>' if caching @nodes << node end yield node buffer = buffer[(stop+MAGIC_STOP.length)..-1] end leftover = buffer end end @cache_full = true end end |