forked from hudmol/container_management_dartmouth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
managed_container_init.rb
63 lines (46 loc) · 1.7 KB
/
managed_container_init.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
require_relative 'lib/barcode_check'
module JSONModel
module Validations
self.singleton_class.send(:alias_method, :check_instance_pre_managed_container, :check_instance)
def self.check_instance(hash)
if hash['sub_container']
[]
else
check_instance_pre_managed_container(hash)
end
end
def self.check_sub_container(hash)
errors = []
if (!hash["type_2"].nil? && hash["indicator_2"].nil?) || (hash["type_2"].nil? && !hash["indicator_2"].nil?)
errors << ["type_2", "container 2 requires both a type and indicator"]
end
if (hash["type_2"].nil? && hash["indicator_2"].nil? && (!hash["type_3"].nil? || !hash["indicator_3"].nil?))
errors << ["type_2", "container 2 is required if container 3 is provided"]
end
if (!hash["type_3"].nil? && hash["indicator_3"].nil?) || (hash["type_3"].nil? && !hash["indicator_3"].nil?)
errors << ["type_3", "container 3 requires both a type and indicator"]
end
errors
end
if JSONModel(:sub_container)
JSONModel(:sub_container).add_validation("check_sub_container") do |hash|
check_sub_container(hash)
end
end
def self.check_container_profile(hash)
errors = []
# Ensure depth, width and height have no more than 2 decimal places
["depth", "width", "height"].each do |k|
if hash[k] !~ /\A\d+(\.\d\d?)?\Z/
errors << [k, "must be a number with no more than 2 decimal places"]
end
end
errors
end
if JSONModel(:container_profile)
JSONModel(:container_profile).add_validation("check_container_profile") do |hash|
check_container_profile(hash)
end
end
end
end