Module: RestfulObjects::ObjectCollections

Defined in:
lib/restful_objects/domain_model/mixins/object_collections.rb

Constant Summary collapse

HTTP_OK =
200

Instance Method Summary collapse

Instance Method Details

#ro_add_to_collection_and_get_response(name, json) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/restful_objects/domain_model/mixins/object_collections.rb', line 43

def ro_add_to_collection_and_get_response(name, json)
  raise "collection not exists" unless ro_get_collection_type(name)
  href_value = JSON.parse(json)['value']['href']
  match = Regexp.new(".*/objects/(?<domain-type>\\w*)/(?<object-id>\\d*)").match(href_value)
  raise "Invalid request format" if not match
  domain_type = match['domain-type']
  id = match['object-id'].to_i
  raise "Value does not exists" unless ro_domain_model.objects.include?(id)
  raise "Domain-type does not exists" unless ro_domain_model.types.include?(domain_type)

  send(name).push(ro_domain_model.objects[id])

  return ro_get_collection_response(name)
end

#ro_delete_from_collection_and_get_response(name, json) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/restful_objects/domain_model/mixins/object_collections.rb', line 58

def ro_delete_from_collection_and_get_response(name, json)
  raise "collection not exists" unless ro_get_collection_type(name)
  href_value = JSON.parse(json)['value']['href']
  match = Regexp.new(".*/objects/(?<domain-type>\\w*)/(?<object-id>\\d*)").match(href_value)
  raise "Invalid request format" if not match
  domain_type = match['domain-type']
  id = match['object-id'].to_i
  raise "Value does not exists" unless ro_domain_model.objects.include?(id)
  raise "Domain-type does not exists" unless ro_domain_model.types.include?(domain_type)

  send(name).delete(ro_domain_model.objects[id])

  return ro_get_collection_response(name)
end

#ro_get_collection_response(name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/restful_objects/domain_model/mixins/object_collections.rb', line 8

def ro_get_collection_response(name)
  raise "collection not exists" unless ro_get_collection_type(name)

  value = []
  send(name).each do |object|
    link = link_to(:value, "/objects/#{object.ro_domain_type.id}/#{object.ro_instance_id}", :object, method: 'GET', collection: name)
    link['title'] = object.ro_title
    value << link
  end

  representation = {
    'id' => name,
    'value' => value,
    'links' => [
        link_to(:self, "/objects/#{ro_domain_type.id}/#{ro_instance_id}/collections/#{name}", :object_collection),
        link_to(:up, "/objects/#{ro_domain_type.id}/#{ro_instance_id}", :object)
      ],
    'extensions' => ro_get_collection_type(name).
  }

  unless ro_get_collection_type(name).read_only
    add_to_link = link_to(:add_to, "/objects/#{ro_domain_type.id}/#{ro_instance_id}/collections/#{name}",
                          :object_collection, method: 'PUT', collection: name)
    add_to_link['arguments'] = { 'value' => nil }
    remove_from_link = link_to(:remove_from, "/objects/#{ro_domain_type.id}/#{ro_instance_id}/collections/#{name}",
                               :object_collection, method: 'DELETE', collection: name)
    remove_from_link['arguments'] = { 'value' => nil }
    representation['links'].concat [ add_to_link, remove_from_link ]
  else
    representation['disabledReason'] = ro_get_collection_type(name).disabled_reason
  end

  [HTTP_OK, { 'Content-Type' => ro_content_type_for_object_collection(ro_get_collection_type(name).type) }, representation.to_json]
end

#ro_get_collection_type(name) ⇒ Object



4
5
6
# File 'lib/restful_objects/domain_model/mixins/object_collections.rb', line 4

def ro_get_collection_type(name)
  ro_domain_type.collections[name]
end