Class: GoogleDrive::List

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/google_drive/list.rb

Overview

Provides access to cells using column names. Use GoogleDrive::Worksheet#list to get GoogleDrive::List object. – This is implemented as wrapper of GoogleDrive::Worksheet i.e. using cells feed, not list feed. In this way, we can easily provide consistent API as GoogleDrive::Worksheet using save()/reload().

Instance Method Summary collapse

Constructor Details

#initialize(worksheet) ⇒ List

:nodoc:



18
19
20
# File 'lib/google_drive/list.rb', line 18

def initialize(worksheet) #:nodoc:
  @worksheet = worksheet
end

Instance Method Details

#[](index) ⇒ Object

Returns Hash-like object (GoogleDrive::ListRow) for the row with the index. Keys of the object are colum names (the first row). The second row has index 0.

Note that updates to the returned object are not sent to the server until you call GoogleDrive::Worksheet#save().



33
34
35
# File 'lib/google_drive/list.rb', line 33

def [](index)
  ListRow.new(self, index)
end

#[]=(index, hash) ⇒ Object

Updates the row with the index with the given Hash object. Keys of hash are colum names (the first row). The second row has index 0.

Note that update is not sent to the server until you call GoogleDrive::Worksheet#save().



43
44
45
# File 'lib/google_drive/list.rb', line 43

def []=(index, hash)
  self[index].replace(hash)
end

#each(&_block) ⇒ Object

Iterates over Hash-like object (GoogleDrive::ListRow) for each row (except for the first row). Keys of the object are colum names (the first row).



50
51
52
53
54
# File 'lib/google_drive/list.rb', line 50

def each(&_block)
  for i in 0...size
    yield(self[i])
  end
end

#get(index, key) ⇒ Object

:nodoc:



93
94
95
# File 'lib/google_drive/list.rb', line 93

def get(index, key) #:nodoc:
  @worksheet[index + 2, key_to_col(key)]
end

#input_value(index, key) ⇒ Object

:nodoc:



101
102
103
# File 'lib/google_drive/list.rb', line 101

def input_value(index, key) #:nodoc:
  @worksheet.input_value(index + 2, key_to_col(key))
end

#keysObject

Column names i.e. the contents of the first row. Duplicates are removed.



58
59
60
# File 'lib/google_drive/list.rb', line 58

def keys
  (1..@worksheet.num_cols).map { |i| @worksheet[1, i] }.uniq
end

#keys=(ary) ⇒ Object

Updates column names i.e. the contents of the first row.

Note that update is not sent to the server until you call GoogleDrive::Worksheet#save().



66
67
68
69
70
71
72
73
# File 'lib/google_drive/list.rb', line 66

def keys=(ary)
  for i in 1..ary.size
    @worksheet[1, i] = ary[i - 1]
  end
  for i in (ary.size + 1)..@worksheet.num_cols
    @worksheet[1, i] = ''
  end
end

#numeric_value(index, key) ⇒ Object

:nodoc:



97
98
99
# File 'lib/google_drive/list.rb', line 97

def numeric_value(index, key) #:nodoc:
  @worksheet.numeric_value(index + 2, key_to_col(key))
end

#push(hash) ⇒ Object

Adds a new row to the bottom. Keys of hash are colum names (the first row). Returns GoogleDrive::ListRow for the new row.

Note that update is not sent to the server until you call GoogleDrive::Worksheet#save().



81
82
83
84
85
# File 'lib/google_drive/list.rb', line 81

def push(hash)
  row = self[size]
  row.update(hash)
  row
end

#set(index, key, value) ⇒ Object

:nodoc:



105
106
107
# File 'lib/google_drive/list.rb', line 105

def set(index, key, value) #:nodoc:
  @worksheet[index + 2, key_to_col(key)] = value
end

#sizeObject

Number of non-empty rows in the worksheet excluding the first row.



23
24
25
# File 'lib/google_drive/list.rb', line 23

def size
  @worksheet.num_rows - 1
end

#to_hash_arrayObject

Returns all rows (except for the first row) as Array of Hash. Keys of Hash objects are colum names (the first row).



89
90
91
# File 'lib/google_drive/list.rb', line 89

def to_hash_array
  map(&:to_hash)
end