reportinator 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Reportinator
2
- class MethodParser < Base
2
+ class MethodParser < Parser
3
3
  attribute :target
4
4
  attribute :method
5
5
 
@@ -8,7 +8,10 @@ module Reportinator
8
8
  end
9
9
 
10
10
  def output
11
- return send_value(target, method) if method_class == Symbol
11
+ if method_class == Symbol
12
+ value = send_value(target, method)
13
+ return escape_value(value)
14
+ end
12
15
  return parse_array_method if method_class == Array
13
16
  return parse_hash_method if method_class == Hash
14
17
  nil
@@ -24,7 +27,6 @@ module Reportinator
24
27
  output = target
25
28
  method.each do |m|
26
29
  value = parse_method(output, m)
27
- next unless value.present?
28
30
  valid = true
29
31
  output = value
30
32
  end
@@ -0,0 +1,47 @@
1
+ module Reportinator
2
+ class ReportParser < Parser
3
+ attribute :element
4
+ attribute :data
5
+
6
+ def self.parse(element, data = nil)
7
+ set_data = (data.present? ? data : element)
8
+ new(element: element, data: set_data).output
9
+ end
10
+
11
+ def output
12
+ return parse_array if element_class == Array
13
+ return parse_hash if element_class == Hash
14
+ return parse_string if element_class == String
15
+ element
16
+ end
17
+
18
+ def parse_array
19
+ raise "Not an array" unless element_class == Array
20
+ element.map { |value| parse_value(value) }
21
+ end
22
+
23
+ def parse_hash
24
+ raise "Not a hash" unless element_class == Hash
25
+ element.transform_values { |value| parse_value(value) }
26
+ end
27
+
28
+ def parse_string
29
+ raise "Not a string" unless element_class == String
30
+ return element unless element.strip.start_with?("?")
31
+ return element.sub("?/", "") if element.strip.start_with?("?/")
32
+ # return parse_row_total if element.start_with?("?tr")
33
+ # return parse_column_total if element.start_with?("?tc")
34
+ element
35
+ end
36
+
37
+ def element_class
38
+ element.class
39
+ end
40
+
41
+ private
42
+
43
+ def parse_value(value)
44
+ self.class.parse(value, data)
45
+ end
46
+ end
47
+ end
@@ -1,7 +1,5 @@
1
1
  module Reportinator
2
- class ValueParser < Base
3
- VALUE_FUNCTIONS = %i[a d n rn rd r]
4
-
2
+ class ValueParser < Parser
5
3
  attribute :element
6
4
  attribute :variables, default: {}
7
5
 
@@ -13,132 +11,37 @@ module Reportinator
13
11
  def self.parse_and_execute(target, values, variables = {})
14
12
  parsed_target = target
15
13
  if target.instance_of?(String)
16
- parsed_target = new(element: target, variables: variables).parse_string
14
+ parsed_target = parse(target, variables)
17
15
  end
18
16
  parsed_values = parse(values, variables)
19
17
  MethodParser.parse(parsed_target, parsed_values)
20
18
  end
21
19
 
22
20
  def output
21
+ config.configured_functions.each do |function|
22
+ return function.parse(element, variables) if function.accepts? element
23
+ end
23
24
  return parse_array if element_class == Array
24
25
  return parse_hash if element_class == Hash
25
- return parse_string if element_class == String
26
26
  element
27
27
  end
28
28
 
29
29
  def parse_array
30
30
  raise "Not an array" unless element_class == Array
31
- front = element[0]
32
- return parse_executed_array if front.instance_of?(String) && front.start_with?("#")
33
31
  element.map { |value| parse_value(value) }
34
32
  end
35
33
 
36
- def parse_executed_array
37
- raise "Not an executable array" unless element[0].start_with?("#")
38
- values = element
39
- target = values.delete_at(0).sub("#", "")
40
- parse_and_execute_value(target, values)
41
- end
42
-
43
34
  def parse_hash
44
35
  raise "Not a hash" unless element_class == Hash
45
36
  element.transform_values { |value| parse_value(value) }
46
37
  end
47
38
 
48
- def parse_string
49
- raise "Not a string" unless element_class == String
50
- return element.sub(":", "").to_sym if element.start_with?(":")
51
- return element.sub("&", "").constantize if element.start_with?("&")
52
- return parse_variable(element) if element.start_with?("$")
53
- return parse_function(element) if element.start_with?("!")
54
- element
55
- end
56
-
57
39
  def element_class
58
40
  element.class
59
41
  end
60
42
 
61
- private
62
-
63
- def parse_variable(value)
64
- key = value.sub("$", "").to_sym
65
- variables[key]
66
- end
67
-
68
- def parse_function(value)
69
- input = value.strip
70
- function = function_type(input)
71
- return value unless function.present?
72
- input.sub!(function_prefix(function), "")
73
- output = run_function(function, input)
74
- output.nil? ? value : output
75
- end
76
-
77
- def run_function(function, input)
78
- case function
79
- when :a then addition_function(input)
80
- when :d then date_function(input)
81
- when :n then number_function(input)
82
- when :r then range_function(input)
83
- when :rn then range_function(input, :number)
84
- when :rd then range_function(input, :date)
85
- end
86
- end
87
-
88
- def function_type(value)
89
- VALUE_FUNCTIONS.each do |function|
90
- return function if value.start_with?(function_prefix(function))
91
- end
92
- false
93
- end
94
-
95
- def function_prefix(function)
96
- "!#{function}"
97
- end
98
-
99
- def addition_function(value)
100
- values = parse_function_array(value)
101
- values.map! { |value| number_function(value) }
102
- values.sum(0)
103
- rescue
104
- 0
105
- end
106
-
107
- def date_function(value)
108
- Time.parse(value)
109
- rescue
110
- Time.now
111
- end
112
-
113
- def number_function(value)
114
- float = (value =~ /\d\.\d/)
115
- return value.to_f if float.present?
116
- value.to_i
117
- rescue
118
- 0
119
- end
120
-
121
- def range_function(value, type = :any)
122
- values = parse_function_array(value)
123
- case type
124
- when :number then values.map! { |subvalue| number_function(subvalue) }
125
- when :date then values.map! { |subvalue| date_function(subvalue) }
126
- end
127
- Range.new(*values)
128
- rescue
129
- Range(0..1)
130
- end
131
-
132
- def parse_function_array(value)
133
- value.split(",").map { |value| parse_value(value.strip) }
134
- end
135
-
136
43
  def parse_value(value)
137
44
  self.class.parse(value, variables)
138
45
  end
139
-
140
- def parse_and_execute_value(target, value)
141
- self.class.parse_and_execute(target, value, variables)
142
- end
143
46
  end
144
47
  end
@@ -6,15 +6,13 @@ module Reportinator
6
6
  validates :target, presence: true
7
7
 
8
8
  def data
9
- return get_model_data(target) unless target.methods.include? :first
9
+ return get_model_data(target) unless target.respond_to? :to_ary
10
10
  records_data
11
11
  end
12
12
 
13
13
  def records_data
14
- records = target
15
- records = target.all if target.methods.include? :all
16
14
  output = []
17
- records.each do |model|
15
+ target.each do |model|
18
16
  value = get_model_data(model)
19
17
  output << value
20
18
  end
@@ -23,8 +21,13 @@ module Reportinator
23
21
 
24
22
  def get_model_data(target)
25
23
  method_list.map do |method|
26
- ValueParser.parse_and_execute(target, method)
24
+ parse_method(target, method)
27
25
  end
28
26
  end
27
+
28
+ def parse_method(target, method)
29
+ return method if method.instance_of?(String)
30
+ MethodParser.parse(target, method)
31
+ end
29
32
  end
30
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Reportinator
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/reportinator.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "reportinator/version"
4
3
  require "csv"
5
4
  require "json"
6
5
  require "fileutils"
7
6
  require "active_support"
8
7
  require "active_model"
9
8
  require "require_all"
9
+ require_relative "reportinator/version"
10
+ require_relative "reportinator/base"
10
11
 
11
12
  module Reportinator
12
13
  class Error < StandardError; end
@@ -27,6 +28,10 @@ module Reportinator
27
28
  @logger || ActiveSupport::Logger.new($stdout)
28
29
  end
29
30
 
31
+ def self.parse(input, variables = {})
32
+ ValueParser.parse(input, variables)
33
+ end
34
+
30
35
  def self.report(template, additional_params = {})
31
36
  Loader.data_from_template(template, additional_params)
32
37
  end
@@ -43,27 +48,4 @@ module Reportinator
43
48
  end
44
49
  path
45
50
  end
46
-
47
- class Base
48
- include ActiveModel::API
49
- include ActiveModel::Attributes
50
-
51
- require_all "lib/reportinator"
52
-
53
- def self.config
54
- Reportinator.config
55
- end
56
-
57
- def config
58
- self.class.config
59
- end
60
-
61
- def self.logger
62
- Reportinator.logger
63
- end
64
-
65
- def logger
66
- self.class.logger
67
- end
68
- end
69
51
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reportinator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moxvallix
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-06 00:00:00.000000000 Z
11
+ date: 2022-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -56,10 +56,29 @@ files:
56
56
  - Rakefile
57
57
  - app/reports/example.report.json
58
58
  - app/reports/multiplication.report.json
59
+ - docs/0_first_report.md
59
60
  - lib/reportinator.rb
61
+ - lib/reportinator/base.rb
60
62
  - lib/reportinator/config.rb
63
+ - lib/reportinator/function.rb
64
+ - lib/reportinator/functions/array.rb
65
+ - lib/reportinator/functions/array/helper.rb
66
+ - lib/reportinator/functions/array/join.rb
67
+ - lib/reportinator/functions/array/method.rb
68
+ - lib/reportinator/functions/string.rb
69
+ - lib/reportinator/functions/string/addition.rb
70
+ - lib/reportinator/functions/string/constant.rb
71
+ - lib/reportinator/functions/string/date.rb
72
+ - lib/reportinator/functions/string/join.rb
73
+ - lib/reportinator/functions/string/logical.rb
74
+ - lib/reportinator/functions/string/number.rb
75
+ - lib/reportinator/functions/string/range.rb
76
+ - lib/reportinator/functions/string/symbol.rb
77
+ - lib/reportinator/functions/string/variable.rb
61
78
  - lib/reportinator/loader.rb
79
+ - lib/reportinator/parser.rb
62
80
  - lib/reportinator/parsers/method.rb
81
+ - lib/reportinator/parsers/report.rb
63
82
  - lib/reportinator/parsers/value.rb
64
83
  - lib/reportinator/report.rb
65
84
  - lib/reportinator/types/model.rb