Class: SheetsDB::Spreadsheet

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

Defined Under Namespace

Classes: LastWorksheetCannotBeDeletedError, WorksheetAssociationAlreadyRegisteredError, WorksheetNotFoundError

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

.create_worksheet_association(resource, **kwargs) ⇒ Object



26
27
28
29
30
# File 'lib/sheets_db/spreadsheet.rb', line 26

def create_worksheet_association(resource, **kwargs)
  define_method(resource) do
    worksheet_association(resource, **kwargs)
  end
end

.has_many(resource, worksheet_name:, class_name:) ⇒ Object



10
11
12
13
# File 'lib/sheets_db/spreadsheet.rb', line 10

def has_many(resource, worksheet_name:, class_name:)
  register_worksheet_association(resource, worksheet_name: worksheet_name, class_name: class_name)
  create_worksheet_association(resource, worksheet_name: worksheet_name, class_name: class_name)
end

.register_worksheet_association(resource, worksheet_name:, class_name:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/sheets_db/spreadsheet.rb', line 15

def register_worksheet_association(resource, worksheet_name:, class_name:)
  @associations ||= {}
  if @associations.fetch(resource, nil)
    raise WorksheetAssociationAlreadyRegisteredError
  end
  @associations[resource] = {
    worksheet_name: worksheet_name,
    class_name: class_name
  }
end

Instance Method Details

#clean_up_default_worksheet!(force: false) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/sheets_db/spreadsheet.rb', line 95

def clean_up_default_worksheet!(force: false)
  default_sheet = google_drive_resource.worksheet_by_title("Sheet1")
  return unless default_sheet

  raise LastWorksheetCannotBeDeletedError if google_drive_resource.worksheets.count == 1

  wrap_worksheet(google_drive_resource: default_sheet).
    delete_google_drive_resource!(force: force)
end

#find_and_setup_worksheet!(title:, type: nil, create: false) ⇒ Object



88
89
90
91
92
93
# File 'lib/sheets_db/spreadsheet.rb', line 88

def find_and_setup_worksheet!(title:, type: nil, create: false)
  resource = google_drive_worksheet_by_title(title, create: create)
  set_up_worksheet!(google_drive_resource: resource, type: type)
rescue ChildResourceNotFoundError
  raise WorksheetNotFoundError
end

#find_association_by_id(association_name, id) ⇒ Object



33
34
35
# File 'lib/sheets_db/spreadsheet.rb', line 33

def find_association_by_id(association_name, id)
  find_associations_by_ids(association_name, [id]).first
end

#find_associations_by_attribute(association_name, attribute_name, value) ⇒ Object



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

def find_associations_by_attribute(association_name, attribute_name, value)
  send(association_name).find_by_attribute(attribute_name, value)
end

#find_associations_by_ids(association_name, ids) ⇒ Object



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

def find_associations_by_ids(association_name, ids)
  send(association_name).find_by_ids(ids)
end

#find_or_create_worksheet!(title:, type: nil) ⇒ Object



84
85
86
# File 'lib/sheets_db/spreadsheet.rb', line 84

def find_or_create_worksheet!(title:, type: nil)
  find_and_setup_worksheet!(title: title, type: type, create: true)
end

#find_worksheet(title:, type: nil) ⇒ Object



74
75
76
77
78
# File 'lib/sheets_db/spreadsheet.rb', line 74

def find_worksheet(title:, type: nil)
  find_worksheet!(title: title, type: type)
rescue WorksheetNotFoundError
  nil
end

#find_worksheet!(title:, type: nil) ⇒ Object



80
81
82
# File 'lib/sheets_db/spreadsheet.rb', line 80

def find_worksheet!(title:, type: nil)
  find_and_setup_worksheet!(title: title, type: type, create: false)
end

#google_drive_worksheet_by_title(title, **kwargs) ⇒ Object



105
106
107
# File 'lib/sheets_db/spreadsheet.rb', line 105

def google_drive_worksheet_by_title(title, **kwargs)
  find_child_google_drive_resource_by(type: :worksheet, title: title, **kwargs)
end

#select_from_association(association_name, &block) ⇒ Object



45
46
47
# File 'lib/sheets_db/spreadsheet.rb', line 45

def select_from_association(association_name, &block)
  send(association_name).select(&block)
end

#set_up_worksheet!(google_drive_resource:, type: nil) ⇒ Object



62
63
64
# File 'lib/sheets_db/spreadsheet.rb', line 62

def set_up_worksheet!(google_drive_resource:, type: nil)
  wrap_worksheet(google_drive_resource: google_drive_resource, type: type).set_up!
end

#worksheet_association(association_name, worksheet_name:, class_name:) ⇒ Object



49
50
51
52
53
# File 'lib/sheets_db/spreadsheet.rb', line 49

def worksheet_association(association_name, worksheet_name:, class_name:)
  @associated_resources ||= {}
  @associated_resources[association_name] ||=
    find_or_create_worksheet!(title: worksheet_name, type: Support.constantize(class_name))
end

#worksheetsObject



55
56
57
58
59
60
# File 'lib/sheets_db/spreadsheet.rb', line 55

def worksheets
  @anonymous_resources ||= {}
  @anonymous_resources[:worksheets] ||= google_drive_resource.worksheets.map { |raw|
    set_up_worksheet!(google_drive_resource: raw)
  }
end

#wrap_worksheet(google_drive_resource:, type: nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/sheets_db/spreadsheet.rb', line 66

def wrap_worksheet(google_drive_resource:, type: nil)
  Worksheet.new(
    google_drive_resource: google_drive_resource,
    spreadsheet: self,
    type: type
  )
end