Class: RestfulObjects::Type

Inherits:
Object
  • Object
show all
Includes:
LinkGenerator
Defined in:
lib/restful_objects/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from LinkGenerator

#generate_rel, #generate_repr_type, #link_to, #underscore_to_hyphen_string

Constructor Details

#initialize(id) ⇒ Type

Returns a new instance of Type.



8
9
10
11
12
13
14
15
16
17
# File 'lib/restful_objects/type.rb', line 8

def initialize(id)
  @id = id
  @properties = PropertyList.new(id)
  @collections = CollectionList.new(id)
  @actions = ActionList.new(id)
  @is_service = false
  @friendly_name = ''
  @plural_name = ''
  @description = ''
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



5
6
7
# File 'lib/restful_objects/type.rb', line 5

def actions
  @actions
end

#collectionsObject (readonly)

Returns the value of attribute collections.



5
6
7
# File 'lib/restful_objects/type.rb', line 5

def collections
  @collections
end

#descriptionObject

Returns the value of attribute description.



6
7
8
# File 'lib/restful_objects/type.rb', line 6

def description
  @description
end

#friendly_nameObject

Returns the value of attribute friendly_name.



6
7
8
# File 'lib/restful_objects/type.rb', line 6

def friendly_name
  @friendly_name
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/restful_objects/type.rb', line 5

def id
  @id
end

#is_serviceObject (readonly)

Returns the value of attribute is_service.



5
6
7
# File 'lib/restful_objects/type.rb', line 5

def is_service
  @is_service
end

#plural_nameObject

Returns the value of attribute plural_name.



6
7
8
# File 'lib/restful_objects/type.rb', line 6

def plural_name
  @plural_name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



5
6
7
# File 'lib/restful_objects/type.rb', line 5

def properties
  @properties
end

Instance Method Details

#get_representationObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/restful_objects/type.rb', line 19

def get_representation
  { 'name' => @id,
    'domainType' => @id,
    'friendlyName' => @friendly_name,
    'pluralName' => @plural_name,
    'description' => @description,
    'isService' => @is_service,
    'members' => get_members,
    'typeActions' => get_type_actions,
    'links' => [ link_to(:self, "/domain-types/#{@id}", :domain_type) ],
    'extensions' => {}
  }.to_json
end

#metadataObject



68
69
70
71
72
73
74
# File 'lib/restful_objects/type.rb', line 68

def 
  { 'domainType' => id,
    'friendlyName' => friendly_name,
    'pluralName' => plural_name,
    'description' => description,
    'isService' => is_service }
end

#new_proto_persistent_objectObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/restful_objects/type.rb', line 33

def new_proto_persistent_object
  persist_link = link_to(:persist, "/objects/#{id}", :object, method: 'POST')
  persist_link['arguments'] = { 'members' => {} }
  members = {}
  properties.each do |name, property|
    if not property.optional
      persist_link['arguments']['members'][name] = {
        'value' => nil,
        'extensions' => property. }
    end
    members[name] = { 'value' => nil }
  end

  { 'title' => "New #{id}",
    'members' => members,
    'links' => [ persist_link ],
    'extensions' => {} }
end

#post_prototype_object(members_json) ⇒ Object



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

def post_prototype_object(members_json)
  members = JSON.parse(members_json)['members']

  new_object = Object.const_get(@id.to_sym).new

  members.each do |name, value|
    if properties.include?(name) then
      new_object.put_property_as_json(name, value.to_json)
    else
      raise "member of property '#{name}' not found in type '#{@id}'"
    end
  end

  new_object.get_representation
end