report_logic 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e25770ff6e1144dbbdba31d2cd5ce7c18b754fc
4
- data.tar.gz: 01b93068112f0428369d7bf05f7c595cfc483e71
3
+ metadata.gz: 6cea2b2274f2df932ea10af17de0b1a67a784132
4
+ data.tar.gz: c42344ccba2cc912cb674f51fbea93d1ca4d0ad1
5
5
  SHA512:
6
- metadata.gz: e7cc1c97d098b1595c8799f45c1d69954976b6628655c9868e30eff3038c1ba33a9680b41ba919a71c50256e0c49b455ec9dc2391214b3d507589b5476ff5ed4
7
- data.tar.gz: fff161688583ee20d397030165b647b15e3b419ee196d81892e311b717fb0b38b264975a05f645394053bf0e190ac784ed2898734d04140e54b981838b4f4c5a
6
+ metadata.gz: 51970b4691bf3dd8c0b156829ea95c926bd1d4f565e74647b1a85b7f9de513be51ddb7b2b23454ec40361fe502b938440b99bc94e67ec14d4908a2909ebfa81b
7
+ data.tar.gz: d32eb19919641f367dbf782212597645f32b4c16bc1b26d719e868ca0c7b3c519a81fe0974adb5af8574acbf742d49d6f0f08920627b17fe3052d3faae3fca61
data/README.md CHANGED
@@ -9,12 +9,170 @@ Using this, you can so export this report to any format you want.
9
9
  gem install report_logic
10
10
  ```
11
11
 
12
- Using bundler:
12
+ Using Bundler:
13
13
 
14
14
  ```ruby
15
- gem 'report_logic'
15
+ gem 'report_logic', '~> 0.0.2'
16
16
  ```
17
17
 
18
18
  ## Usage
19
19
 
20
- Building...
20
+ The goal here is to provide an easy way to extract report's logic to a Plain Old
21
+ Ruby Object. With this extraction, it's easy to use the same report class to
22
+ render reports in different formats. The report is defined by doing this:
23
+
24
+ ```ruby
25
+ # Create a class inheriting from ReportLogic::Base
26
+ class MyReport < ReportLogic::Base
27
+
28
+ # Override this method to define your report
29
+ def build
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 pass a collection to a session, it will become a matrix, instead of
39
+ # an array of Field. For each object in the collection, the block will be
40
+ # yielded with it so you can define what Fields will compose this matrix.
41
+ session(:row, collection) do |record|
42
+ value record.id # `value` creates a ReportLogic::Field in the session,
43
+ # which `value` will be the value passed to it.
44
+ value record.name
45
+ end
46
+ end
47
+ end
48
+ ```
49
+
50
+ And that's the basics. Now, to show this report:
51
+
52
+ ```ruby
53
+ report = MyReport.new(MyModel.all)
54
+
55
+ # Printing a title
56
+ puts 'MY REPORT'
57
+
58
+ # Printing the headers
59
+ report.each(:header) do |field|
60
+ print field.name
61
+ print ' | '
62
+ end
63
+ puts
64
+
65
+ # Printing the rows
66
+ report.each(:row) do |row|
67
+ row.each do |field|
68
+ print field.value
69
+ print ' | '
70
+ end
71
+ end
72
+ puts
73
+ ```
74
+
75
+ This output sucks, but it shows how simple it is to read a report
76
+ and output it in any ways you want. You could use ERB to generate HTML or XLS
77
+ outputs in Rails, or even Prawn to generate PDF's.
78
+
79
+ ## Decorators
80
+
81
+ This gem uses the Decorator pattern to allow you to modify a Field before
82
+ printing. To do so, you just need to inherit from `ReportLogic::Decorator`,
83
+ like so:
84
+
85
+ ```ruby
86
+ class MyDecorator < ReportLogic::Decorator
87
+ # This method is invoked when the report is built. Override this if you
88
+ # want to customize the whole behaviour of your decorator.
89
+ def decorate(field)
90
+ decorate_name(field)
91
+ decorate_value(field)
92
+ 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
+ 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
+
110
+ ```ruby
111
+ class MyReport < ReportLogic::Base
112
+ def build
113
+ session(:row, collection) do |record|
114
+ field 'Name', record.name
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
121
+ end
122
+ end
123
+ ```
124
+
125
+ You can apply a decorator to a specific field. To do so, you need to:
126
+
127
+ ```ruby
128
+ field 'Name', record.name, decorate_with: SpecifiDecorator.new
129
+ ```
130
+
131
+ ## I18n Support
132
+
133
+ There is a built-in decorator that handles internationalization for you. To use
134
+ it, you need to do this:
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
147
+ end
148
+ end
149
+ ```
150
+
151
+ This way, your fields' names and values will be translated **if they are symbols**.
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
+ ```
171
+
172
+ ## Contributing
173
+
174
+ Do you have an idea or a bug fix to add to the gem? Please do!
175
+
176
+ 1. Fork it
177
+ 2. Make your changes and commits
178
+ 3. Create a pull request
@@ -17,6 +17,8 @@ module ReportLogic
17
17
  def session(key, collection = nil, &block)
18
18
  @current_session = sessions[key] ||= Session.new(key, self)
19
19
  @current_session.process(collection, &block)
20
+ ensure
21
+ @current_session = nil
20
22
  end
21
23
 
22
24
  def count
@@ -31,6 +33,14 @@ module ReportLogic
31
33
  end
32
34
  end
33
35
 
36
+ def decorate_with(*args)
37
+ if @current_session
38
+ @current_session.decorate_with(*args)
39
+ else
40
+ super
41
+ end
42
+ end
43
+
34
44
  protected
35
45
 
36
46
  def ensure_built_and_decorated
data/report_logic.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'report_logic'
3
- s.version = '0.0.1'
4
- s.date = '2014-09-17'
3
+ s.version = '0.0.2'
4
+ s.date = '2014-09-18'
5
5
  s.summary = "Generating report logic."
6
6
  s.description = "This gem provides an easy way to generate reports' logic. "\
7
7
  "Using this, you can so export this report to any format you want."
@@ -15,6 +15,17 @@ class MyReport < ReportLogic::Base
15
15
  session(:test_context) do
16
16
  context_value
17
17
  end
18
+
19
+ session(:test_master_decorator) do
20
+ field :decorable, 2
21
+ end
22
+
23
+ session(:exclusive_decorator) do
24
+ value 2
25
+ decorate_with MyDecorator.new
26
+ end
27
+
28
+ decorate_with MyDecorator.new(key: :decorable)
18
29
  end
19
30
 
20
31
  def context_value
@@ -29,4 +29,12 @@ describe ReportLogic::Base do
29
29
  it "counts records" do
30
30
  expect(report.count).to eq 2
31
31
  end
32
+
33
+ it "scopes decorators" do
34
+ s1 = report.each(:test_master_decorator).to_a
35
+ s2 = report.each(:exclusive_decorator ).to_a
36
+
37
+ expect(s1.first.value).to eq(3)
38
+ expect(s2.first.value).to eq(3)
39
+ end
32
40
  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.0.1
4
+ version: 0.0.2
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: 2014-09-17 00:00:00.000000000 Z
11
+ date: 2014-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n