Class: Graphiti::Adapters::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/graphiti/adapters/abstract.rb

Direct Known Subclasses

ActiveRecord, Null

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Abstract

Returns a new instance of Abstract.



6
7
8
# File 'lib/graphiti/adapters/abstract.rb', line 6

def initialize(resource)
  @resource = resource
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/graphiti/adapters/abstract.rb', line 4

def resource
  @resource
end

Class Method Details

.default_operatorsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/graphiti/adapters/abstract.rb', line 24

def self.default_operators
  {
    string: [
      :eq,
      :not_eq,
      :eql,
      :not_eql,
      :prefix,
      :not_prefix,
      :suffix,
      :not_suffix,
      :match,
      :not_match,
    ],
    uuid: [:eq, :not_eq],
    enum: [:eq, :not_eq],
    integer_id: numerical_operators,
    integer: numerical_operators,
    big_decimal: numerical_operators,
    float: numerical_operators,
    boolean: [:eq],
    date: numerical_operators,
    datetime: numerical_operators,
    hash: [:eq],
    array: [:eq],
  }
end

.numerical_operatorsObject



403
404
405
# File 'lib/graphiti/adapters/abstract.rb', line 403

def self.numerical_operators
  [:eq, :not_eq, :gt, :gte, :lt, :lte].freeze
end

.sideloading_classesObject

You want to override this! Map of association_type => sideload_class e.g. { has_many: Adapters::ActiveRecord::HasManySideload }



14
15
16
17
18
19
20
21
22
# File 'lib/graphiti/adapters/abstract.rb', line 14

def self.sideloading_classes
  {
    has_many: ::Graphiti::Sideload::HasMany,
    belongs_to: ::Graphiti::Sideload::BelongsTo,
    has_one: ::Graphiti::Sideload::HasOne,
    many_to_many: ::Graphiti::Sideload::ManyToMany,
    polymorphic_belongs_to: ::Graphiti::Sideload::PolymorphicBelongsTo,
  }
end

Instance Method Details

#assign_attributes(model_instance, attributes) ⇒ Object

TODO respond to and specific error



389
390
391
392
393
# File 'lib/graphiti/adapters/abstract.rb', line 389

def assign_attributes(model_instance, attributes)
  attributes.each_pair do |k, v|
    model_instance.send(:"#{k}=", v)
  end
end

#associate(parent, child, association_name, association_type) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/graphiti/adapters/abstract.rb', line 365

def associate(parent, child, association_name, association_type)
  if activerecord_associate?(parent, child, association_name)
    activerecord_adapter.associate \
      parent, child, association_name, association_type
  elsif [:has_many, :many_to_many].include?(association_type)
    if parent.send(:"#{association_name}").nil?
      parent.send(:"#{association_name}=", [child])
    else
      parent.send(:"#{association_name}") << child
    end
  else
    parent.send(:"#{association_name}=", child)
  end
end

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



354
355
356
357
358
359
360
361
362
363
# File 'lib/graphiti/adapters/abstract.rb', line 354

def associate_all(parent, children, association_name, association_type)
  if activerecord_associate?(parent, children[0], association_name)
    activerecord_adapter.associate_all parent,
      children, association_name, association_type
  else
    children.each do |c|
      associate(parent, c, association_name, association_type)
    end
  end
end

#average(scope, attr) ⇒ Float

Returns the average of the scope.

Examples:

ActiveRecord default

def average(scope, attr)
  scope.average(attr).to_f
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Float)

    the average of the scope



274
275
276
# File 'lib/graphiti/adapters/abstract.rb', line 274

def average(scope, attr)
  raise "you must override #average in an adapter subclass"
end

#base_scope(model) ⇒ Object



224
225
226
# File 'lib/graphiti/adapters/abstract.rb', line 224

def base_scope(model)
  raise "you must override #base_scope in an adapter subclass"
end

#belongs_to_many_filter(sideload, scope, value) ⇒ Object



350
351
352
# File 'lib/graphiti/adapters/abstract.rb', line 350

def belongs_to_many_filter(sideload, scope, value)
  raise "You must implement #belongs_to_many_filter in an adapter subclass"
end

#build(model_class) ⇒ Object



384
385
386
# File 'lib/graphiti/adapters/abstract.rb', line 384

def build(model_class)
  model_class.new
end

#count(scope, attr) ⇒ Numeric

Returns the count of the scope.

Examples:

ActiveRecord default

def count(scope, attr)
  column = attr == :total ? :all : attr
  scope.uniq.count(column)
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Numeric)

    the count of the scope



263
264
265
# File 'lib/graphiti/adapters/abstract.rb', line 263

def count(scope, attr)
  raise "you must override #count in an adapter subclass"
end

#destroy(model_instance) ⇒ Object



399
400
401
# File 'lib/graphiti/adapters/abstract.rb', line 399

def destroy(model_instance)
  raise "you must override #destroy in an adapter subclass"
end

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



380
381
382
# File 'lib/graphiti/adapters/abstract.rb', line 380

def disassociate(parent, child, association_name, association_type)
  raise "you must override #disassociate in an adapter subclass"
end

#filter_big_decimal_eq(scope, attribute, value) ⇒ Object



148
149
150
# File 'lib/graphiti/adapters/abstract.rb', line 148

def filter_big_decimal_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_eq)
end

#filter_big_decimal_gt(scope, attribute, value) ⇒ Object



156
157
158
# File 'lib/graphiti/adapters/abstract.rb', line 156

def filter_big_decimal_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_gt)
end

#filter_big_decimal_gte(scope, attribute, value) ⇒ Object



160
161
162
# File 'lib/graphiti/adapters/abstract.rb', line 160

def filter_big_decimal_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_gte)
end

#filter_big_decimal_lt(scope, attribute, value) ⇒ Object



164
165
166
# File 'lib/graphiti/adapters/abstract.rb', line 164

def filter_big_decimal_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_lt)
end

#filter_big_decimal_lte(scope, attribute, value) ⇒ Object



168
169
170
# File 'lib/graphiti/adapters/abstract.rb', line 168

def filter_big_decimal_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_lte)
end

#filter_big_decimal_not_eq(scope, attribute, value) ⇒ Object



152
153
154
# File 'lib/graphiti/adapters/abstract.rb', line 152

def filter_big_decimal_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_decimal_not_eq)
end

#filter_boolean_eq(scope, attribute, value) ⇒ Object



220
221
222
# File 'lib/graphiti/adapters/abstract.rb', line 220

def filter_boolean_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_boolean_eq)
end

#filter_date_eq(scope, attribute, value) ⇒ Object



196
197
198
# File 'lib/graphiti/adapters/abstract.rb', line 196

def filter_date_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_eq)
end

#filter_date_gt(scope, attribute, value) ⇒ Object



204
205
206
# File 'lib/graphiti/adapters/abstract.rb', line 204

def filter_date_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_gt)
end

#filter_date_gte(scope, attribute, value) ⇒ Object



208
209
210
# File 'lib/graphiti/adapters/abstract.rb', line 208

def filter_date_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_gte)
end

#filter_date_lt(scope, attribute, value) ⇒ Object



212
213
214
# File 'lib/graphiti/adapters/abstract.rb', line 212

def filter_date_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_lt)
end

#filter_date_lte(scope, attribute, value) ⇒ Object



216
217
218
# File 'lib/graphiti/adapters/abstract.rb', line 216

def filter_date_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_lte)
end

#filter_date_not_eq(scope, attribute, value) ⇒ Object



200
201
202
# File 'lib/graphiti/adapters/abstract.rb', line 200

def filter_date_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_date_not_eq)
end

#filter_datetime_eq(scope, attribute, value) ⇒ Object



172
173
174
# File 'lib/graphiti/adapters/abstract.rb', line 172

def filter_datetime_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_eq)
end

#filter_datetime_gt(scope, attribute, value) ⇒ Object



180
181
182
# File 'lib/graphiti/adapters/abstract.rb', line 180

def filter_datetime_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_gt)
end

#filter_datetime_gte(scope, attribute, value) ⇒ Object



184
185
186
# File 'lib/graphiti/adapters/abstract.rb', line 184

def filter_datetime_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_gte)
end

#filter_datetime_lt(scope, attribute, value) ⇒ Object



188
189
190
# File 'lib/graphiti/adapters/abstract.rb', line 188

def filter_datetime_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_lt)
end

#filter_datetime_lte(scope, attribute, value) ⇒ Object



192
193
194
# File 'lib/graphiti/adapters/abstract.rb', line 192

def filter_datetime_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_lte)
end

#filter_datetime_not_eq(scope, attribute, value) ⇒ Object



176
177
178
# File 'lib/graphiti/adapters/abstract.rb', line 176

def filter_datetime_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_datetime_not_eq)
end

#filter_float_eq(scope, attribute, value) ⇒ Object



124
125
126
# File 'lib/graphiti/adapters/abstract.rb', line 124

def filter_float_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_eq)
end

#filter_float_gt(scope, attribute, value) ⇒ Object



132
133
134
# File 'lib/graphiti/adapters/abstract.rb', line 132

def filter_float_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_gt)
end

#filter_float_gte(scope, attribute, value) ⇒ Object



136
137
138
# File 'lib/graphiti/adapters/abstract.rb', line 136

def filter_float_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_gte)
end

#filter_float_lt(scope, attribute, value) ⇒ Object



140
141
142
# File 'lib/graphiti/adapters/abstract.rb', line 140

def filter_float_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_lt)
end

#filter_float_lte(scope, attribute, value) ⇒ Object



144
145
146
# File 'lib/graphiti/adapters/abstract.rb', line 144

def filter_float_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_lte)
end

#filter_float_not_eq(scope, attribute, value) ⇒ Object



128
129
130
# File 'lib/graphiti/adapters/abstract.rb', line 128

def filter_float_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_float_not_eq)
end

#filter_integer_eq(scope, attribute, value) ⇒ Object



100
101
102
# File 'lib/graphiti/adapters/abstract.rb', line 100

def filter_integer_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_eq)
end

#filter_integer_gt(scope, attribute, value) ⇒ Object



108
109
110
# File 'lib/graphiti/adapters/abstract.rb', line 108

def filter_integer_gt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_gt)
end

#filter_integer_gte(scope, attribute, value) ⇒ Object



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

def filter_integer_gte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_gte)
end

#filter_integer_lt(scope, attribute, value) ⇒ Object



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

def filter_integer_lt(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_lt)
end

#filter_integer_lte(scope, attribute, value) ⇒ Object



120
121
122
# File 'lib/graphiti/adapters/abstract.rb', line 120

def filter_integer_lte(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_lte)
end

#filter_integer_not_eq(scope, attribute, value) ⇒ Object



104
105
106
# File 'lib/graphiti/adapters/abstract.rb', line 104

def filter_integer_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_integer_not_eq)
end

#filter_string_eq(scope, attribute, value) ⇒ Object



52
53
54
# File 'lib/graphiti/adapters/abstract.rb', line 52

def filter_string_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_eq)
end

#filter_string_eql(scope, attribute, value) ⇒ Object



56
57
58
# File 'lib/graphiti/adapters/abstract.rb', line 56

def filter_string_eql(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_eql)
end

#filter_string_match(scope, attribute, value) ⇒ Object



84
85
86
# File 'lib/graphiti/adapters/abstract.rb', line 84

def filter_string_match(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_match)
end

#filter_string_not_eq(scope, attribute, value) ⇒ Object



60
61
62
# File 'lib/graphiti/adapters/abstract.rb', line 60

def filter_string_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_eq)
end

#filter_string_not_eql(scope, attribute, value) ⇒ Object



64
65
66
# File 'lib/graphiti/adapters/abstract.rb', line 64

def filter_string_not_eql(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_eql)
end

#filter_string_not_match(scope, attribute, value) ⇒ Object



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

def filter_string_not_match(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_match)
end

#filter_string_not_prefix(scope, attribute, value) ⇒ Object



72
73
74
# File 'lib/graphiti/adapters/abstract.rb', line 72

def filter_string_not_prefix(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_prefix)
end

#filter_string_not_suffix(scope, attribute, value) ⇒ Object



80
81
82
# File 'lib/graphiti/adapters/abstract.rb', line 80

def filter_string_not_suffix(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_not_suffix)
end

#filter_string_prefix(scope, attribute, value) ⇒ Object



68
69
70
# File 'lib/graphiti/adapters/abstract.rb', line 68

def filter_string_prefix(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_prefix)
end

#filter_string_suffix(scope, attribute, value) ⇒ Object



76
77
78
# File 'lib/graphiti/adapters/abstract.rb', line 76

def filter_string_suffix(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_string_suffix)
end

#filter_uuid_eq(scope, attribute, value) ⇒ Object



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

def filter_uuid_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_uuid_eq)
end

#filter_uuid_not_eq(scope, attribute, value) ⇒ Object



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

def filter_uuid_not_eq(scope, attribute, value)
  raise Errors::AdapterNotImplemented.new(self, attribute, :filter_uuid_not_eq)
end

#maximum(scope, attr) ⇒ Numeric

Returns the maximum value of the scope.

Examples:

ActiveRecord default

def maximum(scope, attr)
  scope.maximum(attr)
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Numeric)

    the maximum value of the scope



296
297
298
# File 'lib/graphiti/adapters/abstract.rb', line 296

def maximum(scope, attr)
  raise "you must override #maximum in an adapter subclass"
end

#minimum(scope, attr) ⇒ Numeric

Returns the maximum value of the scope.

Examples:

ActiveRecord default

def maximum(scope, attr)
  scope.maximum(attr)
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Numeric)

    the maximum value of the scope



307
308
309
# File 'lib/graphiti/adapters/abstract.rb', line 307

def minimum(scope, attr)
  raise "you must override #maximum in an adapter subclass"
end

#order(scope, attribute, direction) ⇒ Object

Returns the scope.

Examples:

ActiveRecord default

def order(scope, attribute, direction)
  scope.order(attribute => direction)
end

Parameters:

  • scope

    The scope object we are chaining

  • attribute (Symbol)

    The attribute name we are sorting

  • direction (Symbol)

    The direction we are sorting (asc/desc)

Returns:

  • the scope



237
238
239
# File 'lib/graphiti/adapters/abstract.rb', line 237

def order(scope, attribute, direction)
  raise "you must override #order in an adapter subclass"
end

#paginate(scope, current_page, per_page) ⇒ Object

Returns the scope.

Examples:

ActiveRecord default

# via kaminari gem
def paginate(scope, current_page, per_page)
  scope.page(current_page).per(per_page)
end

Parameters:

  • scope

    The scope object we are chaining

  • current_page (Integer)

    The current page number

  • per_page (Integer)

    The number of results per page

Returns:

  • the scope



251
252
253
# File 'lib/graphiti/adapters/abstract.rb', line 251

def paginate(scope, current_page, per_page)
  raise "you must override #paginate in an adapter subclass"
end

#resolve(scope) ⇒ Object

Resolve the scope. This is where you’d actually fire SQL, actually make an HTTP call, etc.

Examples:

ActiveRecordDefault

def resolve(scope)
  scope.to_a
end

Suggested Customization

# When making a service call, we suggest this abstraction
# 'scope' here is a hash
def resolve(scope)
  # The implementation of .where can be whatever you want
  SomeModelClass.where(scope)
end

Parameters:

  • scope

    The scope object to resolve

Returns:

  • an array of Model instances

See Also:



346
347
348
# File 'lib/graphiti/adapters/abstract.rb', line 346

def resolve(scope)
  scope
end

#save(model_instance) ⇒ Object



395
396
397
# File 'lib/graphiti/adapters/abstract.rb', line 395

def save(model_instance)
  raise "you must override #save in an adapter subclass"
end

#sum(scope, attr) ⇒ Numeric

Returns the sum of the scope.

Examples:

ActiveRecord default

def sum(scope, attr)
  scope.sum(attr)
end

Parameters:

  • scope

    the scope object we are chaining

  • attr (Symbol)

    corresponding stat attribute name

Returns:

  • (Numeric)

    the sum of the scope



285
286
287
# File 'lib/graphiti/adapters/abstract.rb', line 285

def sum(scope, attr)
  raise "you must override #sum in an adapter subclass"
end

#transaction(model_class) ⇒ Object

This method must yield the code to run within the transaction. This method should roll back the transaction if an error is raised.

Examples:

ActiveRecord default

def transaction(model_class)
  model_class.transaction do
    yield
  end
end

Parameters:

  • model_class (Class)

    The class we’re operating on

See Also:

  • Resource.model


323
324
325
# File 'lib/graphiti/adapters/abstract.rb', line 323

def transaction(model_class)
  raise "you must override #transaction in an adapter subclass, it must yield"
end