Class: SheetsDB::Resource

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sheets_db/resource.rb

Direct Known Subclasses

Collection, Spreadsheet

Defined Under Namespace

Classes: CollectionTypeAlreadyRegisteredError, ResourceTypeMismatchError

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(google_drive_resource) ⇒ Resource

Returns a new instance of Resource.


55
56
57
# File 'lib/sheets_db/resource.rb', line 55

def initialize(google_drive_resource)
  @google_drive_resource = google_drive_resource
end

Class Attribute Details

.resource_typeObject (readonly)

Returns the value of attribute resource_type


7
8
9
# File 'lib/sheets_db/resource.rb', line 7

def resource_type
  @resource_type
end

Instance Attribute Details

#google_drive_resourceObject (readonly)

Returns the value of attribute google_drive_resource


53
54
55
# File 'lib/sheets_db/resource.rb', line 53

def google_drive_resource
  @google_drive_resource
end

Class Method Details

.belongs_to_many(resource, class_name:) ⇒ Object


26
27
28
29
30
31
32
33
34
# File 'lib/sheets_db/resource.rb', line 26

def belongs_to_many(resource, class_name:)
  register_association(resource, class_name: class_name, resource_type: :parents)
  define_method(resource) do
    result = instance_variable_get(:"@#{resource}")
    result || instance_variable_set(:"@#{resource}",
      google_drive_resource.parents.map { |id| Support.constantize(class_name).find_by_id(id) }
    )
  end
end

.find_by_id(id, session: SheetsDB::Session.default) ⇒ Object


18
19
20
21
22
23
24
# File 'lib/sheets_db/resource.rb', line 18

def find_by_id(id, session: SheetsDB::Session.default)
  google_drive_resource = session.raw_file_by_id(id)
  if @resource_type && !google_drive_resource.is_a?(@resource_type)
    fail(ResourceTypeMismatchError, "The file with id #{id} is not a #{@resource_type}")
  end
  new(google_drive_resource)
end

.inherited(subclass) ⇒ Object


9
10
11
12
# File 'lib/sheets_db/resource.rb', line 9

def inherited(subclass)
  super
  subclass.instance_variable_set(:@resource_type, @resource_type)
end

.register_association(resource, class_name:, resource_type:) ⇒ Object


36
37
38
39
40
41
42
43
44
45
# File 'lib/sheets_db/resource.rb', line 36

def register_association(resource, class_name:, resource_type:)
  @associations ||= {}
  if @associations.values.any? { |value| value[:resource_type] == resource_type }
    raise CollectionTypeAlreadyRegisteredError
  end
  @associations[resource] = {
    resource_type: resource_type,
    class_name: class_name
  }
end

.set_resource_type(resource_type) ⇒ Object


14
15
16
# File 'lib/sheets_db/resource.rb', line 14

def set_resource_type(resource_type)
  @resource_type = resource_type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?


59
60
61
62
# File 'lib/sheets_db/resource.rb', line 59

def ==(other)
  other.is_a?(self.class) &&
    other.google_drive_resource == google_drive_resource
end

#base_attributesObject


70
71
72
73
74
75
76
77
# File 'lib/sheets_db/resource.rb', line 70

def base_attributes
  {
    id: id,
    name: name,
    created_at: created_at,
    updated_at: updated_at
  }
end

#hashObject


66
67
68
# File 'lib/sheets_db/resource.rb', line 66

def hash
  [self.class, google_drive_resource].hash
end