Class: SheetsDB::Spreadsheet
Defined Under Namespace
Classes: LastWorksheetCannotBeDeletedError, WorksheetAssociationAlreadyRegisteredError, WorksheetNotFoundError
Constant Summary
collapse
- SHEET_URL_REGEX =
/spreadsheets\/d\/(?<sheet_id>[^\/]+)/.freeze
- DEFAULT_WORKSHEET_TITLE =
"Sheet1".freeze
Instance Attribute Summary
Attributes inherited from Resource
#google_drive_resource
Class Method Summary
collapse
Instance Method Summary
collapse
-
#clean_up_default_worksheet!(force: false) ⇒ Object
-
#existing_raw_data_from_worksheet(worksheet_title: nil) ⇒ Object
-
#find_and_setup_worksheet!(title:, type: nil, create: false) ⇒ Object
-
#find_association_by_id(association_name, id) ⇒ Object
-
#find_associations_by_attribute(association_name, attribute_name, value) ⇒ Object
-
#find_associations_by_ids(association_name, ids) ⇒ Object
-
#find_or_create_worksheet!(title:, type: nil) ⇒ Object
-
#find_worksheet(title:, type: nil) ⇒ Object
-
#find_worksheet!(title:, type: nil) ⇒ Object
-
#google_drive_worksheet_by_title(title, **kwargs) ⇒ Object
-
#select_from_association(association_name, &block) ⇒ Object
-
#set_up_worksheet!(google_drive_resource:, type: nil) ⇒ Object
-
#worksheet_association(association_name, worksheet_name:, class_name:) ⇒ Object
-
#worksheets ⇒ Object
-
#wrap_worksheet(google_drive_resource:, type: nil) ⇒ Object
-
#write_raw_data_to_worksheet!(data, worksheet_title: nil, rewrite: false) ⇒ Object
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
Class Method Details
.create_worksheet_association(resource, **kwargs) ⇒ Object
29
30
31
32
33
|
# File 'lib/sheets_db/spreadsheet.rb', line 29
def create_worksheet_association(resource, **kwargs)
define_method(resource) do
worksheet_association(resource, **kwargs)
end
end
|
35
36
37
|
# File 'lib/sheets_db/spreadsheet.rb', line 35
def (id_string)
(matches = SHEET_URL_REGEX.match(id_string)) ? matches[:sheet_id] : id_string&.gsub("/", "")
end
|
.has_many(resource, worksheet_name:, class_name:) ⇒ Object
13
14
15
16
|
# File 'lib/sheets_db/spreadsheet.rb', line 13
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
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/sheets_db/spreadsheet.rb', line 18
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
111
112
113
114
115
116
117
118
119
|
# File 'lib/sheets_db/spreadsheet.rb', line 111
def clean_up_default_worksheet!(force: false)
default_sheet = google_drive_resource.worksheet_by_title(DEFAULT_WORKSHEET_TITLE)
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
|
#existing_raw_data_from_worksheet(worksheet_title: nil) ⇒ Object
45
46
47
|
# File 'lib/sheets_db/spreadsheet.rb', line 45
def existing_raw_data_from_worksheet(worksheet_title: nil)
find_worksheet!(title: worksheet_title || DEFAULT_WORKSHEET_TITLE).existing_raw_data
end
|
#find_and_setup_worksheet!(title:, type: nil, create: false) ⇒ Object
104
105
106
107
108
109
|
# File 'lib/sheets_db/spreadsheet.rb', line 104
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
49
50
51
|
# File 'lib/sheets_db/spreadsheet.rb', line 49
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
57
58
59
|
# File 'lib/sheets_db/spreadsheet.rb', line 57
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
53
54
55
|
# File 'lib/sheets_db/spreadsheet.rb', line 53
def find_associations_by_ids(association_name, ids)
send(association_name).find_by_ids(ids)
end
|
#find_or_create_worksheet!(title:, type: nil) ⇒ Object
100
101
102
|
# File 'lib/sheets_db/spreadsheet.rb', line 100
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
90
91
92
93
94
|
# File 'lib/sheets_db/spreadsheet.rb', line 90
def find_worksheet(title:, type: nil)
find_worksheet!(title: title, type: type)
rescue WorksheetNotFoundError
nil
end
|
#find_worksheet!(title:, type: nil) ⇒ Object
96
97
98
|
# File 'lib/sheets_db/spreadsheet.rb', line 96
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
121
122
123
|
# File 'lib/sheets_db/spreadsheet.rb', line 121
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
61
62
63
|
# File 'lib/sheets_db/spreadsheet.rb', line 61
def select_from_association(association_name, &block)
send(association_name).select(&block)
end
|
#set_up_worksheet!(google_drive_resource:, type: nil) ⇒ Object
78
79
80
|
# File 'lib/sheets_db/spreadsheet.rb', line 78
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
65
66
67
68
69
|
# File 'lib/sheets_db/spreadsheet.rb', line 65
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
|
#worksheets ⇒ Object
71
72
73
74
75
76
|
# File 'lib/sheets_db/spreadsheet.rb', line 71
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
82
83
84
85
86
87
88
|
# File 'lib/sheets_db/spreadsheet.rb', line 82
def wrap_worksheet(google_drive_resource:, type: nil)
Worksheet.new(
google_drive_resource: google_drive_resource,
spreadsheet: self,
type: type
)
end
|
#write_raw_data_to_worksheet!(data, worksheet_title: nil, rewrite: false) ⇒ Object
40
41
42
43
|
# File 'lib/sheets_db/spreadsheet.rb', line 40
def write_raw_data_to_worksheet!(data, worksheet_title: nil, rewrite: false)
find_or_create_worksheet!(title: worksheet_title || DEFAULT_WORKSHEET_TITLE).
write_raw_data!(data, rewrite: rewrite)
end
|