Module: RestfulObjects::ObjectCollections

Defined in:
lib/restful_objects/object_collections.rb

Instance Method Summary collapse

Instance Method Details

#add_to_collection(collection, json) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/restful_objects/object_collections.rb', line 54

def add_to_collection(collection, json)
  raise "collection not exists" if not rs_model.types[self.class.name].collections.include?(collection)
  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" if not rs_model.objects.include?(id)
  raise "Domain-type does not exists" if not rs_model.types.include?(domain_type)

  send(collection.to_sym).push(rs_model.objects[id])

  get_collection_as_json(collection)
end

#collections_membersObject



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

def collections_members
  members = {}
  rs_type.collections.each do |name, collection|
    members[name] = {
      'memberType' => 'collection',
      'size' => rs_type.collections.count,
      'links' => [
        link_to(:details, "/objects/#{self.class.name}/#{object_id}/collections/#{name}", :object_collection, collection: name)
      ],
      'extensions' => collection.
    }
  end
  members
end

#delete_from_collection(collection, json) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/restful_objects/object_collections.rb', line 69

def delete_from_collection(collection, json)
  raise "collection not exists" if not rs_model.types[self.class.name].collections.include?(collection)
  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" if not rs_model.objects.include?(id)
  raise "Domain-type does not exists" if not rs_model.types.include?(domain_type)

  send(collection.to_sym).delete(rs_model.objects[id])

  get_collection_as_json(collection)
end

#get_collection_as_json(collection) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/restful_objects/object_collections.rb', line 18

def get_collection_as_json(collection)
  raise "collection not exists" if not rs_model.types[self.class.name].collections.include?(collection)

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

  representation = {
    'id' => collection,
    'value' => value,
    'links' => [
        link_to(:self, "/objects/#{rs_type.id}/#{rs_instance_id}/collections/#{collection}", :object_collection),
        link_to(:up, "/objects/#{rs_type.id}/#{rs_instance_id}", :object)
      ],
    'extensions' => rs_type.collections[collection].
  }

  if not rs_model.types[self.class.name].collections[collection].read_only then
    add_to_link = link_to(:add_to, "/objects/#{rs_type.id}/#{rs_instance_id}/collections/#{collection}",
                          :object_collection, method: 'PUT', collection: collection)
    add_to_link['arguments'] = { 'value' => nil }
    remove_from_link = link_to(:remove_from, "/objects/#{rs_type.id}/#{rs_instance_id}/collections/#{collection}",
                               :object_collection, method: 'DELETE', collection: collection)
    remove_from_link['arguments'] = { 'value' => nil }
    representation['links'].concat [ add_to_link, remove_from_link ]
  else
    representation['disabledReason'] =
      rs_model.types[self.class.name].collections[collection].disabled_reason
  end

  representation.to_json
end