Class: SheetsDB::Resource
- Inherits:
-
Object
- Object
- SheetsDB::Resource
show all
- Extended by:
- Forwardable
- Defined in:
- lib/sheets_db/resource.rb
Defined Under Namespace
Classes: ChildResourceNotFoundError, 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.
81
82
83
|
# File 'lib/sheets_db/resource.rb', line 81
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.
8
9
10
|
# File 'lib/sheets_db/resource.rb', line 8
def resource_type
@resource_type
end
|
Instance Attribute Details
#google_drive_resource ⇒ Object
Returns the value of attribute google_drive_resource.
79
80
81
|
# File 'lib/sheets_db/resource.rb', line 79
def google_drive_resource
@google_drive_resource
end
|
Class Method Details
.association_methods_for_type(type) ⇒ Object
64
65
66
67
68
69
70
71
|
# File 'lib/sheets_db/resource.rb', line 64
def association_methods_for_type(type)
raise ArgumentError unless %i[spreadsheet worksheet subcollection].include?(type)
google_type = type == :spreadsheet ? :file : type
create_prefix = type == :worksheet ? :add : :create
OpenStruct.new(find: :"#{google_type}_by_title", create: :"#{create_prefix}_#{type}")
end
|
.belongs_to_many(resource, class_name:) ⇒ Object
43
44
45
46
47
48
49
50
51
|
# File 'lib/sheets_db/resource.rb', line 43
def belongs_to_many(resource, class_name:)
register_association(resource, class_name: class_name, resource_type: :parents)
define_method(resource) do
@associated_resources ||= {}
@associated_resources[resource] ||= google_drive_resource.parents.map { |id|
Support.constantize(class_name).find_by_id(id)
}
end
end
|
.find(id_or_url, session: SheetsDB::Session.default) ⇒ Object
.find_by_id(id, session: SheetsDB::Session.default) ⇒ Object
29
30
31
|
# File 'lib/sheets_db/resource.rb', line 29
def find_by_id(id, session: SheetsDB::Session.default)
wrap_google_drive_resource(session.raw_file_by_id(id))
end
|
.find_by_url(url, session: SheetsDB::Session.default) ⇒ Object
25
26
27
|
# File 'lib/sheets_db/resource.rb', line 25
def find_by_url(url, session: SheetsDB::Session.default)
wrap_google_drive_resource(session.raw_file_by_url(url))
end
|
.inherited(subclass) ⇒ Object
10
11
12
13
|
# File 'lib/sheets_db/resource.rb', line 10
def inherited(subclass)
super
subclass.instance_variable_set(:@resource_type, @resource_type)
end
|
.register_association(resource, class_name:, resource_type:) ⇒ Object
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/sheets_db/resource.rb', line 53
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
15
16
17
|
# File 'lib/sheets_db/resource.rb', line 15
def set_resource_type(resource_type)
@resource_type = resource_type
end
|
.wrap_google_drive_resource(google_drive_resource) ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'lib/sheets_db/resource.rb', line 33
def wrap_google_drive_resource(google_drive_resource)
if resource_type && !google_drive_resource.is_a?(resource_type)
fail(
ResourceTypeMismatchError,
"The file #{google_drive_resource.human_url} is not a #{resource_type}"
)
end
new(google_drive_resource)
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
95
96
97
98
|
# File 'lib/sheets_db/resource.rb', line 95
def ==(other)
other.is_a?(self.class) &&
other.google_drive_resource == google_drive_resource
end
|
#base_attributes ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/sheets_db/resource.rb', line 106
def base_attributes
{
id: id,
name: name,
created_at: created_at,
updated_at: updated_at
}
end
|
#delete! ⇒ Object
91
92
93
|
# File 'lib/sheets_db/resource.rb', line 91
def delete!
google_drive_resource.delete
end
|
#find_child_google_drive_resource_by(type:, title:, create: false) ⇒ Object
115
116
117
118
119
120
121
122
|
# File 'lib/sheets_db/resource.rb', line 115
def find_child_google_drive_resource_by(type:, title:, create: false)
association_methods = self.class.association_methods_for_type(type)
child_resource = google_drive_resource.public_send(association_methods.find, title)
child_resource ||= google_drive_resource.public_send(association_methods.create, title) if create
raise ChildResourceNotFoundError, [self, type, title] if child_resource.nil?
child_resource
end
|
#hash ⇒ Object
102
103
104
|
# File 'lib/sheets_db/resource.rb', line 102
def hash
[self.class, google_drive_resource].hash
end
|
#reload! ⇒ Object
85
86
87
88
89
|
# File 'lib/sheets_db/resource.rb', line 85
def reload!
@associated_resources = nil
@anonymous_resources = nil
google_drive_resource.reload_metadata
end
|