Class: Lessonly::Resource
- Inherits:
-
Sawyer::Resource
- Object
- Sawyer::Resource
- Lessonly::Resource
show all
- Defined in:
- lib/lessonly/resource.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.all(query = {}) ⇒ Object
8
9
10
|
# File 'lib/lessonly/resource.rb', line 8
def self.all(query = {})
client.get collection_path, query: query
end
|
.basename ⇒ Object
rubocop:enable PredicateName
67
68
69
|
# File 'lib/lessonly/resource.rb', line 67
def self.basename
name.split('::').last.underscore.dasherize.pluralize
end
|
.client ⇒ Object
88
89
90
|
# File 'lib/lessonly/resource.rb', line 88
def self.client
@client ||= Lessonly::Client.new
end
|
.collection_path ⇒ Object
24
25
26
|
# File 'lib/lessonly/resource.rb', line 24
def self.collection_path
basename
end
|
.create(params, query = {}) ⇒ Object
20
21
22
|
# File 'lib/lessonly/resource.rb', line 20
def self.create(params, query = {})
client.post collection_path, new(client.agent, params), query: query
end
|
.define_has_many_getter(relation) ⇒ Object
rubocop:enable PredicateName
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/lessonly/resource.rb', line 35
def self.define_has_many_getter(relation)
define_method relation do
if (memoized = instance_variable_get("@#{relation}"))
memoized
else
entries = client.get("#{href}/#{relation}")
instance_variable_set("@#{relation}", entries)
entries
end
end
end
|
.find(id) ⇒ Object
12
13
14
|
# File 'lib/lessonly/resource.rb', line 12
def self.find(id)
find_by_href("#{collection_path}/#{id}")
end
|
.find_by_href(href) ⇒ Object
16
17
18
|
# File 'lib/lessonly/resource.rb', line 16
def self.find_by_href(href)
client.get(href)
end
|
.has_many(relation) ⇒ Object
rubocop:disable PredicateName
29
30
31
32
|
# File 'lib/lessonly/resource.rb', line 29
def self.has_many(relation)
define_has_many_getter(relation)
end
|
.has_one(relation, options = {}) ⇒ Object
rubocop:disable PredicateName
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/lessonly/resource.rb', line 48
def self.has_one(relation, options = {})
define_method relation do
if (memoized = instance_variable_get("@#{relation}"))
memoized
else
klass = options[:klass] || "Lessonly::#{relation.classify}"
id_attr = options[:using] ? options[:using] : "#{relation}_id"
relation_id = instance_variable_get('@attrs')[id_attr]
if relation_id
item = klass.constantize.find(relation_id)
instance_variable_set("@#{relation}", item)
end
end
end
end
|
Instance Method Details
#client ⇒ Object
92
93
94
|
# File 'lib/lessonly/resource.rb', line 92
def client
self.class.client
end
|
#destroy ⇒ Object
76
77
78
|
# File 'lib/lessonly/resource.rb', line 76
def destroy
client.delete href, self
end
|
#href ⇒ Object
84
85
86
|
# File 'lib/lessonly/resource.rb', line 84
def href
"#{self.class.collection_path}/#{id}"
end
|
#reload ⇒ Object
80
81
82
|
# File 'lib/lessonly/resource.rb', line 80
def reload
self.class.find_by_href(href)
end
|
#update(params) ⇒ Object
71
72
73
74
|
# File 'lib/lessonly/resource.rb', line 71
def update(params)
self.attrs = attrs.merge(params)
client.put href, self
end
|