Class: Rusk::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/rusk/book.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, &block) ⇒ Book

Returns a new instance of Book.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rusk/book.rb', line 8

def initialize(file, &block)
  @files = Zip::ZipFile.open(file)
  @content = Nokogiri::XML(@files.read("content.xml"))
  @sheets = @content.xpath("//table:table")

  if block
    begin
      yield self
    ensure
      @files.close
    end
  end
end

Instance Attribute Details

#sheetsObject (readonly)

Returns the value of attribute sheets.



6
7
8
# File 'lib/rusk/book.rb', line 6

def sheets
  @sheets
end

Class Method Details

.open(file, &block) ⇒ Object



35
36
37
# File 'lib/rusk/book.rb', line 35

def self.open(file, &block)
  new(file, &block)
end

Instance Method Details

#[](name_or_index) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rusk/book.rb', line 22

def [](name_or_index)
  if name_or_index.is_a? Numeric
    sheet = @sheets[name_or_index]
  else
    sheet = @sheets.detect{ |i| i["table:name"] == name_or_index }
  end
  sheet && Rusk::Sheet.new(sheet)
end

#saveObject



31
32
33
# File 'lib/rusk/book.rb', line 31

def save
  @files.get_output_stream("content.xml") { |f| f.puts @content }
end