report_logic 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -1
- data/README.md +43 -101
- data/lib/report_logic/base.rb +32 -41
- data/lib/report_logic/field.rb +24 -17
- data/lib/report_logic.rb +2 -8
- data/report_logic.gemspec +7 -7
- data/spec/app/my_report.rb +23 -20
- data/spec/report_logic/base_spec.rb +20 -17
- data/spec/spec_helper.rb +1 -1
- metadata +3 -14
- data/lib/report_logic/decorable.rb +0 -17
- data/lib/report_logic/decorator.rb +0 -26
- data/lib/report_logic/i18n_decorator.rb +0 -13
- data/lib/report_logic/i18n_helper.rb +0 -14
- data/lib/report_logic/i18n_support.rb +0 -7
- data/lib/report_logic/session.rb +0 -51
- data/spec/app/my_decorator.rb +0 -5
- data/spec/app/my_i18n_report.rb +0 -12
- data/spec/report_logic/decorator_spec.rb +0 -49
- data/spec/report_logic/i18n_support_spec.rb +0 -27
- data/spec/report_logic/session_spec.rb +0 -73
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cb5c7c667ee0a5c8228b50f6ee901af04bafd30
|
4
|
+
data.tar.gz: 48a9eb686cd2018e8ca6303e59901457f25424ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bae0ce482ce7f60035008fc8cac709b12286a1ad29b8ce6f6bc8258be9d4b14a164e5eb7b6616107afb8767eefb83b0a7283deaaca00b42949335ef5d3669a1
|
7
|
+
data.tar.gz: cbd54520a0457b2373af13adeedbf5e386842c9eca9f91cb5b477aa40f22df5b7df7cd7fe8755dab61fff7f5418abb68ed1c587526ef6fb325a2efee27b00027
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.2.2
|
data/README.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
# ReportLogic
|
2
2
|
|
3
3
|
This gem provides an easy way to generate reports' logic.
|
4
|
-
|
4
|
+
You can organize the way your data needs to be grouped, converted and organized,
|
5
|
+
regardless how you are going to display it afterwards. I use this to export
|
6
|
+
the same report in various formats, like XLS and PDF, using the same Report
|
7
|
+
object.
|
5
8
|
|
6
9
|
## Installation
|
7
10
|
|
@@ -12,37 +15,34 @@ gem install report_logic
|
|
12
15
|
Using Bundler:
|
13
16
|
|
14
17
|
```ruby
|
15
|
-
gem 'report_logic'
|
18
|
+
gem 'report_logic'
|
16
19
|
```
|
17
20
|
|
18
21
|
## Usage
|
19
22
|
|
20
23
|
The goal here is to provide an easy way to extract report's logic to a Plain Old
|
21
|
-
Ruby Object.
|
22
|
-
render reports in different formats. The report is defined by doing this:
|
24
|
+
Ruby Object. The report is defined by doing this:
|
23
25
|
|
24
26
|
```ruby
|
25
27
|
# Create a class inheriting from ReportLogic::Base
|
26
28
|
class MyReport < ReportLogic::Base
|
27
29
|
|
28
|
-
#
|
29
|
-
|
30
|
-
|
31
|
-
#
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
value record.name
|
45
|
-
end
|
30
|
+
# Create as many sessions as you need. Sessions group fields that you can
|
31
|
+
# then display later.
|
32
|
+
session :header do
|
33
|
+
field 'ID' # `field` creates a ReportLogic::Field in the session,
|
34
|
+
# which `name` will be the value passed to it.
|
35
|
+
field 'Name'
|
36
|
+
end
|
37
|
+
|
38
|
+
# If you call `collection_session`, it will become a matrix, instead of
|
39
|
+
# an array of Field. For each object in the collection given to the
|
40
|
+
# constructor, the block will be yielded with it so you can define what Fields
|
41
|
+
# will compose the matrix.
|
42
|
+
collection_session :row do |record|
|
43
|
+
value record.id # `value` creates a ReportLogic::Field in the session,
|
44
|
+
# which `value` will be the value passed to it.
|
45
|
+
value record.name
|
46
46
|
end
|
47
47
|
end
|
48
48
|
```
|
@@ -56,14 +56,14 @@ report = MyReport.new(MyModel.all)
|
|
56
56
|
puts 'MY REPORT'
|
57
57
|
|
58
58
|
# Printing the headers
|
59
|
-
report.each
|
59
|
+
report.header.each do |field|
|
60
60
|
print field.name
|
61
61
|
print ' | '
|
62
62
|
end
|
63
63
|
puts
|
64
64
|
|
65
65
|
# Printing the rows
|
66
|
-
report.each
|
66
|
+
report.row.each do |row|
|
67
67
|
row.each do |field|
|
68
68
|
print field.value
|
69
69
|
print ' | '
|
@@ -72,102 +72,44 @@ report.each(:row) do |row|
|
|
72
72
|
end
|
73
73
|
```
|
74
74
|
|
75
|
-
This output
|
75
|
+
This output is not pretty, but it shows how simple it is to read a report
|
76
76
|
and output it in any ways you want. You could use ERB to generate HTML or XLS
|
77
77
|
outputs in Rails, or even Prawn to generate PDF's.
|
78
78
|
|
79
79
|
## Decorators
|
80
80
|
|
81
|
-
This gem uses the Decorator
|
82
|
-
printing. To do so,
|
83
|
-
|
81
|
+
This gem uses the Decorator Pattern to allow you to modify a Field before
|
82
|
+
printing. To do so, pass the decorator as a parameter to the field. It can be
|
83
|
+
any object, it just needs to respond to `call`. It can be a simple block.
|
84
84
|
|
85
85
|
```ruby
|
86
|
-
class MyDecorator
|
87
|
-
|
88
|
-
|
89
|
-
def decorate(field)
|
90
|
-
decorate_name(field)
|
91
|
-
decorate_value(field)
|
86
|
+
class MyDecorator
|
87
|
+
def call(name)
|
88
|
+
"=> #{name}"
|
92
89
|
end
|
93
|
-
|
94
|
-
# This method is called by default from ReportLogic::Decorator, as
|
95
|
-
# you can see above. Override this if you just want to decorate the name
|
96
|
-
# attribute of your fields.
|
97
|
-
def decorate_name(field); end
|
98
|
-
|
99
|
-
# This method is called by default from ReportLogic::Decorator as well.
|
100
|
-
# Override this if you just want to decorate the value attribute of your fields.
|
101
|
-
def decorate_value(field); end
|
102
90
|
end
|
103
|
-
```
|
104
|
-
|
105
|
-
This sample decorator is, in fact, pretty much the same as
|
106
|
-
`ReportLogic::Decorator`. For more details, take a look at the class itself.
|
107
|
-
|
108
|
-
You can then apply a decorator in many ways:
|
109
91
|
|
110
|
-
```ruby
|
111
92
|
class MyReport < ReportLogic::Base
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
field 'Gender', record.gender
|
116
|
-
|
117
|
-
decorate_with MyDecorator.new # This decorator applies to this session
|
118
|
-
end
|
119
|
-
|
120
|
-
decorate_with AnotherDecorator.new # This decorator applies to the whole report
|
93
|
+
collection_session :row do |record|
|
94
|
+
field 'Name', record.name, decorate_name: MyDecorator.new,
|
95
|
+
decorate_value: ->(value) { " - #{value}" }
|
121
96
|
end
|
122
97
|
end
|
123
|
-
```
|
124
98
|
|
125
|
-
|
126
|
-
|
127
|
-
```ruby
|
128
|
-
field 'Name', record.name, decorate_with: SpecificDecorator.new
|
129
|
-
```
|
99
|
+
MyModel = Struct.new(:name)
|
100
|
+
collection = [MyModel.new('John')]
|
130
101
|
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
```ruby
|
137
|
-
class MyReport < ReportLogic::Base
|
138
|
-
include ReportLogic::I18nSupport # Include de module
|
139
|
-
|
140
|
-
def build
|
141
|
-
i18n_decorate # Invoke the decorator to be added
|
142
|
-
|
143
|
-
session(:row, collection) do |record|
|
144
|
-
field 'Name', record.name
|
145
|
-
field 'Gender', record.gender
|
146
|
-
end
|
102
|
+
MyReport.new(collection).row.each do |row|
|
103
|
+
row.each do |field|
|
104
|
+
puts field.name
|
105
|
+
puts field.value
|
147
106
|
end
|
148
107
|
end
|
108
|
+
#=> => Name
|
109
|
+
#=> - John
|
149
110
|
```
|
150
111
|
|
151
|
-
|
152
|
-
So, to make it work, you need to use symbols:
|
153
|
-
|
154
|
-
```ruby
|
155
|
-
class MyReport < ReportLogic::Base
|
156
|
-
include ReportLogic::I18nSupport
|
157
|
-
|
158
|
-
def build
|
159
|
-
i18n_decorate
|
160
|
-
|
161
|
-
session(:filter) do
|
162
|
-
field :name , 'Test' # The field's name will be translated using
|
163
|
-
# 'report.names.name'
|
164
|
-
field :gender, :male # The field's name will be translated using
|
165
|
-
# 'report.names.gender' and the value will use
|
166
|
-
# 'report.values.male'.
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
```
|
112
|
+
If you need to apply multiple decorators on the same field, just pass an array.
|
171
113
|
|
172
114
|
## Contributing
|
173
115
|
|
data/lib/report_logic/base.rb
CHANGED
@@ -1,70 +1,61 @@
|
|
1
1
|
module ReportLogic
|
2
2
|
class Base
|
3
|
-
include Decorable
|
4
|
-
|
5
3
|
attr_reader :collection
|
6
4
|
|
7
5
|
def initialize(collection = nil)
|
8
6
|
@collection = collection
|
9
7
|
end
|
10
8
|
|
11
|
-
def
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
def session(key, collection = nil, &block)
|
18
|
-
@current_session = sessions[key] ||= Session.new(key, self)
|
19
|
-
@current_session.process(collection, &block)
|
20
|
-
ensure
|
21
|
-
@current_session = nil
|
9
|
+
def fields(&block)
|
10
|
+
@current = []
|
11
|
+
instance_exec &block
|
12
|
+
@current
|
22
13
|
end
|
23
14
|
|
24
|
-
def
|
25
|
-
|
15
|
+
def collection_fields(&block)
|
16
|
+
collection.map do |record|
|
17
|
+
fields do
|
18
|
+
instance_exec record, &block
|
19
|
+
end
|
20
|
+
end
|
26
21
|
end
|
27
22
|
|
28
|
-
def
|
29
|
-
|
30
|
-
@
|
31
|
-
|
32
|
-
|
23
|
+
def self.session(key, &block)
|
24
|
+
define_method key do
|
25
|
+
@current = []
|
26
|
+
instance_eval &block
|
27
|
+
@current
|
33
28
|
end
|
34
29
|
end
|
35
30
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
else
|
40
|
-
super
|
31
|
+
def self.collection_session(key, &block)
|
32
|
+
define_method key do
|
33
|
+
collection_fields &block
|
41
34
|
end
|
42
35
|
end
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
build unless @built
|
48
|
-
decorate unless @decorated
|
49
|
-
@built = true
|
50
|
-
@decorated = true
|
37
|
+
def each(key)
|
38
|
+
fail '`each` is not used anymore. Please, refer to the project\'s '\
|
39
|
+
' documentation on Github for more details.'
|
51
40
|
end
|
52
41
|
|
53
|
-
def
|
54
|
-
|
42
|
+
def session(key, collection = nil, &block)
|
43
|
+
fail '`session` is not used anymore. Please, refer to the project\'s '\
|
44
|
+
' documentation on Github for more details.'
|
55
45
|
end
|
56
46
|
|
57
|
-
def
|
58
|
-
|
59
|
-
sess.decorate(@decorators)
|
60
|
-
end
|
47
|
+
def count
|
48
|
+
collection.size
|
61
49
|
end
|
62
50
|
|
63
|
-
|
51
|
+
protected
|
64
52
|
|
65
|
-
def
|
66
|
-
@
|
53
|
+
def field(name, value = nil, **options)
|
54
|
+
@current << Field.new(name, value, **options)
|
67
55
|
end
|
68
56
|
|
57
|
+
def value(val, **options)
|
58
|
+
field(nil, val, **options)
|
59
|
+
end
|
69
60
|
end
|
70
61
|
end
|
data/lib/report_logic/field.rb
CHANGED
@@ -1,16 +1,22 @@
|
|
1
1
|
module ReportLogic
|
2
2
|
class Field
|
3
|
-
|
3
|
+
attr_reader :type, :config
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@
|
5
|
+
def initialize(name, value = nil, **config)
|
6
|
+
@name = name
|
7
|
+
@value = value
|
8
|
+
@type = config.delete :type
|
9
|
+
@decorate_name = config.delete :decorate_name
|
10
|
+
@decorate_value = config.delete :decorate_value
|
11
11
|
@config = config
|
12
|
-
|
13
|
-
|
12
|
+
end
|
13
|
+
|
14
|
+
def name
|
15
|
+
apply_decorators @name, @decorate_name
|
16
|
+
end
|
17
|
+
|
18
|
+
def value
|
19
|
+
apply_decorators @value, @decorate_value
|
14
20
|
end
|
15
21
|
|
16
22
|
def type
|
@@ -21,16 +27,17 @@ module ReportLogic
|
|
21
27
|
@value.class
|
22
28
|
end
|
23
29
|
|
24
|
-
|
25
|
-
apply_decorators(master_decorators) if master_decorators
|
26
|
-
apply_decorators(decorators)
|
27
|
-
end
|
30
|
+
private
|
28
31
|
|
29
|
-
def apply_decorators(decorators)
|
30
|
-
|
31
|
-
|
32
|
+
def apply_decorators(str, decorators)
|
33
|
+
return str unless decorators
|
34
|
+
if decorators.respond_to? :each
|
35
|
+
decorators.each do |dec|
|
36
|
+
str = dec.call str
|
37
|
+
end
|
38
|
+
return str
|
32
39
|
end
|
33
|
-
|
40
|
+
decorators.call str
|
34
41
|
end
|
35
42
|
end
|
36
43
|
end
|
data/lib/report_logic.rb
CHANGED
@@ -1,10 +1,4 @@
|
|
1
1
|
module ReportLogic
|
2
|
-
autoload :Base
|
3
|
-
autoload :
|
4
|
-
autoload :Decorator , 'report_logic/decorator'
|
5
|
-
autoload :Field , 'report_logic/field'
|
6
|
-
autoload :Session , 'report_logic/session'
|
7
|
-
autoload :I18nDecorator, 'report_logic/i18n_decorator'
|
8
|
-
autoload :I18nSupport , 'report_logic/i18n_support'
|
9
|
-
autoload :I18nHelper , 'report_logic/i18n_helper'
|
2
|
+
autoload :Base, 'report_logic/base'
|
3
|
+
autoload :Field, 'report_logic/field'
|
10
4
|
end
|
data/report_logic.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'report_logic'
|
3
|
-
s.version = '0.
|
4
|
-
s.date = '
|
5
|
-
s.summary =
|
6
|
-
s.description =
|
7
|
-
|
8
|
-
s.authors = [
|
3
|
+
s.version = '1.0.0'
|
4
|
+
s.date = '2015-09-09'
|
5
|
+
s.summary = 'Generating report logic.'
|
6
|
+
s.description = 'This gem provides an easy way to generate reports\' logic. '\
|
7
|
+
'Using this, you can so export this report to any format you want.'
|
8
|
+
s.authors = ['Diego Aguir Selzlein']
|
9
9
|
s.email = 'diegoselzlein@gmail.com'
|
10
10
|
|
11
11
|
s.files = `git ls-files`.split("\n")
|
12
|
-
s.require_paths = [
|
12
|
+
s.require_paths = ['lib']
|
13
13
|
|
14
14
|
s.homepage = 'https://github.com/nerde/report_logic'
|
15
15
|
s.license = 'MIT'
|
data/spec/app/my_report.rb
CHANGED
@@ -1,31 +1,34 @@
|
|
1
1
|
Record = Struct.new(:id, :name)
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
session(:header) do
|
6
|
-
field 'ID'
|
7
|
-
field 'Name'
|
8
|
-
end
|
3
|
+
FIRST_DECORATOR = ->(value) { value + 3 }
|
4
|
+
SECOND_DECORATOR = ->(value) { value / 2 }
|
9
5
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
6
|
+
class MyReport < ReportLogic::Base
|
7
|
+
session :header do
|
8
|
+
field 'ID'
|
9
|
+
field 'Name'
|
10
|
+
end
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
collection_session :rows do |record|
|
13
|
+
value record.id
|
14
|
+
value record.name
|
15
|
+
end
|
18
16
|
|
19
|
-
|
20
|
-
|
17
|
+
def my_own_rows
|
18
|
+
collection.map do |record|
|
19
|
+
fields do
|
20
|
+
value record.id
|
21
|
+
end
|
21
22
|
end
|
23
|
+
end
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
25
|
+
session :test_context do
|
26
|
+
value context_value
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
+
session :decorated do
|
30
|
+
field 'A', 1, decorate_name: ->(name) { name + 'B' },
|
31
|
+
decorate_value: [FIRST_DECORATOR, SECOND_DECORATOR]
|
29
32
|
end
|
30
33
|
|
31
34
|
def context_value
|
@@ -6,35 +6,38 @@ describe ReportLogic::Base do
|
|
6
6
|
|
7
7
|
let(:report) { MyReport.new([rec1, rec2]) }
|
8
8
|
|
9
|
-
let(:headers) { report.
|
10
|
-
let(:rows) { report.
|
9
|
+
let(:headers) { report.header }
|
10
|
+
let(:rows) { report.rows }
|
11
|
+
let(:decorated) { report.decorated }
|
11
12
|
|
12
|
-
it
|
13
|
-
expect(headers.count
|
14
|
-
expect(headers.
|
15
|
-
expect(headers.
|
13
|
+
it 'generates grouped fields' do
|
14
|
+
expect(headers.count).to eql(2)
|
15
|
+
expect(headers.first.name).to eql('ID')
|
16
|
+
expect(headers.last.name).to eql('Name')
|
16
17
|
|
18
|
+
expect(rows.count).to eql(2)
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
first_row = rows.next
|
20
|
+
first_row = rows.first
|
21
21
|
expect(first_row.first.value).to eql(1)
|
22
22
|
expect(first_row.last.value ).to eql('Diego')
|
23
23
|
|
24
|
-
last_row = rows.
|
24
|
+
last_row = rows.last
|
25
25
|
expect(last_row.first.value ).to eql(2)
|
26
26
|
expect(last_row.last.value ).to eql('Andressa')
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
30
|
-
expect(report.
|
29
|
+
it 'allows to define methods directly' do
|
30
|
+
expect(report.my_own_rows.size).to eq 2
|
31
|
+
expect(report.my_own_rows.first.map(&:value)).to eq [1]
|
32
|
+
expect(report.my_own_rows.last.map(&:value)).to eq [2]
|
31
33
|
end
|
32
34
|
|
33
|
-
it
|
34
|
-
|
35
|
-
|
35
|
+
it 'decorates names and values' do
|
36
|
+
expect(decorated.first.name).to eql('AB')
|
37
|
+
expect(decorated.first.value).to eql(2)
|
38
|
+
end
|
36
39
|
|
37
|
-
|
38
|
-
expect(
|
40
|
+
it 'counts records' do
|
41
|
+
expect(report.count).to eq 2
|
39
42
|
end
|
40
43
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,7 +3,7 @@ require File.expand_path('../../lib/report_logic', __FILE__)
|
|
3
3
|
Dir[File.expand_path('../app/*.rb', __FILE__)].each { |f| require f }
|
4
4
|
|
5
5
|
RSpec.configure do |config|
|
6
|
-
config.order =
|
6
|
+
config.order = 'random'
|
7
7
|
config.before(:each) { GC.disable }
|
8
8
|
config.after(:each) { GC.enable }
|
9
9
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: report_logic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Diego Aguir Selzlein
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: i18n
|
@@ -37,23 +37,12 @@ files:
|
|
37
37
|
- deploy
|
38
38
|
- lib/report_logic.rb
|
39
39
|
- lib/report_logic/base.rb
|
40
|
-
- lib/report_logic/decorable.rb
|
41
|
-
- lib/report_logic/decorator.rb
|
42
40
|
- lib/report_logic/field.rb
|
43
|
-
- lib/report_logic/i18n_decorator.rb
|
44
|
-
- lib/report_logic/i18n_helper.rb
|
45
|
-
- lib/report_logic/i18n_support.rb
|
46
|
-
- lib/report_logic/session.rb
|
47
41
|
- report_logic.gemspec
|
48
42
|
- script/console
|
49
|
-
- spec/app/my_decorator.rb
|
50
|
-
- spec/app/my_i18n_report.rb
|
51
43
|
- spec/app/my_report.rb
|
52
44
|
- spec/report_logic/base_spec.rb
|
53
|
-
- spec/report_logic/decorator_spec.rb
|
54
45
|
- spec/report_logic/field_spec.rb
|
55
|
-
- spec/report_logic/i18n_support_spec.rb
|
56
|
-
- spec/report_logic/session_spec.rb
|
57
46
|
- spec/spec_helper.rb
|
58
47
|
homepage: https://github.com/nerde/report_logic
|
59
48
|
licenses:
|
@@ -75,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
64
|
version: '0'
|
76
65
|
requirements: []
|
77
66
|
rubyforge_project:
|
78
|
-
rubygems_version: 2.
|
67
|
+
rubygems_version: 2.4.5
|
79
68
|
signing_key:
|
80
69
|
specification_version: 4
|
81
70
|
summary: Generating report logic.
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module ReportLogic
|
2
|
-
module Decorable
|
3
|
-
def decorators
|
4
|
-
@decorators ||= []
|
5
|
-
end
|
6
|
-
|
7
|
-
def decorate_with(dec)
|
8
|
-
if dec
|
9
|
-
if dec.respond_to?(:each)
|
10
|
-
dec.each { |dec| decorators << dec }
|
11
|
-
else
|
12
|
-
decorators << dec
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module ReportLogic
|
2
|
-
class Decorator
|
3
|
-
attr_accessor :key
|
4
|
-
|
5
|
-
def initialize(key: nil)
|
6
|
-
@key = key
|
7
|
-
end
|
8
|
-
|
9
|
-
def decorate(field)
|
10
|
-
decorate_name(field)
|
11
|
-
decorate_value(field)
|
12
|
-
end
|
13
|
-
|
14
|
-
def decorate_name(field); end
|
15
|
-
|
16
|
-
def decorate_value(field); end
|
17
|
-
|
18
|
-
def decorate_if_matches(field)
|
19
|
-
decorate(field) if matches?(field)
|
20
|
-
end
|
21
|
-
|
22
|
-
def matches?(field)
|
23
|
-
key.nil? || key == field.key || Array === key && key.include?(field.key)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module ReportLogic
|
2
|
-
class I18nDecorator < Decorator
|
3
|
-
include I18nHelper
|
4
|
-
|
5
|
-
def decorate_name(field, name=field.name, **options)
|
6
|
-
field.name = i18n_lookup(:names , name, **options) if Symbol === name
|
7
|
-
end
|
8
|
-
|
9
|
-
def decorate_value(field, value=field.value, **options)
|
10
|
-
field.value = i18n_lookup(:values , value, **options) if Symbol === value
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'i18n'
|
2
|
-
|
3
|
-
module ReportLogic
|
4
|
-
module I18nHelper
|
5
|
-
def i18n_lookup(scope, key, default: key.to_sym, resource_class: nil, **options)
|
6
|
-
lookup = if resource_class
|
7
|
-
:"#{resource_class.to_s.downcase.underscore}.#{key}"
|
8
|
-
else
|
9
|
-
key
|
10
|
-
end
|
11
|
-
I18n.t(lookup, scope: [:report, scope], default: default, **options)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
data/lib/report_logic/session.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
module ReportLogic
|
2
|
-
class Session
|
3
|
-
include Decorable
|
4
|
-
|
5
|
-
attr_accessor :key, :report
|
6
|
-
|
7
|
-
def initialize(key = nil, report = nil)
|
8
|
-
@key, @report = key, report
|
9
|
-
end
|
10
|
-
|
11
|
-
def fields
|
12
|
-
@fields ||= []
|
13
|
-
end
|
14
|
-
|
15
|
-
def current_row
|
16
|
-
@current_row ||= fields
|
17
|
-
end
|
18
|
-
|
19
|
-
def field(name, value = nil, **options)
|
20
|
-
current_row << Field.new(name, value, **options)
|
21
|
-
end
|
22
|
-
|
23
|
-
def value(val, **options)
|
24
|
-
field(nil, val, **options)
|
25
|
-
end
|
26
|
-
|
27
|
-
def process(collection = nil)
|
28
|
-
if collection.respond_to?(:each)
|
29
|
-
collection.each do |record|
|
30
|
-
@current_row = []
|
31
|
-
yield record
|
32
|
-
fields.push current_row
|
33
|
-
@current_row = nil
|
34
|
-
end
|
35
|
-
else
|
36
|
-
yield
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def decorate(master_decorators = nil)
|
41
|
-
master_decorators ||= []
|
42
|
-
fields.each do |field_or_row|
|
43
|
-
if field_or_row.respond_to?(:each)
|
44
|
-
field_or_row.each { |f| f.decorate(master_decorators + decorators) }
|
45
|
-
else
|
46
|
-
field_or_row.decorate(master_decorators + decorators)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
data/spec/app/my_decorator.rb
DELETED
data/spec/app/my_i18n_report.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ReportLogic::Decorator do
|
4
|
-
let(:decorator) { MyDecorator.new }
|
5
|
-
let(:field) { ReportLogic::Field.new(1, 2, key: :first) }
|
6
|
-
|
7
|
-
describe "#decorate_if_matches" do
|
8
|
-
|
9
|
-
it "decorates object" do
|
10
|
-
decorator.decorate_if_matches(field)
|
11
|
-
|
12
|
-
expect(field.value).to eql(3)
|
13
|
-
end
|
14
|
-
|
15
|
-
context "decorator does not match" do
|
16
|
-
it "doesn't decorate" do
|
17
|
-
decorator.stub(:matches?).and_return(false)
|
18
|
-
|
19
|
-
decorator.decorate_if_matches(field)
|
20
|
-
|
21
|
-
expect(field.value).to eql(2)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context "key doesn't match" do
|
26
|
-
let(:decorator) { ReportLogic::Decorator.new key: :second }
|
27
|
-
|
28
|
-
it "doesn't decorate" do
|
29
|
-
decorator.decorate_if_matches(field)
|
30
|
-
|
31
|
-
expect(field.value).to eql(2)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
context "key is an array" do
|
36
|
-
let(:decorator) { MyDecorator.new key: [:second] }
|
37
|
-
|
38
|
-
it "checks inclusion" do
|
39
|
-
f2 = ReportLogic::Field.new(1, 2, key: :second)
|
40
|
-
|
41
|
-
decorator.decorate_if_matches(field)
|
42
|
-
decorator.decorate_if_matches(f2)
|
43
|
-
|
44
|
-
expect(field.value).to eql(2)
|
45
|
-
expect(f2 .value).to eql(3)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'i18n'
|
3
|
-
|
4
|
-
describe ReportLogic::I18nSupport do
|
5
|
-
let(:report) { MyI18nReport.new }
|
6
|
-
|
7
|
-
let(:headers) { report.each(:header).to_a }
|
8
|
-
|
9
|
-
before(:all) do
|
10
|
-
I18n.enforce_available_locales = false
|
11
|
-
I18n.backend.store_translations(:es, report: { names: { name: 'Nombre' },
|
12
|
-
values: { test: 'This is translated too' } })
|
13
|
-
I18n.default_locale = :es
|
14
|
-
end
|
15
|
-
|
16
|
-
after(:all) do
|
17
|
-
I18n.backend = nil
|
18
|
-
I18n.default_locale = nil
|
19
|
-
end
|
20
|
-
|
21
|
-
it "translates key fields" do
|
22
|
-
expect(headers.size ).to eql(2)
|
23
|
-
expect(headers.first.name).to eql('ID')
|
24
|
-
expect(headers.last .name).to eql('Nombre')
|
25
|
-
expect(headers.last.value).to eql('This is translated too')
|
26
|
-
end
|
27
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe ReportLogic::Session do
|
4
|
-
let(:session) { ReportLogic::Session.new }
|
5
|
-
let(:fields) { session.fields }
|
6
|
-
let(:field) { session.fields.first }
|
7
|
-
|
8
|
-
it "generates fields with name" do
|
9
|
-
session.field 'Test Field'
|
10
|
-
|
11
|
-
expect(fields.size).to eq(1)
|
12
|
-
expect(field.key ).to be_nil
|
13
|
-
expect(field.name ).to eq('Test Field')
|
14
|
-
expect(field.value).to be_nil
|
15
|
-
end
|
16
|
-
|
17
|
-
it "generates fields with value" do
|
18
|
-
session.value 'Test Field'
|
19
|
-
|
20
|
-
expect(fields.size).to eq(1)
|
21
|
-
expect(field.key ).to be_nil
|
22
|
-
expect(field.name ).to be_nil
|
23
|
-
expect(field.value).to eq('Test Field')
|
24
|
-
end
|
25
|
-
|
26
|
-
it "generates fields with name and value" do
|
27
|
-
session.field 'Test Field', 'Value'
|
28
|
-
|
29
|
-
expect(fields.size).to eq(1)
|
30
|
-
expect(field.key ).to be_nil
|
31
|
-
expect(field.name ).to eq('Test Field')
|
32
|
-
expect(field.value).to eq('Value')
|
33
|
-
end
|
34
|
-
|
35
|
-
it "generates fields with key" do
|
36
|
-
session.field 'Test Field', 'Value', key: :sparta
|
37
|
-
|
38
|
-
expect(fields.size).to eq(1)
|
39
|
-
expect(field.key ).to eq(:sparta)
|
40
|
-
expect(field.name ).to eq('Test Field')
|
41
|
-
expect(field.value).to eq('Value')
|
42
|
-
end
|
43
|
-
|
44
|
-
it "uses name as key when it's a symbol" do
|
45
|
-
session.field :name
|
46
|
-
|
47
|
-
expect(fields.size).to eq(1)
|
48
|
-
expect(field.key ).to eq(:name)
|
49
|
-
expect(field.name ).to eq(:name)
|
50
|
-
expect(field.value).to be_nil
|
51
|
-
end
|
52
|
-
|
53
|
-
it "decorates fields" do
|
54
|
-
session.field 'First Field' , 1
|
55
|
-
session.field 'Second Field', 2
|
56
|
-
session.decorate_with MyDecorator.new
|
57
|
-
|
58
|
-
session.decorate
|
59
|
-
|
60
|
-
expect(fields.first.value).to eq(3)
|
61
|
-
expect(fields.last .value).to eq(3)
|
62
|
-
end
|
63
|
-
|
64
|
-
it "can decorate a single field" do
|
65
|
-
session.field 'First Field' , 1
|
66
|
-
session.field 'Second Field', 2, decorate_with: MyDecorator.new
|
67
|
-
|
68
|
-
session.decorate
|
69
|
-
|
70
|
-
expect(fields.first.value).to eq(1)
|
71
|
-
expect(fields.last .value).to eq(3)
|
72
|
-
end
|
73
|
-
end
|