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

Direct Known Subclasses

Query::RemoteSideloadResource

Defined Under Namespace

Modules: Configuration, DSL, Documentation, Interface, Persistence, Polymorphism, Remote, Sideloading

Constant Summary

Constants included from Links

Links::DEFAULT_ACTIONS

Constants included from Configuration

Configuration::DEFAULT_MAX_PAGE_SIZE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#assign_attributes, #build, #create, #delete, #destroy, #save, #update

Methods included from Sideloading

included

Methods included from Configuration

#adapter, #all_attributes, #attributes, #default_filters, #extra_attributes, #filters, #get_attr, #get_attr!, #grouped_filters, #pagination, #sort_all, #sorts, #stats

Class Method Details

.contextObject



46
47
48
# File 'lib/graphiti/resource.rb', line 46

def self.context
  Graphiti.context[:object]
end

.context_namespaceObject



54
55
56
# File 'lib/graphiti/resource.rb', line 54

def self.context_namespace
  Graphiti.context[:namespace]
end

Instance Method Details

#after_commit(model, metadata) ⇒ Object



134
135
136
137
138
139
# File 'lib/graphiti/resource.rb', line 134

def after_commit(model, )
  hooks = self.class.config[:after_commit][[:method]] || []
  hooks.each do |hook|
    instance_exec(model, , &hook)
  end
end

#after_filtering(scope) ⇒ Object



23
24
25
# File 'lib/graphiti/resource.rb', line 23

def after_filtering(scope)
  scope
end

#after_graph_persist(model, metadata) ⇒ Object



120
121
122
123
124
125
# File 'lib/graphiti/resource.rb', line 120

def after_graph_persist(model, )
  hooks = self.class.config[:after_graph_persist][[:method]] || []
  hooks.each do |hook|
    instance_exec(model, , &hook)
  end
end

#around_scoping(scope, query_hash) {|scope| ... } ⇒ Object

Yields:

  • (scope)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/graphiti/resource.rb', line 11

def around_scoping(scope, query_hash)
  extra_fields = query_hash[:extra_fields] || {}
  extra_fields = extra_fields[type] || []
  extra_fields.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



92
93
94
# File 'lib/graphiti/resource.rb', line 92

def associate(parent, child, association_name, type)
  adapter.associate(parent, child, association_name, type)
end

#associate_all(parent, children, association_name, type) ⇒ Object



88
89
90
# File 'lib/graphiti/resource.rb', line 88

def associate_all(parent, children, association_name, type)
  adapter.associate_all(parent, children, association_name, type)
end

#base_scopeObject



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

def base_scope
  adapter.base_scope(model)
end

#before_commit(model, metadata) ⇒ Object



127
128
129
130
131
132
# File 'lib/graphiti/resource.rb', line 127

def before_commit(model, )
  hooks = self.class.config[:before_commit][[:method]] || []
  hooks.each do |hook|
    instance_exec(model, , &hook)
  end
end

#before_resolve(scope, query) ⇒ Object



112
113
114
# File 'lib/graphiti/resource.rb', line 112

def before_resolve(scope, query)
  scope
end

#build_scope(base, query, opts = {}) ⇒ Object



62
63
64
# File 'lib/graphiti/resource.rb', line 62

def build_scope(base, query, opts = {})
  Scope.new(base, self, query, opts)
end

#contextObject



50
51
52
# File 'lib/graphiti/resource.rb', line 50

def context
  self.class.context
end

#context_namespaceObject



58
59
60
# File 'lib/graphiti/resource.rb', line 58

def context_namespace
  self.class.context_namespace
end

#decorate_record(record, index = nil) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/graphiti/resource.rb', line 31

def decorate_record(record, index = nil)
  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)
    record.instance_variable_set(:@__graphiti_index, index) if index
  end
end

#disassociate(parent, child, association_name, type) ⇒ Object



96
97
98
# File 'lib/graphiti/resource.rb', line 96

def disassociate(parent, child, association_name, type)
  adapter.disassociate(parent, child, association_name, type)
end


157
158
159
160
161
# File 'lib/graphiti/resource.rb', line 157

def links(model)
  self.class.links.each_with_object({}) do |(name, blk), memo|
    memo[name] = instance_exec(model, &blk)
  end
end

#links?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/graphiti/resource.rb', line 153

def links?
  self.class.links.any?
end

#persist_with_relationships(meta, attributes, relationships, caller_model = nil, foreign_key = nil) ⇒ Object



100
101
102
103
104
# File 'lib/graphiti/resource.rb', line 100

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



116
117
118
# File 'lib/graphiti/resource.rb', line 116

def resolve(scope)
  adapter.resolve(scope)
end

#serializer_for(model) ⇒ Object



27
28
29
# File 'lib/graphiti/resource.rb', line 27

def serializer_for(model)
  serializer
end

#stat(attribute, calculation) ⇒ Object



106
107
108
109
110
# File 'lib/graphiti/resource.rb', line 106

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

#transactionObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/graphiti/resource.rb', line 141

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/graphiti/resource.rb', line 70

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



40
41
42
43
44
# File 'lib/graphiti/resource.rb', line 40

def with_context(object, namespace = nil)
  Graphiti.with_context(object, namespace) do
    yield
  end
end