Class: Isomorfeus::Data::Handler::Generic

Inherits:
LucidHandler::Base
  • Object
show all
Defined in:
lib/isomorfeus/data/handler/generic.rb

Instance Method Summary collapse

Instance Method Details

#process_create(response_agent, type_class, type_class_name) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/isomorfeus/data/handler/generic.rb', line 37

def process_create(response_agent, type_class, type_class_name)
  # 'Isomorfeus::Data::Handler::Generic', self.name, :create, data_hash
  data = response_agent.request[type_class_name]['create']
  instance_data = data['instance']
  included_items_data = data.key?('included_items') ? data['included_items'] : nil
  if Isomorfeus.current_user.authorized?(type_class, :create, type_class.props_from_data(instance_data))
    instance = type_class.instance_from_transport(instance_data, included_items_data)
    created_type = instance.create
    if created_type
      response_agent.outer_result = {} unless response_agent.outer_result
      response_agent.outer_result.deep_merge!(data: created_type.to_transport)
      if created_type.respond_to?(:included_items_to_transport)
        response_agent.outer_result.deep_merge!(data: created_type.included_items_to_transport)
      end
      response_agent.agent_result = { success: 'ok' }
    else response_agent.error = { error: { type_class_name => 'Create returned nothing!' }}
    end
  else response_agent.error = { error: 'Access denied!' }
  end
end

#process_destroy(response_agent, type_class, type_class_name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/isomorfeus/data/handler/generic.rb', line 117

def process_destroy(response_agent, type_class, type_class_name)
  props = response_agent.request[type_class_name]['destroy']
  props.transform_keys!(&:to_sym)
  if Isomorfeus.current_user.authorized?(type_class, :destroy, props)
    result = type_class.destroy(**props)
    if result
      response_agent.agent_result = { success: 'ok' }
    else
      response_agent.error = { error: { type_class_name => 'Destroy failed!' }}
    end
  else response_agent.error = { error: 'Access denied!' }
  end
end

#process_execute(response_agent, type_class, type_class_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/isomorfeus/data/handler/generic.rb', line 58

def process_execute(response_agent, type_class, type_class_name)
  # 'Isomorfeus::Data::Handler::Generic', self.name, :execute, props_json
  props = response_agent.request[type_class_name]['execute']
  props.transform_keys!(&:to_sym)
  if Isomorfeus.current_user.authorized?(type_class, :execute, props)
    queried_type = type_class.execute(**props)
    if queried_type
      response_agent.outer_result = {} unless response_agent.outer_result
      response_agent.outer_result.deep_merge!(data: queried_type.to_transport)
      if queried_type.respond_to?(:included_items_to_transport)
        response_agent.outer_result.deep_merge!(data: queried_type.included_items_to_transport)
      end
      response_agent.agent_result = { success: 'ok' }
    else response_agent.error = { error: { type_class_name => 'Query returned nothing!' }}
    end
  else response_agent.error = { error: 'Access denied!' }
  end
end

#process_load(response_agent, type_class, type_class_name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/isomorfeus/data/handler/generic.rb', line 77

def process_load(response_agent, type_class, type_class_name)
  # 'Isomorfeus::Data::Handler::Generic', self.name, :load, key: key
  props = response_agent.request[type_class_name]['load']
  props.transform_keys!(&:to_sym)
  if Isomorfeus.current_user.authorized?(type_class, :load, props)
    loaded_type = type_class.load(**props)
    if loaded_type
      response_agent.outer_result = {} unless response_agent.outer_result
      response_agent.outer_result.deep_merge!(data: loaded_type.to_transport)
      if loaded_type.respond_to?(:included_items_to_transport)
        response_agent.outer_result.deep_merge!(data: loaded_type.included_items_to_transport)
      end
      response_agent.agent_result = { success: 'ok' }
    else response_agent.error = { error: { type_class_name => 'Load returned nothing!' }}
    end
  else response_agent.error = { error: 'Access denied!' }
  end
end

#process_request(response_agent) ⇒ Object

responsible for loading: LucidObject LucidFile LucidQuery



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/isomorfeus/data/handler/generic.rb', line 12

def process_request(response_agent)
  # promise_send_path('Isomorfeus::Data::Handler::Generic', self.to_s, action, props_hash)
  response_agent.request.each_key do |type_class_name|
    if Isomorfeus.valid_data_class_name?(type_class_name)
      type_class = Isomorfeus.cached_data_class(type_class_name)
      if type_class
        response_agent.request[type_class_name].each_key do |action|
          case action
          when 'load' then process_load(response_agent, type_class, type_class_name)
          when 'execute' then process_execute(response_agent, type_class, type_class_name)
          when 'create' then process_create(response_agent, type_class, type_class_name)
          when 'save' then process_save(response_agent, type_class, type_class_name)
          when 'destroy' then process_destroy(response_agent, type_class, type_class_name)
          else response_agent.error = { error: { action => 'No such thing!' }}
          end
        end
      else response_agent.error = { error: { type_class_name => 'No such class!' }}
      end
    else response_agent.error = { error: { type_class_name => 'Not a valid LucidData class!' }}
    end
  end
rescue Exception => e
  response_agent.error = { error: "Isomorfeus::Data::Handler::Generic: #{e.message}\n#{e.backtrace.join("\n")}" }
end

#process_save(response_agent, type_class, type_class_name) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/isomorfeus/data/handler/generic.rb', line 96

def process_save(response_agent, type_class, type_class_name)
  # 'Isomorfeus::Data::Handler::Generic', self.name, :save, data_hash
  data = response_agent.request[type_class_name]['save']
  instance_data = data['instance']
  included_items_data = data.key?('included_items') ? data['included_items'] : nil
  if Isomorfeus.current_user.authorized?(type_class, :save, type_class.props_from_data(instance_data))
    instance = type_class.instance_from_transport(instance_data, included_items_data)
    saved_type = instance.save
    if saved_type
      response_agent.outer_result = {} unless response_agent.outer_result
      response_agent.outer_result.deep_merge!(data: saved_type.to_transport)
      if saved_type.respond_to?(:included_items_to_transport)
        response_agent.outer_result.deep_merge!(data: saved_type.included_items_to_transport)
      end
      response_agent.agent_result = { success: 'ok' }
    else response_agent.error = { error: { type_class_name => 'Save returned nothing!' }}
    end
  else response_agent.error = { error: 'Access denied!' }
  end
end