Module: RestfulObjects::ObjectProperties

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

Constant Summary collapse

HTTP_OK =
200
HTTP_NOT_FOUND =
404

Instance Method Summary collapse

Instance Method Details

#ro_clear_property_and_get_response(name) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/restful_objects/domain_model/mixins/object_properties.rb', line 73

def ro_clear_property_and_get_response(name)
  name = String(name)
  if !(name)
    return [HTTP_NOT_FOUND, { 'Warning' => "No such property #{name}" }, '']
  elsif (name).read_only
    return [HTTP_FORBIDDEN, { 'Warning' => "Read-only property #{name}" }, '']
  end

  send("#{name}=", nil)

  on_after_update if respond_to?(:on_after_update)
  return ro_get_property_response(name)
end

#ro_get_property_metadata(name) ⇒ Object



5
6
7
# File 'lib/restful_objects/domain_model/mixins/object_properties.rb', line 5

def (name)
  ro_domain_type.properties[name]
end

#ro_get_property_response(name) ⇒ Object



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
42
43
44
45
46
47
48
# File 'lib/restful_objects/domain_model/mixins/object_properties.rb', line 9

def ro_get_property_response(name)
  name     = String(name)
  property = ro_domain_type.properties[name]
  return [HTTP_NOT_FOUND, { 'Warning' => "No such property #{name}" }, ''] unless property

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

  unless property.read_only
    representation[name]['links'] << link_to(:modify,
                                             "/objects/#{ro_domain_type.id}/#{ro_instance_id}/properties/#{name}",
                                             :object_property,
                                             { property: name, method: 'PUT', arguments: { 'value' => nil } })
    representation[name]['links'] << link_to(:clear,
                                             "/objects/#{ro_domain_type.id}/#{ro_instance_id}/properties/#{name}",
                                             :object_property,
                                             { property: name, method: 'DELETE'})
    if self.respond_to?("#{name}_choices")
      choices = self.send("#{name}_choices")
      raise "value returned by #{name}_choices method should be an Array" unless choices.is_a?(Array)
      if (name).is_reference
        choices_json = choices.map { |object| object.ro_property_relation_representation(name) }
      else
        choices_json = choices.map { |value| decode_value(value, (name).return_type) }
      end
      representation[name]['choices'] = choices_json
    end
  else
    representation[name]['disabledReason'] = property.disabled_reason
  end

  [HTTP_OK, { 'Content-Type' => ro_content_type_for_property }, representation.to_json]
end

#ro_put_multiple_properties_and_get_response(input) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/restful_objects/domain_model/mixins/object_properties.rb', line 62

def ro_put_multiple_properties_and_get_response(input)
  properties = JSON.parse(input)
  properties.each do |name, value|
    raise 'property not exists' unless ro_domain_type.properties.include?(name)
    raise 'read-only property' if ro_domain_type.properties[name].read_only
    ro_set_property_as_json(name, value['value'])
    on_after_update if respond_to?(:on_after_update)
  end
  [HTTP_OK, { 'Content-Type' => ro_content_type_for_object(ro_domain_type.id) }, ro_get_representation(false).to_json]
end

#ro_put_property_and_get_response(name, input) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/restful_objects/domain_model/mixins/object_properties.rb', line 50

def ro_put_property_and_get_response(name, input)
  name     = String(name)
  property = ro_domain_type.properties[name]
  return [HTTP_NOT_FOUND, { 'Warning' => "No such property #{name}" }, ''] unless property
  return [HTTP_FORBIDDEN, { 'Warning' => "Read-only property #{name}" }, ''] if property.read_only

  ro_set_property_as_json(name, JSON.parse(input)['value'])
  on_after_update if respond_to?(:on_after_update)

  ro_get_property_response(name)
end