Class: JsonapiCompliable::Adapters::Null

Inherits:
Abstract
  • Object
show all
Defined in:
lib/jsonapi_compliable/adapters/null.rb

Overview

The Null adapter is a 'pass-through' adapter. It won't modify the scope. Useful when your customization does not support all possible configuration (e.g. the service you hit does not support sorting)

Instance Method Summary collapse

Methods inherited from Abstract

#create, #destroy, #disassociate, #sideloading_module, #update

Instance Method Details

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

Assign these two objects together.

association_name and association_type come from your sideload configuration:

allow_sideload :the_name, type: the_type do
  # ... code.
end

Examples:

Basic accessor

def associate(parent, child, association_name, association_type)
  if association_type == :has_many
    parent.send(association_name).push(child)
  else
    child.send(:#{association_name}=", parent)
  end
end

Parameters:

  • parent

    The parent object (via the JSONAPI 'relationships' graph)

  • child

    The child object (via the JSONAPI 'relationships' graph)

  • association_name

    The 'relationships' key we are processing

  • association_type

    The Sideload type (see Sideload#type). Usually :has_many/:belongs_to/etc



61
62
# File 'lib/jsonapi_compliable/adapters/null.rb', line 61

def associate(parent, child, association_name, association_type)
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



28
29
30
# File 'lib/jsonapi_compliable/adapters/null.rb', line 28

def average(scope, attr)
  scope
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



23
24
25
# File 'lib/jsonapi_compliable/adapters/null.rb', line 23

def count(scope, attr)
  scope
end

#filter(scope, attribute, value) ⇒ Object

Returns the scope

Examples:

ActiveRecord default

def filter(scope, attribute, value)
  scope.where(attribute => value)
end

Parameters:

  • scope

    The scope object we are chaining

  • attribute (Symbol)

    The attribute name we are filtering

  • value

    The corresponding query parameter value

Returns:

  • the scope



8
9
10
# File 'lib/jsonapi_compliable/adapters/null.rb', line 8

def filter(scope, attribute, value)
  scope
end

#maximum(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



38
39
40
# File 'lib/jsonapi_compliable/adapters/null.rb', line 38

def maximum(scope, attr)
  scope
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



43
44
45
# File 'lib/jsonapi_compliable/adapters/null.rb', line 43

def minimum(scope, attr)
  scope
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



13
14
15
# File 'lib/jsonapi_compliable/adapters/null.rb', line 13

def order(scope, attribute, direction)
  scope
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



18
19
20
# File 'lib/jsonapi_compliable/adapters/null.rb', line 18

def paginate(scope, current_page, per_page)
  scope
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:



56
57
58
# File 'lib/jsonapi_compliable/adapters/null.rb', line 56

def resolve(scope)
  scope
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



33
34
35
# File 'lib/jsonapi_compliable/adapters/null.rb', line 33

def sum(scope, attr)
  scope
end

#transaction(model_class) ⇒ Object

Since this is a null adapter, just yield

Parameters:

  • model_class (Class)

    The class we're operating on

Returns:

  • Result of yield

See Also:



51
52
53
# File 'lib/jsonapi_compliable/adapters/null.rb', line 51

def transaction(model_class)
  yield
end