Class: Graphiti::Adapters::Abstract

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

Direct Known Subclasses

ActiveRecord, Null

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence::Associations

#process_belongs_to, #process_has_many, #update_foreign_key, #update_foreign_key_for_parents, #update_foreign_type

Constructor Details

#initialize(resource) ⇒ Abstract

Returns a new instance of Abstract.



9
10
11
# File 'lib/graphiti/adapters/abstract.rb', line 9

def initialize(resource)
  @resource = resource
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

Class Method Details

.default_operatorsObject



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

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, :eql, :not_eql],
    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



414
415
416
# File 'lib/graphiti/adapters/abstract.rb', line 414

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 }



17
18
19
20
21
22
23
24
25
# File 'lib/graphiti/adapters/abstract.rb', line 17

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



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

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



369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/graphiti/adapters/abstract.rb', line 369

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



358
359
360
361
362
363
364
365
366
367
# File 'lib/graphiti/adapters/abstract.rb', line 358

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



278
279
280
# File 'lib/graphiti/adapters/abstract.rb', line 278

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

#base_scope(model) ⇒ Object



227
228
229
# File 'lib/graphiti/adapters/abstract.rb', line 227

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

#belongs_to_many_filter(sideload, scope, value) ⇒ Object



354
355
356
# File 'lib/graphiti/adapters/abstract.rb', line 354

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



388
389
390
# File 'lib/graphiti/adapters/abstract.rb', line 388

def build(model_class)
  model_class.new
end

#can_group?Boolean

Returns:

  • (Boolean)


418
419
420
# File 'lib/graphiti/adapters/abstract.rb', line 418

def can_group?
  false
end

#closeObject



407
408
# File 'lib/graphiti/adapters/abstract.rb', line 407

def close
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



267
268
269
# File 'lib/graphiti/adapters/abstract.rb', line 267

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

#destroy(model_instance) ⇒ Object



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

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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#filter_date_eq(scope, attribute, value) ⇒ Object



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

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

#filter_date_gt(scope, attribute, value) ⇒ Object



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

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

#filter_date_gte(scope, attribute, value) ⇒ Object



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

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

#filter_date_lt(scope, attribute, value) ⇒ Object



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

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

#filter_date_lte(scope, attribute, value) ⇒ Object



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

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



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

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



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

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

#filter_datetime_gt(scope, attribute, value) ⇒ Object



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

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

#filter_datetime_gte(scope, attribute, value) ⇒ Object



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

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

#filter_datetime_lt(scope, attribute, value) ⇒ Object



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

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

#filter_datetime_lte(scope, attribute, value) ⇒ Object



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

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



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

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



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

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

#filter_float_gt(scope, attribute, value) ⇒ Object



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

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

#filter_float_gte(scope, attribute, value) ⇒ Object



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

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

#filter_float_lt(scope, attribute, value) ⇒ Object



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

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

#filter_float_lte(scope, attribute, value) ⇒ Object



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

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



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

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



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

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

#filter_integer_gt(scope, attribute, value) ⇒ Object



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

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

#filter_integer_gte(scope, attribute, value) ⇒ Object



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

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

#filter_integer_lt(scope, attribute, value) ⇒ Object



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

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

#filter_integer_lte(scope, attribute, value) ⇒ Object



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

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



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

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



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

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

#filter_string_eql(scope, attribute, value) ⇒ Object



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

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

#filter_string_match(scope, attribute, value) ⇒ Object



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#filter_string_suffix(scope, attribute, value) ⇒ Object



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

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

#filter_uuid_eq(scope, attribute, value) ⇒ Object



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

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



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

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



300
301
302
# File 'lib/graphiti/adapters/abstract.rb', line 300

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



311
312
313
# File 'lib/graphiti/adapters/abstract.rb', line 311

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



240
241
242
# File 'lib/graphiti/adapters/abstract.rb', line 240

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

#paginate(scope, current_page, per_page, offset) ⇒ Object

Returns the scope.

Examples:

ActiveRecord default

# via kaminari gem
def paginate(scope, current_page, per_page, offset)
  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

  • offset (Integer)

    The offset to start from

Returns:

  • the scope



255
256
257
# File 'lib/graphiti/adapters/abstract.rb', line 255

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

#persistence_attributes(persistance, attributes) ⇒ Object



410
411
412
# File 'lib/graphiti/adapters/abstract.rb', line 410

def persistence_attributes(persistance, attributes)
  attributes
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:



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

def resolve(scope)
  scope
end

#save(model_instance) ⇒ Object



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

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



289
290
291
# File 'lib/graphiti/adapters/abstract.rb', line 289

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


327
328
329
# File 'lib/graphiti/adapters/abstract.rb', line 327

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