Class: Graphiti::Resource
- Inherits:
-
Object
show all
- Includes:
- Links, Configuration, DSL, Documentation, Interface, Persistence, Sideloading
- Defined in:
- lib/graphiti/resource.rb,
lib/graphiti/resource/dsl.rb,
lib/graphiti/resource/remote.rb,
lib/graphiti/resource/interface.rb,
lib/graphiti/resource/persistence.rb,
lib/graphiti/resource/sideloading.rb,
lib/graphiti/resource/polymorphism.rb,
lib/graphiti/resource/configuration.rb,
lib/graphiti/resource/documentation.rb
Defined Under Namespace
Modules: Configuration, DSL, Documentation, Interface, Persistence, Polymorphism, Remote, Sideloading
Constant Summary
Constants included
from Links
Links::DEFAULT_ACTIONS
Configuration::DEFAULT_MAX_PAGE_SIZE
Class Method Summary
collapse
Instance Method Summary
collapse
-
#after_commit(model, metadata) ⇒ Object
-
#after_graph_persist(model, metadata) ⇒ Object
-
#around_scoping(scope, query_hash) {|scope| ... } ⇒ Object
-
#associate(parent, child, association_name, type) ⇒ Object
-
#associate_all(parent, children, association_name, type) ⇒ Object
-
#base_scope ⇒ Object
-
#before_commit(model, metadata) ⇒ Object
-
#before_resolve(scope, query) ⇒ Object
-
#build_scope(base, query, opts = {}) ⇒ Object
-
#context ⇒ Object
-
#context_namespace ⇒ Object
-
#decorate_record(record) ⇒ Object
-
#disassociate(parent, child, association_name, type) ⇒ Object
-
#persist_with_relationships(meta, attributes, relationships, caller_model = nil, foreign_key = nil) ⇒ Object
-
#resolve(scope) ⇒ Object
-
#serializer_for(model) ⇒ Object
-
#stat(attribute, calculation) ⇒ Object
-
#transaction ⇒ Object
-
#typecast(name, value, flag) ⇒ Object
-
#with_context(object, namespace = nil) ⇒ Object
#assign_attributes, #build, #create, #delete, #destroy, #save, #update
included
#adapter, #all_attributes, #attributes, #default_filters, #extra_attributes, #filters, #get_attr, #get_attr!, #pagination, #sort_all, #sorts, #stats
Class Method Details
41
42
43
|
# File 'lib/graphiti/resource.rb', line 41
def self.context
Graphiti.context[:object]
end
|
.context_namespace ⇒ Object
49
50
51
|
# File 'lib/graphiti/resource.rb', line 49
def self.context_namespace
Graphiti.context[:namespace]
end
|
Instance Method Details
#after_commit(model, metadata) ⇒ Object
129
130
131
132
133
134
|
# File 'lib/graphiti/resource.rb', line 129
def after_commit(model, metadata)
hooks = self.class.config[:after_commit][metadata[:method]] || []
hooks.each do |hook|
instance_exec(model, metadata, &hook)
end
end
|
#after_graph_persist(model, metadata) ⇒ Object
115
116
117
118
119
120
|
# File 'lib/graphiti/resource.rb', line 115
def after_graph_persist(model, metadata)
hooks = self.class.config[:after_graph_persist][metadata[:method]] || []
hooks.each do |hook|
instance_exec(model, metadata, &hook)
end
end
|
#around_scoping(scope, query_hash) {|scope| ... } ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/graphiti/resource.rb', line 11
def around_scoping(scope, query_hash)
= query_hash[:extra_fields] || {}
= [type] || []
.each do |name|
if (config = self.class.config[:extra_attributes][name])
scope = instance_exec(scope, &config[:hook]) if config[:hook]
end
end
yield scope
end
|
#associate(parent, child, association_name, type) ⇒ Object
87
88
89
|
# File 'lib/graphiti/resource.rb', line 87
def associate(parent, child, association_name, type)
adapter.associate(parent, child, association_name, type)
end
|
#associate_all(parent, children, association_name, type) ⇒ Object
83
84
85
|
# File 'lib/graphiti/resource.rb', line 83
def associate_all(parent, children, association_name, type)
adapter.associate_all(parent, children, association_name, type)
end
|
#base_scope ⇒ Object
61
62
63
|
# File 'lib/graphiti/resource.rb', line 61
def base_scope
adapter.base_scope(model)
end
|
#before_commit(model, metadata) ⇒ Object
122
123
124
125
126
127
|
# File 'lib/graphiti/resource.rb', line 122
def before_commit(model, metadata)
hooks = self.class.config[:before_commit][metadata[:method]] || []
hooks.each do |hook|
instance_exec(model, metadata, &hook)
end
end
|
#before_resolve(scope, query) ⇒ Object
107
108
109
|
# File 'lib/graphiti/resource.rb', line 107
def before_resolve(scope, query)
scope
end
|
#build_scope(base, query, opts = {}) ⇒ Object
57
58
59
|
# File 'lib/graphiti/resource.rb', line 57
def build_scope(base, query, opts = {})
Scope.new(base, self, query, opts)
end
|
45
46
47
|
# File 'lib/graphiti/resource.rb', line 45
def context
self.class.context
end
|
#context_namespace ⇒ Object
53
54
55
|
# File 'lib/graphiti/resource.rb', line 53
def context_namespace
self.class.context_namespace
end
|
#decorate_record(record) ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/graphiti/resource.rb', line 27
def decorate_record(record)
unless record.instance_variable_get(:@__graphiti_serializer)
serializer = serializer_for(record)
record.instance_variable_set(:@__graphiti_serializer, serializer)
record.instance_variable_set(:@__graphiti_resource, self)
end
end
|
#disassociate(parent, child, association_name, type) ⇒ Object
91
92
93
|
# File 'lib/graphiti/resource.rb', line 91
def disassociate(parent, child, association_name, type)
adapter.disassociate(parent, child, association_name, type)
end
|
#persist_with_relationships(meta, attributes, relationships, caller_model = nil, foreign_key = nil) ⇒ Object
95
96
97
98
99
|
# File 'lib/graphiti/resource.rb', line 95
def persist_with_relationships(meta, attributes, relationships, caller_model = nil, foreign_key = nil)
persistence = Graphiti::Util::Persistence \
.new(self, meta, attributes, relationships, caller_model, foreign_key)
persistence.run
end
|
#resolve(scope) ⇒ Object
111
112
113
|
# File 'lib/graphiti/resource.rb', line 111
def resolve(scope)
adapter.resolve(scope)
end
|
#serializer_for(model) ⇒ Object
23
24
25
|
# File 'lib/graphiti/resource.rb', line 23
def serializer_for(model)
serializer
end
|
#stat(attribute, calculation) ⇒ Object
101
102
103
104
105
|
# File 'lib/graphiti/resource.rb', line 101
def stat(attribute, calculation)
stats_dsl = stats[attribute] || stats[attribute.to_sym]
raise Errors::StatNotFound.new(attribute, calculation) unless stats_dsl
stats_dsl.calculation(calculation)
end
|
#transaction ⇒ Object
136
137
138
139
140
141
142
143
144
145
146
|
# File 'lib/graphiti/resource.rb', line 136
def transaction
response = nil
begin
adapter.transaction(model) do
response = yield
end
rescue Errors::ValidationError => e
response = {result: e.validation_response}
end
response
end
|
#typecast(name, value, flag) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/graphiti/resource.rb', line 65
def typecast(name, value, flag)
att = get_attr!(name, flag, request: true)
type_name = att[:type]
if flag == :filterable
type_name = filters[name][:type]
end
type = Graphiti::Types[type_name]
return if value.nil? && type[:kind] != "array"
begin
flag = :read if flag == :readable
flag = :write if flag == :writable
flag = :params if [:sortable, :filterable].include?(flag)
type[flag][value]
rescue => e
raise Errors::TypecastFailed.new(self, name, value, e, type_name)
end
end
|
#with_context(object, namespace = nil) ⇒ Object
35
36
37
38
39
|
# File 'lib/graphiti/resource.rb', line 35
def with_context(object, namespace = nil)
Graphiti.with_context(object, namespace) do
yield
end
end
|