Class: Svnx::Base::Entries

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Logue::Loggable
Defined in:
lib/svnx/base/entries.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ Entries

Returns a new instance of Entries.



21
22
23
24
25
26
27
# File 'lib/svnx/base/entries.rb', line 21

def initialize lines
  # it's a hash, but indexed with integers, for non-sequential access:
  @entries  = Hash.new
  doc       = REXML::Document.new Array(lines).join
  @elements = get_elements doc
  @size     = @elements.size
end

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



19
20
21
# File 'lib/svnx/base/entries.rb', line 19

def size
  @size
end

Instance Method Details

#[](idx) ⇒ Object

this doesn’t handle negative indices



38
39
40
41
42
43
44
45
46
# File 'lib/svnx/base/entries.rb', line 38

def [] idx
  if entry = @entries[idx]
    return entry
  end
  if idx < 0 || idx >= size
    raise "error: index #{idx} is not in range(0 .. #{size})"
  end
  @entries[idx] = create_entry @elements[idx + 1]
end

#create_entry(xmlelement) ⇒ Object



33
34
35
# File 'lib/svnx/base/entries.rb', line 33

def create_entry xmlelement
  raise "create_entry must be implemented for: #{self.class}"
end

#each(&blk) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/svnx/base/entries.rb', line 48

def each(&blk)
  # all elements must be processed before each can run:
  if @elements
    # a little confusing here: REXML does each_with_index with idx
    # zero-based, but elements[0] is invalid.
    @elements.each_with_index do |element, idx|
      @entries[idx] ||= create_entry(element)
    end

    @elements = nil
  end

  @entries.keys.sort.collect { |idx| @entries[idx] }.each(&blk)
end

#get_elements(doc) ⇒ Object



29
30
31
# File 'lib/svnx/base/entries.rb', line 29

def get_elements doc
  raise "get_elements must be implemented for: #{self.class}"
end