module RestfulObjects::ObjectActions

Public Instance Methods

ro_encode_action_result() click to toggle source
# File lib/restful_objects/domain_model/mixins/object_actions.rb, line 27
def ro_encode_action_result

end
ro_get_action_response(name) click to toggle source
# File lib/restful_objects/domain_model/mixins/object_actions.rb, line 6
def ro_get_action_response(name)
  { 'id' => name,
    'parameters' => generate_parameters(name),
    'links' => [ rs_action_link(name), rs_action_up_link, rs_invoke_link(name) ],
    'extensions' => ro_get_action_type(name).metadata
  }.to_json
end
ro_get_action_type(name) click to toggle source
# File lib/restful_objects/domain_model/mixins/object_actions.rb, line 2
def ro_get_action_type(name)
  ro_domain_type.actions[name]
end
ro_invoke_action_and_get_response(name, json) click to toggle source
# File lib/restful_objects/domain_model/mixins/object_actions.rb, line 31
def ro_invoke_action_and_get_response(name, json)
  raise 'action does not exists' unless ro_get_action_type(name)

  arguments = json == '' ? {} : JSON.parse(json)

  result = send(name, *ro_parse_action_arguments(name, arguments, json))

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

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

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

  response.to_json
end
ro_parse_action_arguments(name, arguments, json) click to toggle source
# File lib/restful_objects/domain_model/mixins/object_actions.rb, line 14
def ro_parse_action_arguments(name, arguments, json)
  result = []
  ro_get_action_type(name).parameters.each do |name, parameter|
    case parameter.type
      when :int
        result << arguments[name.to_s]['value'].to_i
      else
        result << arguments[name.to_s]['value']
    end
  end
  result
end

Protected Instance Methods

generate_parameters(action_name) click to toggle source

def action_return_type(name)

ro_get_action_type(name).result_type

end

# File lib/restful_objects/domain_model/mixins/object_actions.rb, line 89
def generate_parameters(action_name)
  result = {}
  ro_get_action_type(action_name).parameters.each do |name, parameter|
    result[name] = { 'links' => [], 'extensions' => parameter.metadata }
  end
  result
end