This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
domain_model.rb
131 lines (108 loc) · 2.37 KB
/
domain_model.rb
1
2
3
4
5
6
7
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
require 'roar/representer/json/hal'
# TODO API Resoures & Representations (Media Types)
# HTTP Content Type Header field & value representing
# HAL+JSON media type
#
HAL_CONTENT_TYPE_HEADER = {
'Content-Type' => 'application/hal+json'
}
# API root resource
#
class APIRoot
end
# API root resource HAL representation
#
module APIRootHALRepresentation
include Roar::Representer::JSON::HAL
link :self do
"/"
end
link :folders do
"/folders"
end
end
# Folder resource
#
class Folder
attr_accessor :id
attr_accessor :name
attr_accessor :description
attr_accessor :parent
attr_accessor :meta
def initialize (id = "", name = "", description = "")
@id = id
@name = name
@description = description
@parent = ""
@meta = ""
end
def patch(folder)
@name = folder.name unless folder.name.nil?
@description = folder.description unless folder.description.nil?
@parent = folder.parent unless folder.parent.nil?
@meta = folder.meta unless folder.meta.nil?
end
end
# Folder resource HAL representation
#
module FolderHALRepresentation
include Roar::Representer::JSON::HAL
property :id
property :name
property :description
property :parent
property :meta
link :self do
"/folders/#{id}"
end
link :edit do
"/folders/#{id}"
end
end
# Folder resource JSON representation
#
module FolderJSONRepresentation
include Roar::Representer::JSON
property :id
property :name
property :description
property :parent
property :meta
end
# Folders collection resource
#
class Folders
attr_accessor :folders
def initialize(dummy_data_count = 0)
@folders = Array.new
dummy_data_count.times do |i|
folder = Folder.new(i, "Folder ##{i}", "Lorem Ipsum #{i}")
folder.extend(FolderHALRepresentation)
@folders << folder
end
end
def folder_count
@folders.length
end
def folder_with_id(id)
return nil unless id >= 0
folder = @folders.detect { |folder| folder.id == id }
end
end
# Folders collection resource HAL representation
#
module FoldersHALRepresentation
include Roar::Representer::JSON::HAL
property :folder_count
link :self do
"/folders"
end
link :create do
"/folders"
end
collection :folders, extend: FolderHALRepresentation, class: Folder, embedded: true do
property :id
property :name
property :description
end
end