reportinator 0.2.0 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +51 -7
- data/Gemfile.lock +10 -0
- data/README.md +113 -22
- data/app/reports/example.report.json +7 -3
- data/app/reports/multiplication.report.json +1 -1
- data/app/reports/multiplication_v2.report.json +15 -0
- data/data/schema/report_schema.json +76 -0
- data/docs/0_first_report.md +8 -8
- data/lib/reportinator/base.rb +2 -1
- data/lib/reportinator/config.rb +10 -0
- data/lib/reportinator/function.rb +5 -5
- data/lib/reportinator/functions/array/flatten.rb +12 -0
- data/lib/reportinator/functions/array/helper.rb +7 -1
- data/lib/reportinator/functions/array/join.rb +1 -1
- data/lib/reportinator/functions/array/range.rb +11 -0
- data/lib/reportinator/functions/array/snippet.rb +30 -0
- data/lib/reportinator/functions/array/string.rb +10 -0
- data/lib/reportinator/functions/string/number.rb +26 -4
- data/lib/reportinator/functions/string/variable.rb +4 -2
- data/lib/reportinator/helpers.rb +29 -0
- data/lib/reportinator/parsers/method.rb +5 -2
- data/lib/reportinator/parsers/value.rb +14 -13
- data/lib/reportinator/report/column.rb +25 -0
- data/lib/reportinator/report/loader.rb +71 -0
- data/lib/reportinator/report/report.rb +33 -0
- data/lib/reportinator/report/row.rb +42 -0
- data/lib/reportinator/report/template.rb +108 -0
- data/lib/reportinator/{report.rb → report_type.rb} +4 -1
- data/lib/reportinator/types/model.rb +10 -5
- data/lib/reportinator/types/preset.rb +23 -2
- data/lib/reportinator/version.rb +1 -1
- data/lib/reportinator.rb +21 -11
- metadata +30 -5
- data/lib/reportinator/loader.rb +0 -120
data/lib/reportinator/loader.rb
DELETED
@@ -1,120 +0,0 @@
|
|
1
|
-
module Reportinator
|
2
|
-
class Loader < Base
|
3
|
-
attribute :type
|
4
|
-
attribute :template
|
5
|
-
attribute :variables
|
6
|
-
attribute :params
|
7
|
-
|
8
|
-
def self.data_from_template(template, additional_params = {})
|
9
|
-
template_data = load_template(template, additional_params)
|
10
|
-
unless template_data.instance_of?(Array)
|
11
|
-
output = split_rows(template_data.data)
|
12
|
-
return ReportParser.parse(output)
|
13
|
-
end
|
14
|
-
output = []
|
15
|
-
template_data.each { |report| output += split_rows(report.data) }
|
16
|
-
ReportParser.parse(output)
|
17
|
-
end
|
18
|
-
|
19
|
-
def self.load_template(template, additional_params = {})
|
20
|
-
data = parse_template(template)
|
21
|
-
return load_multiple(data, additional_params) if data.instance_of?(Array)
|
22
|
-
load_singular(data, additional_params)
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.load_multiple(data, additional_params)
|
26
|
-
data.map { |report| load_singular(report, additional_params) }
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.load_singular(data, additional_params)
|
30
|
-
parent_variables = additional_params[:variables]
|
31
|
-
child_variables = data[:variables]
|
32
|
-
if child_variables.present?
|
33
|
-
data[:variables] = ValueParser.parse(child_variables, parent_variables)
|
34
|
-
end
|
35
|
-
data.merge!(additional_params) { |key, old_value, new_value| merge_values(new_value, old_value) }
|
36
|
-
filtered_data = filter_params(data, attribute_names)
|
37
|
-
variables = filtered_data[:variables]
|
38
|
-
parsed_data = ValueParser.parse(filtered_data, variables)
|
39
|
-
new(parsed_data).report
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.find_template(template)
|
43
|
-
suffixes = config.configured_suffixes
|
44
|
-
directories = config.configured_directories
|
45
|
-
template_files = suffixes.map { |suffix| (suffix.present? ? "#{template}.#{suffix}" : template) }
|
46
|
-
template_paths = directories.map { |dir| template_files.map { |file| "#{dir}/#{file}" } }
|
47
|
-
template_paths.flatten!
|
48
|
-
template_paths.each do |path|
|
49
|
-
return path if File.exist? path
|
50
|
-
end
|
51
|
-
raise "Missing template: #{template}. Searched: #{template_paths}"
|
52
|
-
end
|
53
|
-
|
54
|
-
def self.parse_template(template)
|
55
|
-
file = find_template(template)
|
56
|
-
begin
|
57
|
-
json = File.read(file)
|
58
|
-
JSON.parse(json, symbolize_names: true)
|
59
|
-
rescue
|
60
|
-
raise "Error parsing template file: #{file}"
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def self.split_rows(data)
|
65
|
-
data = data.instance_of?(Array) ? data : [data]
|
66
|
-
rows = []
|
67
|
-
temp = []
|
68
|
-
data.each do |col|
|
69
|
-
if col.instance_of?(Array)
|
70
|
-
rows << temp unless temp.empty?
|
71
|
-
temp = []
|
72
|
-
rows << col
|
73
|
-
else
|
74
|
-
temp << col
|
75
|
-
end
|
76
|
-
end
|
77
|
-
rows << temp unless temp.empty?
|
78
|
-
rows
|
79
|
-
end
|
80
|
-
|
81
|
-
def self.filter_params(params, allowed_params)
|
82
|
-
filtered_params = params.select { |param| allowed_params.include? param.to_s }
|
83
|
-
if params.size > filtered_params.size
|
84
|
-
invalid_params = (params.keys - filtered_params.keys).map { |key| key.to_s }
|
85
|
-
logger.warn "Invalid attributes found: #{invalid_params} Valid attributes are: #{allowed_params}"
|
86
|
-
end
|
87
|
-
filtered_params
|
88
|
-
end
|
89
|
-
|
90
|
-
def self.merge_values(new_value, old_value)
|
91
|
-
return old_value.merge(new_value) if old_value.is_a?(Hash) && new_value.is_a?(Hash)
|
92
|
-
new_value
|
93
|
-
end
|
94
|
-
|
95
|
-
def report
|
96
|
-
if template.present?
|
97
|
-
additional_params = {type: type, variables: variables, params: params}
|
98
|
-
self.class.load_template(template, additional_params.compact)
|
99
|
-
else
|
100
|
-
attribute_list = report_class.attribute_names
|
101
|
-
filtered_params = self.class.filter_params(params, attribute_list)
|
102
|
-
report_class.new(filtered_params)
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
def report_class
|
107
|
-
types = config.configured_types
|
108
|
-
raise "Invalid type: #{type}" unless types.include? type
|
109
|
-
types[type].constantize
|
110
|
-
end
|
111
|
-
|
112
|
-
def template_file
|
113
|
-
self.class.find_template(template)
|
114
|
-
end
|
115
|
-
|
116
|
-
def template_data
|
117
|
-
self.class.parse_template(template)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|