This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
fields.cr
204 lines (180 loc) · 6.42 KB
/
fields.cr
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
196
197
198
199
200
201
202
203
204
require "json"
module Mongo::ORM::Fields
alias Type = JSON::Any | DB::Any
TIME_FORMAT_REGEX = /\d{4,}-\d{2,}-\d{2,}\s\d{2,}:\d{2,}:\d{2,}/
macro included
macro inherited
FIELDS = {} of Nil => Nil
SPECIAL_FIELDS = {} of Nil => Nil
end
end
# specify the fields you want to define and types
macro field(decl)
{% FIELDS[decl.var] = decl.type %}
end
macro embeds(decl)
{% FIELDS[decl.var] = decl.type %}
raise "can only embed classes inheriting from Mongo::ORM::EmbeddedDocument" unless {{decl.type}}.new.is_a?(Mongo::ORM::EmbeddedDocument)
end
macro embeds_many(children_collection, class_name=nil)
{% children_class = class_name ? class_name.id : children_collection.id[0...-1].camelcase %}
{% children_array_class = "Array(#{children_class})" %}
@{{children_collection.id}} = [] of {{children_class}}
def {{children_collection.id}}
@{{children_collection.id}}
end
def {{children_collection.id}}=(value : Array({{children_class}}))
@{{children_collection.id}} = value
end
{% SPECIAL_FIELDS[children_collection.id] = children_class %}
end
# include created_at and updated_at that will automatically be updated
macro timestamps
{% SETTINGS[:timestamps] = true %}
end
macro __process_fields
# Create the properties
{% for name, type in FIELDS %}
property {{name.id}} : Union({{type.id}} | Nil)
{% end %}
{% if SETTINGS[:timestamps] %}
property created_at : Time?
property updated_at : Time?
{% end %}
# keep a hash of the fields to be used for mapping
def self.fields(fields = [] of String)
{% for name, type in FIELDS %}
fields << "{{name.id}}"
{% end %}
{% if SETTINGS[:timestamps] %}
fields << "created_at"
fields << "updated_at"
{% end %}
return fields
end
# keep a hash of the fields to be used for mapping
def fields(fields = {} of String => Type | Nil)
{% for name, type in FIELDS %}
fields["{{name.id}}"] = self.{{name.id}}
{% end %}
{% if SETTINGS[:timestamps] %}
fields["created_at"] = self.created_at
fields["updated_at"] = self.updated_at
{% end %}
fields["_id"] = self._id
return fields
end
# keep a hash of the params that will be passed to the adapter.
def params
parsed_params = [] of DB::Any
{% for name, type in FIELDS %}
{% if type.id == Time.id %}
parsed_params << {{name.id}}.try(&.to_s("%F %X"))
{% else %}
parsed_params << {{name.id}}
{% end %}
{% end %}
{% if SETTINGS[:timestamps] %}
parsed_params << created_at.not_nil!.to_s("%F %X")
parsed_params << updated_at.not_nil!.to_s("%F %X")
{% end %}
return parsed_params
end
def to_h
fields = {} of String => DB::Any
fields["{{PRIMARY[:name]}}"] = {{PRIMARY[:name]}}
{% for name, type in FIELDS %}
{% if type.id == Time.id %}
fields["{{name}}"] = {{name.id}}.try(&.to_s("%F %X"))
{% elsif type.id == Slice.id %}
fields["{{name}}"] = {{name.id}}.try(&.to_s(""))
{% else %}
fields["{{name}}"] = {{name.id}}
{% end %}
{% end %}
{% if SETTINGS[:timestamps] %}
fields["created_at"] = created_at.try(&.to_s("%F %X"))
fields["updated_at"] = updated_at.try(&.to_s("%F %X"))
{% end %}
return fields
end
def to_json(json : JSON::Builder)
json.object do
json.field "{{PRIMARY[:name]}}", {{PRIMARY[:name]}} ? {{PRIMARY[:name]}}.to_s : nil
{% for name, type in FIELDS %}
%field, %value = "{{name.id}}", {{name.id}}
{% if type.id == Time.id %}
json.field %field, %value.to_s
{% elsif type.id == Slice.id %}
json.field %field, %value.id.try(&.to_s(""))
{% else %}
json.field %field, %value
{% end %}
{% end %}
{% if SETTINGS[:timestamps] %}
json.field "created_at", created_at.to_s
json.field "updated_at", updated_at.to_s
{% end %}
end
end
def to_json
JSON.build do |json|
json.object do
json.field "{{PRIMARY[:name]}}", {{PRIMARY[:name]}} ? {{PRIMARY[:name]}}.to_s : nil
{% for name, type in FIELDS %}
%field, %value = "{{name.id}}", {{name.id}}
{% if type.id == Time.id %}
json.field %field, %value.to_s
{% elsif type.id == Slice.id %}
json.field %field, %value.id.try(&.to_s(""))
{% else %}
json.field %field, %value
{% end %}
{% end %}
{% if SETTINGS[:timestamps] %}
json.field "created_at", created_at.to_s
json.field "updated_at", updated_at.to_s
{% end %}
end
end
end
def set_attributes(args : Hash(String | Symbol, Type))
args.each do |k, v|
cast_to_field(k, v.as(Type))
end
end
def set_attributes(**args)
set_attributes(args.to_h)
end
# Casts params and sets fields
private def cast_to_field(name, value : Type)
case name.to_s
{% for _name, type in FIELDS %}
when "{{_name.id}}"
return @{{_name.id}} = nil if value.nil?
{% if type.id == BSON::ObjectId.id %}
@{{_name.id}} = BSON::ObjectId.new value.to_s
{% elsif type.id == Int32.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_i32 : value.is_a?(Int64) ? value.to_s.to_i32 : value.as(Int32)
{% elsif type.id == Int64.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_i64 : value.as(Int64)
{% elsif type.id == Float32.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_f32 : value.is_a?(Float64) ? value.to_s.to_f32 : value.as(Float32)
{% elsif type.id == Float64.id %}
@{{_name.id}} = value.is_a?(String) ? value.to_f64 : value.as(Float64)
{% elsif type.id == Bool.id %}
@{{_name.id}} = ["1", "yes", "true", true].includes?(value)
{% elsif type.id == Time.id %}
if value.is_a?(Time)
@{{_name.id}} = value.to_utc
elsif value.to_s =~ TIME_FORMAT_REGEX
@{{_name.id}} = Time.parse(value.to_s, "%F %X").to_utc
end
{% else %}
@{{_name.id}} = value.to_s
{% end %}
{% end %}
end
end
end
end