Class: Spreet::Sheets

Inherits:
Object
  • Object
show all
Defined in:
lib/spreet/sheets.rb

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Sheets

Returns a new instance of Sheets.

Raises:

  • (ArgumentError)


5
6
7
8
9
# File 'lib/spreet/sheets.rb', line 5

def initialize(document)
  raise ArgumentError.new("Must be a Document") unless document.is_a?(Document)
  @document = document
  @array = []
end

Instance Method Details

#[](sheet) ⇒ Object



33
34
35
36
# File 'lib/spreet/sheets.rb', line 33

def [](sheet)
  sheet = index(sheet)
  return (sheet.is_a?(Integer) ? @array[sheet] : nil)
end

#add(name = nil, position = -1)) ⇒ Object



27
28
29
30
31
# File 'lib/spreet/sheets.rb', line 27

def add(name=nil, position=-1)
  sheet = Sheet.new(@document, name)
  @array.insert(position, sheet)
  return sheet
end

#countObject



11
12
13
# File 'lib/spreet/sheets.rb', line 11

def count
  @array.size
end

#each(&block) ⇒ Object



55
56
57
58
59
# File 'lib/spreet/sheets.rb', line 55

def each(&block)
  for item in @array
    yield item
  end
end

#index(name_or_sheet) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spreet/sheets.rb', line 15

def index(name_or_sheet)
  if name_or_sheet.is_a? String
    @array.each_index do |i|
      return i if @array[i].name == name_or_sheet
    end
  elsif name_or_sheet.is_a? Integer
    return (@array[name_or_sheet].nil? ? nil : name_or_sheet)
  else
    return @array.index(name_or_sheet)
  end
end

#move(sheet, shift = 0) ⇒ Object



42
43
44
45
46
47
# File 'lib/spreet/sheets.rb', line 42

def move(sheet, shift=0)
  position = index(sheet) + shift
  position = 0 if position < 0
  position = self.count-1 if position >= self.count
  move_at(sheet, position)
end

#move_at(sheet, position = -1)) ⇒ Object



49
50
51
52
53
# File 'lib/spreet/sheets.rb', line 49

def move_at(sheet, position=-1)
  if i = index(sheet)
    @array.insert(position, @array.delete_at(i))
  end
end

#remove(sheet) ⇒ Object



38
39
40
# File 'lib/spreet/sheets.rb', line 38

def remove(sheet)
  @array.delete_at(index(sheet))
end