/
models.rb
195 lines (141 loc) · 3.06 KB
/
models.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# custom type
class WindowSize
attr_reader :width, :height
def self.to_mongo(value)
value.to_a
end
def self.from_mongo(value)
value.is_a?(self) ? value : WindowSize.new(value)
end
def initialize(*args)
@width, @height = args.flatten
end
def to_a
[width, height]
end
def ==(other)
other.is_a?(self.class) && other.width == width && other.height == height
end
end
class Post
include MongoMapper::Document
key :title, String
key :body, String
has_many :comments, :as => :commentable, :class_name => 'PostComment'
timestamps!
end
class PostComment
include MongoMapper::Document
key :username, String, :default => 'Anonymous'
key :body, String
key :commentable_id, String
key :commentable_type, String
belongs_to :commentable, :polymorphic => true
timestamps!
end
class Address
include MongoMapper::EmbeddedDocument
key :address, String
key :city, String
key :state, String
key :zip, Integer
end
class Message
include MongoMapper::Document
key :body, String
key :position, Integer
key :_type, String
key :room_id, String
belongs_to :room
end
class Answer
include MongoMapper::Document
key :body, String
end
class Enter < Message; end
class Exit < Message; end
class Chat < Message; end
class Room
include MongoMapper::Document
key :name, String
many :messages, :polymorphic => true
end
class Project
include MongoMapper::Document
key :name, String
many :statuses
many :addresses
end
class Status
include MongoMapper::Document
key :project_id, String
key :target_id, String
key :target_type, String
key :name, String
key :position, Integer
belongs_to :project
belongs_to :target, :polymorphic => true
end
class RealPerson
include MongoMapper::Document
many :pets
key :name, String
def realname=(n)
self.name = n
end
end
class Person
include MongoMapper::EmbeddedDocument
key :name, String
key :child, Person
many :pets
end
class Pet
include MongoMapper::EmbeddedDocument
key :name, String
key :species, String
end
class Media
include MongoMapper::EmbeddedDocument
key :_type, String
key :file, String
end
class Video < Media
key :length, Integer
end
class Image < Media
key :width, Integer
key :height, Integer
end
class Music < Media
key :bitrate, String
end
class Catalog
include MongoMapper::Document
many :medias, :polymorphic => true
end
module TrModels
class Transport
include MongoMapper::EmbeddedDocument
key :_type, String
key :license_plate, String
end
class Car < TrModels::Transport
include MongoMapper::EmbeddedDocument
key :model, String
key :year, Integer
end
class Bus < TrModels::Transport
include MongoMapper::EmbeddedDocument
key :max_passengers, Integer
end
class Ambulance < TrModels::Transport
include MongoMapper::EmbeddedDocument
key :icu, Boolean
end
class Fleet
include MongoMapper::Document
many :transports, :polymorphic => true, :class_name => "TrModels::Transport"
key :name, String
end
end