Class: SheetsDB::Spreadsheet

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

Defined Under Namespace

Classes: WorksheetAssociationAlreadyRegisteredError, WorksheetNotFoundError

Instance Attribute Summary

Attributes inherited from Resource

#google_drive_resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#==, #base_attributes, belongs_to_many, find_by_id, #hash, inherited, #initialize, register_association, set_resource_type

Constructor Details

This class inherits a constructor from SheetsDB::Resource

Class Method Details

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


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sheets_db/spreadsheet.rb', line 24

def self.create_worksheet_association(resource, worksheet_name:, class_name:)
  define_method(resource) do
    @worksheets ||= {}
    @worksheets[resource] ||= begin
      google_drive_worksheet = google_drive_resource.worksheet_by_title(worksheet_name)
      raise WorksheetNotFoundError, worksheet_name if google_drive_worksheet.nil?
      Worksheet.new(
        spreadsheet: self,
        google_drive_resource: google_drive_worksheet,
        type: Support.constantize(class_name)
      )
    end
  end
end

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


8
9
10
11
# File 'lib/sheets_db/spreadsheet.rb', line 8

def self.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


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

def self.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

#find_association_by_id(association_name, id) ⇒ Object


39
40
41
# File 'lib/sheets_db/spreadsheet.rb', line 39

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


47
48
49
# File 'lib/sheets_db/spreadsheet.rb', line 47

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


43
44
45
# File 'lib/sheets_db/spreadsheet.rb', line 43

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

#select_from_association(association_name, &block) ⇒ Object


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

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