petit-felix 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,213 @@
1
+ ## validation stuff for TemplatePDFWriter
2
+
3
+ module PetitFelix
4
+ module Worker
5
+
6
+ class TemplatePDFWriter < PetitFelix::Worker::DefaultPDFWriter
7
+
8
+ SYMBOLIZE = [
9
+ :align,
10
+ :odd_align,
11
+ :even_align,
12
+ :valign,
13
+ :odd_valign,
14
+ :even_valign,
15
+ :direction,
16
+ :mode,
17
+ :style,
18
+ :overflow,
19
+ :rotate_around
20
+ ]
21
+
22
+ ## variable validation methods
23
+
24
+ def args_has_string arg_name, args
25
+
26
+ if !args.key? arg_name
27
+ # text not defined
28
+ @error_param["arg"] = arg_name.to_s
29
+ return 7
30
+ end
31
+
32
+ args[arg_name] = replace_variable args[arg_name].to_s
33
+
34
+ return 0
35
+
36
+ end
37
+
38
+ def args_has_int arg_name, args
39
+
40
+ if !args.key? arg_name
41
+
42
+ # text not defined
43
+ @error_param["arg"] = arg_name.to_s
44
+ return 7
45
+
46
+ end
47
+
48
+ if args[arg_name].instance_of? String
49
+
50
+ begin
51
+
52
+ args[arg_name] = Eqn::Calculator.calc(replace_variable args[arg_name]).to_i
53
+ rescue
54
+
55
+ @error_param["arg"] = replace_variable args[arg_name]
56
+ return 9
57
+ end
58
+ end
59
+
60
+ return 0
61
+ end
62
+
63
+ def args_has_float arg_name, args
64
+
65
+ if !args.key? arg_name
66
+ # text not defined
67
+ @error_param["arg"] = arg_name.to_s
68
+ return 7
69
+
70
+ end
71
+
72
+ begin
73
+
74
+ args[arg_name] = Eqn::Calculator.calc(replace_variable args[arg_name]).to_f
75
+
76
+ rescue
77
+
78
+ @error_param["arg"] = replace_variable args[arg_name]
79
+ return 9
80
+
81
+ end
82
+
83
+ return 0
84
+
85
+ end
86
+
87
+ def args_has_arr arg_name, args, type, options = {}
88
+
89
+ if !args.key? arg_name
90
+ # text not defined
91
+ @error_param["arg"] = arg_name.to_s
92
+ return 7
93
+
94
+ end
95
+
96
+ if args[arg_name].instance_of? String
97
+
98
+ begin
99
+
100
+ set_variables
101
+
102
+ test = replace_variable args[arg_name]
103
+
104
+ args[arg_name] = JSON.parse(test)
105
+
106
+ rescue => error
107
+
108
+ print "\nError parsing array: #{args[arg_name]}\n"
109
+ print error
110
+
111
+ end
112
+
113
+ end
114
+
115
+ if args[arg_name].instance_of? Array
116
+
117
+ if type == :float
118
+
119
+ args[arg_name].map! {|item| Eqn::Calculator.calc(replace_variable item.to_s).to_f }
120
+ elsif type == :int
121
+
122
+ args[arg_name].map! {|item| Eqn::Calculator.calc(replace_variable item.to_s).to_i }
123
+ elsif type == :hash
124
+
125
+ args[arg_name].map! {|item| args_correct_hash item, options[:second_type] }
126
+ else
127
+
128
+ args[arg_name].map! {|item| replace_variable item.to_s }
129
+ end
130
+
131
+ end
132
+
133
+ return 0
134
+
135
+ end
136
+
137
+ def args_correct_hash hash, type
138
+
139
+ hash.transform_keys!(&:to_sym)
140
+
141
+ hash.keys.each do |key|
142
+
143
+ if type == :int
144
+
145
+ begin
146
+
147
+ hash[key] = Eqn::Calculator.calc(replace_variable hash[key].to_s).to_i
148
+
149
+ rescue
150
+
151
+ @error_param["arg"] = replace_variable hash[key]
152
+ return 9
153
+
154
+ end
155
+
156
+ elsif type == :float
157
+
158
+ begin
159
+
160
+ hash[key] = Eqn::Calculator.calc(replace_variable hash[key].to_s).to_f
161
+ rescue
162
+
163
+ @error_param["arg"] = replace_variable hash[key]
164
+ return 9
165
+
166
+ end
167
+
168
+ else
169
+
170
+ hash[key] = replace_variable hash[key].to_s
171
+ end
172
+
173
+ end
174
+
175
+ hash
176
+
177
+ end
178
+
179
+ def args_correct_values args
180
+
181
+ args_has_int :width, args
182
+ args_has_int :height, args
183
+
184
+ SYMBOLIZE.each do |symbol|
185
+
186
+ if args.key? symbol
187
+
188
+ args[symbol] = args[symbol].to_sym
189
+
190
+ end
191
+ end
192
+
193
+ if args.key? :position
194
+
195
+ if ["left","center","right"].include? args[:position]
196
+
197
+ args[:position] = args[:position].to_sym
198
+
199
+ else
200
+
201
+ args[:position] = args[:position].to_i
202
+
203
+ end
204
+
205
+ end
206
+
207
+ args
208
+
209
+ end
210
+
211
+ end
212
+ end
213
+ end