Class: SheetsDB::Resource
- Inherits:
-
Object
- Object
- SheetsDB::Resource
show all
- Extended by:
- Forwardable
- Defined in:
- lib/sheets_db/resource.rb
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.
50
51
52
|
# File 'lib/sheets_db/resource.rb', line 50
def initialize(google_drive_resource)
@google_drive_resource = google_drive_resource
end
|
Class Attribute Details
.resource_type ⇒ Object
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_resource ⇒ Object
Returns the value of attribute google_drive_resource.
48
49
50
|
# File 'lib/sheets_db/resource.rb', line 48
def google_drive_resource
@google_drive_resource
end
|
Class Method Details
.belongs_to_many(resource, class_name:) ⇒ Object
21
22
23
24
25
26
27
28
29
|
# File 'lib/sheets_db/resource.rb', line 21
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
13
14
15
16
17
18
19
|
# File 'lib/sheets_db/resource.rb', line 13
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
|
.register_association(resource, class_name:, resource_type:) ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/sheets_db/resource.rb', line 31
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
9
10
11
|
# File 'lib/sheets_db/resource.rb', line 9
def set_resource_type(resource_type)
@resource_type = resource_type
end
|
Instance Method Details
#==(other) ⇒ Object
54
55
56
57
|
# File 'lib/sheets_db/resource.rb', line 54
def ==(other)
other.is_a?(self.class) &&
other.google_drive_resource == google_drive_resource
end
|
#base_attributes ⇒ Object
59
60
61
62
63
64
65
66
|
# File 'lib/sheets_db/resource.rb', line 59
def base_attributes
{
id: id,
name: name,
created_at: created_at,
updated_at: updated_at
}
end
|