Class: QueryBatchRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/query_batch_request.rb

Overview

Facilitate Batch updates using the pattern of filter_query -> objects to update stub object -> attributes/params to update

!! Should say nothing about how to update the objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ QueryBatchRequest

Returns a new instance of QueryBatchRequest.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/query_batch_request.rb', line 58

def initialize(params)
  @async = params[:async]
  @async_cutoff = params[:async_cutoff]
  @cap = params[:cap]
  @cap_reason = params[:cap_reason]
  @klass = params[:klass]
  @pre_update_filter = params[:pre_update_filter]
  @object_filter_params = params[:object_filter_params]
  @object_params = params[:object_params]
  @preview = params[:preview]
  @project_id = params[:project_id]
  @user_id = params[:user_id]

  @mode = params[:mode]
end

Instance Attribute Details

#async ⇒ Boolean

Returns whether to run the job asyncronously.

Returns:

  • (Boolean) —

    whether to run the job asyncronously



24
25
26
# File 'lib/query_batch_request.rb', line 24

def async
  @async
end

#async_cutoff ⇒ Object

Number of records at which process is automatically made async



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

def async_cutoff
  @async_cutoff
end

#cap ⇒ nil, Integer

Returns the max allowed records.

Returns:

  • (nil, Integer) —

    the max allowed records



31
32
33
# File 'lib/query_batch_request.rb', line 31

def cap
  @cap
end

#cap_reason ⇒ String?

Returns the reason the cap is why it is (not required).

Returns:

  • (String, nil) —

    the reason the cap is why it is (not required)



35
36
37
# File 'lib/query_batch_request.rb', line 35

def cap_reason
  @cap_reason
end

#filter ⇒ Object

Returns a Queries::<>::Filter instance.

Returns:

  • a Queries::<>::Filter instance



42
43
44
# File 'lib/query_batch_request.rb', line 42

def filter
  @filter
end

#klass ⇒ String

Returns defines the Filter/Model to act on.

Returns:

  • (String) —

    defines the Filter/Model to act on



39
40
41
# File 'lib/query_batch_request.rb', line 39

def klass
  @klass
end

#object_filter_params ⇒ Object

Parameters:

  • object_filter_params (Hash) —

    The params to parameterize a Queries::*::Filter. Defines the objects to be updated



16
17
18
# File 'lib/query_batch_request.rb', line 16

def object_filter_params
  @object_filter_params
end

#object_params ⇒ Object

Parameters:

  • object_params (Hash) —

    update the records to these attributes



11
12
13
# File 'lib/query_batch_request.rb', line 11

def object_params
  @object_params
end

#pre_update_filter ⇒ Proc?

Optional per-record pre-update check. Called with each object before update! is attempted. Return nil/false to allow the update, or return an error message String to skip the record and add it to validation_errors.

Returns:

  • (Proc, nil) —

    Optional per-record pre-update check. Called with each object before update! is attempted. Return nil/false to allow the update, or return an error message String to skip the record and add it to validation_errors.



56
57
58
# File 'lib/query_batch_request.rb', line 56

def pre_update_filter
  @pre_update_filter
end

#preview ⇒ Boolean

Returns true - rollback changes, can not be used with async.

Returns:

  • (Boolean) —

    true - rollback changes, can not be used with async



20
21
22
# File 'lib/query_batch_request.rb', line 20

def preview
  @preview
end

#project_id ⇒ Object

Returns the value of attribute project_id.



48
49
50
# File 'lib/query_batch_request.rb', line 48

def project_id
  @project_id
end

#total_attempted ⇒ Object

Count of the records retured in filter. Not used in async



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

def total_attempted
  @total_attempted
end

#user_id ⇒ Object

Returns the value of attribute user_id.



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

def user_id
  @user_id
end

Instance Method Details

#capped? ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
117
# File 'lib/query_batch_request.rb', line 110

def capped?
  if total_attempted > cap
    @cap_reason = "Update to more objects than allowed (#{cap}) requested." if cap_reason.blank?
    return true
  else
    false
  end
end

#response_params ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/query_batch_request.rb', line 94

def response_params
  {
    klass:,
    async:,
    async_cutoff:,
    preview:,
    cap:,
    cap_reason:,
    total_attempted:
  }
end

#run_async? ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
122
123
# File 'lib/query_batch_request.rb', line 119

def run_async?
  if async_cutoff
    return total_attempted > async_cutoff
  end
end

#stub_response ⇒ Object



106
107
108
# File 'lib/query_batch_request.rb', line 106

def stub_response
  BatchResponse.new(response_params)
end

#unprocessable? ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/query_batch_request.rb', line 78

def unprocessable?
  object_filter_params.blank? || object_params.blank? || (run_async? && preview)
end