Class: RestfulObjects::DomainType

Inherits:
Object
  • Object
show all
Includes:
LinkGenerator
Defined in:
lib/restful_objects/domain_model/types/domain_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) ⇒ DomainType

Returns a new instance of DomainType.



12
13
14
15
16
17
18
19
20
21
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 12

def initialize(id)
  @id            = id
  @properties    = {}
  @collections   = {}
  @actions       = {}
  @is_service    = false
  @friendly_name = ''
  @plural_name   = ''
  @description   = ''
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



9
10
11
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 9

def actions
  @actions
end

#collectionsObject (readonly)

Returns the value of attribute collections.



9
10
11
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 9

def collections
  @collections
end

#descriptionObject

Returns the value of attribute description.



10
11
12
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 10

def description
  @description
end

#friendly_nameObject

Returns the value of attribute friendly_name.



10
11
12
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 10

def friendly_name
  @friendly_name
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 9

def id
  @id
end

#is_serviceObject (readonly)

Returns the value of attribute is_service.



9
10
11
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 9

def is_service
  @is_service
end

#plural_nameObject

Returns the value of attribute plural_name.



10
11
12
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 10

def plural_name
  @plural_name
end

#propertiesObject (readonly)

Returns the value of attribute properties.



9
10
11
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 9

def properties
  @properties
end

Instance Method Details

#get_representationObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 38

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



87
88
89
90
91
92
93
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 87

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

#new_proto_persistent_objectObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 52

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



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

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

#register_action(name, options = {}) ⇒ Object



33
34
35
36
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 33

def register_action(name, options = {})
  options[:member_order] ||= @actions.count + 1
  @actions[name]           = ActionDescription.new(name, @id, options)
end

#register_collection(name, type, options = {}) ⇒ Object



28
29
30
31
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 28

def register_collection(name, type, options = {})
  options[:member_order] ||= @collections.count + 1
  @collections[name]       = CollectionDescription.new(name, type, @id, options)
end

#register_property(name, return_type, options = {}) ⇒ Object



23
24
25
26
# File 'lib/restful_objects/domain_model/types/domain_type.rb', line 23

def register_property(name, return_type, options = {})
  options[:member_order] ||= @properties.count + 1
  @properties[name]        = PropertyDescription.new(name, @id, return_type, options)
end