Module: RestfulObjects::ObjectActions

Defined in:
lib/restful_objects/object_actions.rb

Instance Method Summary collapse

Instance Method Details

#action_return_type(action) ⇒ Object



100
101
102
# File 'lib/restful_objects/object_actions.rb', line 100

def action_return_type(action)
  RestfulObjects::DomainModel.current.types[self.class.name].actions[action].result_type
end

#actions_membersObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/restful_objects/object_actions.rb', line 3

def actions_members
  members = {}
  rs_type.actions.each do |name, action|
    members[name] = {
      'memberType' => 'action',
      'links' => [
        !is_service ?
          link_to(:details, "/objects/#{self.class.name}/#{object_id}/actions/#{name}", :object_action, action: name)
          :
          link_to(:details, "/services/#{self.class.name}/actions/#{name}", :object_action, action: name)
      ],
      'extensions' => action.
    }
  end
  members
end

#generate_parameters(action) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/restful_objects/object_actions.rb', line 28

def generate_parameters(action)
  parameters = Hash.new
  rs_type.actions[action].parameters.each do |name, parameter|
    parameters[name] = {
      'links' => [],
      'extensions' => parameter.
    }
  end
  parameters
end

#get_action(action) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/restful_objects/object_actions.rb', line 20

def get_action(action)
  { 'id' => action,
    'parameters' => generate_parameters(action),
    'links' => [ rs_action_link(action), rs_action_up_link, rs_invoke_link(action) ],
    'extensions' => rs_type.actions[action].
  }.to_json
end

#get_action_invoke(action, json) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/restful_objects/object_actions.rb', line 39

def get_action_invoke(action, json)
  raise 'action does not exists' if not rs_type.actions.include?(action)
  action_description = rs_type.actions[action]
  json == '' ? {} : arguments = JSON.parse(json)
  parameters = []
  action_description.parameters.each do |name, parameter|
    case parameter.type
      when :int
        parameters << arguments[name.to_s]['value'].to_i
      else
        parameters << arguments[name.to_s]['value']
    end
  end

  result = send(action.to_sym, *parameters)

  action_link = link_to(:self, "/objects/#{self.class.name}/#{object_id}/actions/#{action}/invoke", :action_result)
  action_link['arguments'] = arguments

  response = {
    'links' => [ action_link ],
    'resultType' => 'scalar',
    'result' => {
      'links' => [],
      'extensions' => { }
    },
    'extensions' => { }
  }

  response['resultType'] = action_description.kind_result_type.to_s
  if result.nil?
    response['result'] = nil
  else
    case action_description.kind_result_type
      when :scalar
        response['resultType'] = 'scalar'
        response['result']['value'] = encode_value(result, action_description.result_type)
        response['result']['links'] = [ link_to(:return_type, '/domain-types/int', :domain_type) ]
      when :object
        response['resultType'] = 'object'
        response['result'] = result.representation
      when :proto_object
        response['resultType'] = 'object'
        response['result'] = result
      when :list
        response['resultType'] = 'list'
        response['result']['links'] =
          [ link_to(:element_type, "/domain-types/#{action_description.result_type.to_s}", :domain_type) ]
        list = []
        result.each do |member|
          member_link = link_to(:element, "/objects/#{action_description.result_type.to_s}/#{member.rs_instance_id}", :object)
          member_link['title'] = member.title
          list << member_link
        end
        response['result']['value'] = list
    end
  end

  response.to_json
end