Class: SheetsDB::Collection

Inherits:
Resource show all
Defined in:
lib/sheets_db/collection.rb

Defined Under Namespace

Classes: SpreadsheetNotFoundError

Instance Attribute Summary

Attributes inherited from Resource

#google_drive_resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, association_methods_for_type, #base_attributes, belongs_to_many, #delete!, find, find_by_id, find_by_url, #find_child_google_drive_resource_by, #hash, inherited, #initialize, register_association, #reload!, set_resource_type, wrap_google_drive_resource

Constructor Details

This class inherits a constructor from SheetsDB::Resource

Class Method Details

.has_many(resource, class_name:, resource_type: :subcollections) ⇒ Object



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

def has_many(resource, class_name:, resource_type: :subcollections)
  unless [:subcollections, :spreadsheets].include?(resource_type)
    raise ArgumentError, "resource_type must be :subcollections or :spreadsheets"
  end
  register_association(resource, class_name: class_name, resource_type: resource_type)
  define_method(resource) do
    @associated_resources ||= {}
    @associated_resources[resource] ||= google_drive_resource.send(resource_type).map { |raw|
      Support.constantize(class_name).new(raw)
    }
  end
end

Instance Method Details

#collectionsObject



57
58
59
60
61
# File 'lib/sheets_db/collection.rb', line 57

def collections
  @anonymous_resources ||= {}
  @anonymous_resources[:collections] ||=
    google_drive_resource.subcollections.map { |raw| Collection.new(raw) }
end

#find_and_wrap_spreadsheet!(title:, create: false) ⇒ Object



45
46
47
48
49
# File 'lib/sheets_db/collection.rb', line 45

def find_and_wrap_spreadsheet!(title:, create: false)
  Spreadsheet.new(google_drive_spreadsheet_by_title(title, create: create))
rescue ChildResourceNotFoundError
  raise SpreadsheetNotFoundError
end

#find_or_create_spreadsheet!(title:) ⇒ Object



41
42
43
# File 'lib/sheets_db/collection.rb', line 41

def find_or_create_spreadsheet!(title:)
  find_and_wrap_spreadsheet!(title: title, create: true)
end

#find_spreadsheet(title:) ⇒ Object



31
32
33
34
35
# File 'lib/sheets_db/collection.rb', line 31

def find_spreadsheet(title:)
  find_spreadsheet!(title: title)
rescue ChildResourceNotFoundError
  nil
end

#find_spreadsheet!(title:) ⇒ Object



37
38
39
# File 'lib/sheets_db/collection.rb', line 37

def find_spreadsheet!(title:)
  find_and_wrap_spreadsheet!(title: title, create: false)
end

#spreadsheetsObject



51
52
53
54
55
# File 'lib/sheets_db/collection.rb', line 51

def spreadsheets
  @anonymous_resources ||= {}
  @anonymous_resources[:spreadsheets] ||=
    google_drive_resource.spreadsheets.map { |raw| Spreadsheet.new(raw) }
end