Class: Rhoconnect::Handler::Changes::Engine

Inherits:
Object
  • Object
show all
Includes:
Helpers::AuthMethod, Helpers::Binding, Helpers::SourceJob
Defined in:
lib/rhoconnect/handler/changes/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::AuthMethod

#auth_method

Methods included from Helpers::Binding

#bind_handler

Methods included from Helpers::SourceJob

#async

Constructor Details

#initialize(operations, model, route_handler, params = {}) ⇒ Engine

Returns a new instance of Engine.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rhoconnect/handler/changes/engine.rb', line 11

def initialize(operations, model, route_handler, params = {})
  raise ArgumentError.new(UNKNOWN_SOURCE) unless (model and model.source)
  raise ArgumentError.new('Invalid app for source') unless model.source.app
  raise ArgumentError.new('Invalid CUD handler') unless route_handler
  
  @source = model.source
  # if handler is not bound - bind it to self
  # normally it should be bound to a Controller's instance
  @route_handler = bind_handler(:cud_handler_method, route_handler)  
  @params = params
  @model = model
  @operations = operations
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



5
6
7
# File 'lib/rhoconnect/handler/changes/engine.rb', line 5

def model
  @model
end

#operationsObject

Returns the value of attribute operations.



5
6
7
# File 'lib/rhoconnect/handler/changes/engine.rb', line 5

def operations
  @operations
end

#paramsObject

Returns the value of attribute params.



5
6
7
# File 'lib/rhoconnect/handler/changes/engine.rb', line 5

def params
  @params
end

#route_handlerObject

Returns the value of attribute route_handler.



5
6
7
# File 'lib/rhoconnect/handler/changes/engine.rb', line 5

def route_handler
  @route_handler
end

#sourceObject

Returns the value of attribute source.



5
6
7
# File 'lib/rhoconnect/handler/changes/engine.rb', line 5

def source
  @source
end

Instance Method Details

#createObject

CUD Operations



74
75
76
# File 'lib/rhoconnect/handler/changes/engine.rb', line 74

def create
  _measure_and_process_cud('create')
end

#deleteObject



82
83
84
# File 'lib/rhoconnect/handler/changes/engine.rb', line 82

def delete
  _measure_and_process_cud('delete')
end

#do_cudObject



25
26
27
28
29
30
31
# File 'lib/rhoconnect/handler/changes/engine.rb', line 25

def do_cud
  if source.cud_queue or source.queue
    async(:cud,source.cud_queue || source.queue)
  else
    run_cud
  end   
end

#do_pass_through_cudObject

Pass through CUD to adapter, no data stored



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rhoconnect/handler/changes/engine.rb', line 34

def do_pass_through_cud
  return if auth_method('login') == false
  res,processed_objects = {},[]
  begin
    operations.each do |op|
      objects = params[op]
      params[:operation] = op if objects
      objects.each do |key,value|
        case op
        when 'create'
          params[:create_object] = value
          @route_handler.call
        when 'update'
          value['id'] = key
          params[:update_object] = value
          @route_handler.call
        when 'delete'
          value['id'] = key
          params[:delete_object] = value
          @route_handler.call
        end
        processed_objects << key
      end if objects
    end
  rescue Exception => e
    log "Error in pass through method: #{e.message}"
    res['error'] = {'message' => e.message } 
  end
  auth_method('logoff')
  res['processed'] = processed_objects
  res.to_json
end

#run_cudObject



67
68
69
70
71
# File 'lib/rhoconnect/handler/changes/engine.rb', line 67

def run_cud
  operations.each do |op|
    send op.to_sym
  end
end

#updateObject



78
79
80
# File 'lib/rhoconnect/handler/changes/engine.rb', line 78

def update
  _measure_and_process_cud('update')
end