Class: Datagrid::Drivers::Array

Inherits:
AbstractDriver show all
Defined in:
lib/datagrid/drivers/array.rb

Overview

:nodoc:

Constant Summary

Constants inherited from AbstractDriver

Datagrid::Drivers::AbstractDriver::TIMESTAMP_CLASSES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractDriver

#append_column_queries, #default_preload, guess_driver, inherited, #match?, #normalized_column_type, #where_by_timestamp_gotcha

Class Method Details

.match?(scope) ⇒ Boolean

Returns:

  • (Boolean)

5
6
7
# File 'lib/datagrid/drivers/array.rb', line 5

def self.match?(scope)
  !Datagrid::Drivers::ActiveRecord.match?(scope) && (scope.is_a?(::Array) || scope.is_a?(Enumerator))
end

Instance Method Details

#asc(scope, order) ⇒ Object


19
20
21
22
23
24
25
# File 'lib/datagrid/drivers/array.rb', line 19

def asc(scope, order)
  return scope unless order
  return scope if order.empty?
  scope.sort_by do |object|
    object.send(order)
  end
end

#batch_each(scope, batch_size, &block) ⇒ Object


72
73
74
# File 'lib/datagrid/drivers/array.rb', line 72

def batch_each(scope, batch_size, &block)
  scope.each(&block)
end

#can_preload?(scope, association) ⇒ Boolean

Returns:

  • (Boolean)

80
81
82
# File 'lib/datagrid/drivers/array.rb', line 80

def can_preload?(scope, association)
  false
end

#column_names(scope) ⇒ Object


68
69
70
# File 'lib/datagrid/drivers/array.rb', line 68

def column_names(scope)
  []
end

#contains(scope, field, value) ⇒ Object


62
63
64
65
66
# File 'lib/datagrid/drivers/array.rb', line 62

def contains(scope, field, value)
  scope.select do |object|
    object.send(field).to_s.include?(value)
  end
end

#default_cache_key(asset) ⇒ Object


76
77
78
# File 'lib/datagrid/drivers/array.rb', line 76

def default_cache_key(asset)
  asset
end

#default_order(scope, column_name) ⇒ Object


31
32
33
# File 'lib/datagrid/drivers/array.rb', line 31

def default_order(scope, column_name)
  column_name
end

#desc(scope, order) ⇒ Object


27
28
29
# File 'lib/datagrid/drivers/array.rb', line 27

def desc(scope, order)
  asc(scope, order).reverse
end

#greater_equal(scope, field, value) ⇒ Object


39
40
41
42
43
44
# File 'lib/datagrid/drivers/array.rb', line 39

def greater_equal(scope, field, value)
  scope.select do |object|
    compare_value = object.send(field)
    compare_value.respond_to?(:>=) && compare_value >= value
  end
end

#has_column?(scope, column_name) ⇒ Boolean

Returns:

  • (Boolean)

53
54
55
# File 'lib/datagrid/drivers/array.rb', line 53

def has_column?(scope, column_name)
  scope.any? && scope.first.respond_to?(column_name)
end

#is_timestamp?(scope, column_name) ⇒ Boolean

Returns:

  • (Boolean)

57
58
59
60
# File 'lib/datagrid/drivers/array.rb', line 57

def is_timestamp?(scope, column_name)
  has_column?(scope, column_name) &&
    timestamp_class?(scope.first.send(column_name).class)
end

#less_equal(scope, field, value) ⇒ Object


46
47
48
49
50
51
# File 'lib/datagrid/drivers/array.rb', line 46

def less_equal(scope, field, value)
  scope.select do |object|
    compare_value = object.send(field)
    compare_value.respond_to?(:<=) && compare_value <= value
  end
end

#reverse_order(scope) ⇒ Object


35
36
37
# File 'lib/datagrid/drivers/array.rb', line 35

def reverse_order(scope)
  scope.reverse
end

#to_scope(scope) ⇒ Object


9
10
11
# File 'lib/datagrid/drivers/array.rb', line 9

def to_scope(scope)
  scope
end

#where(scope, attribute, value) ⇒ Object


13
14
15
16
17
# File 'lib/datagrid/drivers/array.rb', line 13

def where(scope, attribute, value)
  scope.select do |object|
    object.send(attribute) == value
  end
end