report_logic 0.0.2 → 0.1.0

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: 6cea2b2274f2df932ea10af17de0b1a67a784132
4
- data.tar.gz: c42344ccba2cc912cb674f51fbea93d1ca4d0ad1
3
+ metadata.gz: 61001da4659fe8fed8fe0dd7eb7a00e0f053531e
4
+ data.tar.gz: 98e2a5f2f72b48bbd96fbb0d6f75af6c9667809a
5
5
  SHA512:
6
- metadata.gz: 51970b4691bf3dd8c0b156829ea95c926bd1d4f565e74647b1a85b7f9de513be51ddb7b2b23454ec40361fe502b938440b99bc94e67ec14d4908a2909ebfa81b
7
- data.tar.gz: d32eb19919641f367dbf782212597645f32b4c16bc1b26d719e868ca0c7b3c519a81fe0974adb5af8574acbf742d49d6f0f08920627b17fe3052d3faae3fca61
6
+ metadata.gz: 85932e315fea9508600ac8e9f68492c20e79cd10dce0716aa9ed48d8daf75cd94854b4eb5e8153fd0d94c95287eca8ec75d9e99099aa5f235691aad847b01ead
7
+ data.tar.gz: cac5ad2ac88e7ac4f2db7a9b1b972c86d7fde70840c03fce938d2393da9b01f7f839a473132a8f760b39fa98fa22b2a9129c3fc4049eac498aa0cc76217b7ce7
data/README.md CHANGED
@@ -68,8 +68,8 @@ report.each(:row) do |row|
68
68
  print field.value
69
69
  print ' | '
70
70
  end
71
+ puts
71
72
  end
72
- puts
73
73
  ```
74
74
 
75
75
  This output sucks, but it shows how simple it is to read a report
@@ -125,7 +125,7 @@ end
125
125
  You can apply a decorator to a specific field. To do so, you need to:
126
126
 
127
127
  ```ruby
128
- field 'Name', record.name, decorate_with: SpecifiDecorator.new
128
+ field 'Name', record.name, decorate_with: SpecificDecorator.new
129
129
  ```
130
130
 
131
131
  ## I18n Support
data/lib/report_logic.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'forwardable'
2
+
1
3
  module ReportLogic
2
4
  autoload :Base , 'report_logic/base'
3
5
  autoload :Decorable , 'report_logic/decorable'
@@ -1,6 +1,6 @@
1
1
  module ReportLogic
2
2
  class Base
3
- include Decorable
3
+ extend Forwardable
4
4
 
5
5
  attr_reader :collection
6
6
 
@@ -8,37 +8,20 @@ module ReportLogic
8
8
  @collection = collection
9
9
  end
10
10
 
11
- def each(key)
12
- return to_enum(__callee__, key) unless block_given?
13
- ensure_built_and_decorated
14
- sessions[key] and sessions[key].fields.each { |field| yield field }
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
22
- end
23
-
24
11
  def count
25
12
  @collection.size
26
13
  end
27
14
 
28
- def method_missing(method_name, *args, &block)
29
- if @current_session && @current_session.public_methods.include?(method_name)
30
- @current_session.public_send(method_name, *args, &block)
31
- else
32
- super
33
- end
15
+ def main_session
16
+ @main_session ||= Session.new(nil, self)
34
17
  end
35
18
 
36
- def decorate_with(*args)
37
- if @current_session
38
- @current_session.decorate_with(*args)
39
- else
40
- super
41
- end
19
+ def_delegators :main_session, :session, :decorate_with, :field, :value,
20
+ :decorate, :children
21
+
22
+ def each(*args, &block)
23
+ ensure_built_and_decorated
24
+ main_session.each(*args, &block)
42
25
  end
43
26
 
44
27
  protected
@@ -53,17 +36,5 @@ module ReportLogic
53
36
  def build
54
37
  raise NotImplementedError
55
38
  end
56
-
57
- def decorate
58
- sessions.values.each do |sess|
59
- sess.decorate(@decorators)
60
- end
61
- end
62
-
63
- private
64
-
65
- def sessions
66
- @sessions ||= {}
67
- end
68
39
  end
69
40
  end
@@ -2,13 +2,14 @@ module ReportLogic
2
2
  class Field
3
3
  include Decorable
4
4
 
5
- attr_accessor :key, :value, :type, :name
5
+ attr_accessor :key, :value, :type, :name, :config
6
6
 
7
- def initialize(name, value=nil, type: nil, key: nil, decorate_with: nil)
8
- @name = name
9
- @value = value
10
- @type = type
11
- @key = key || (name.is_a?(Symbol) ? name : nil)
7
+ def initialize(name, value=nil, type: nil, key: nil, decorate_with: nil, **config)
8
+ @name = name
9
+ @value = value
10
+ @type = type
11
+ @config = config
12
+ @key = key || (name.is_a?(Symbol) ? name : nil)
12
13
  self.decorate_with(decorate_with)
13
14
  end
14
15
 
@@ -1,7 +1,7 @@
1
1
  module ReportLogic
2
2
  module I18nSupport
3
3
  def i18n_decorate
4
- decorators << I18nDecorator.new
4
+ decorate_with I18nDecorator.new
5
5
  end
6
6
  end
7
7
  end
@@ -24,16 +24,19 @@ module ReportLogic
24
24
  field(nil, val, **options)
25
25
  end
26
26
 
27
- def process(collection = nil)
27
+ def process(collection = nil, &block)
28
28
  if collection.respond_to?(:each)
29
29
  collection.each do |record|
30
- @current_row = []
31
- yield record
32
- fields.push current_row
33
- @current_row = nil
30
+ begin
31
+ @current_row = []
32
+ instance_exec record, &block
33
+ fields.push current_row
34
+ ensure
35
+ @current_row = nil
36
+ end
34
37
  end
35
38
  else
36
- yield
39
+ instance_exec &block
37
40
  end
38
41
  end
39
42
 
@@ -46,6 +49,23 @@ module ReportLogic
46
49
  field_or_row.decorate(master_decorators + decorators)
47
50
  end
48
51
  end
52
+ children.each do |_, sess|
53
+ sess.decorate(decorators)
54
+ end
55
+ end
56
+
57
+ def each(key)
58
+ return to_enum(__callee__, key) unless block_given?
59
+ children[key] and children[key].fields.each { |field| yield field }
60
+ end
61
+
62
+ def session(key, collection = nil, &block)
63
+ sess = children[key] ||= Session.new(key, @report)
64
+ sess.process(collection, &block)
65
+ end
66
+
67
+ def children
68
+ @children ||= {}
49
69
  end
50
70
  end
51
71
  end
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.2'
4
- s.date = '2014-09-18'
3
+ s.version = '0.1.0'
4
+ s.date = '2014-11-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."
data/script/console ADDED
@@ -0,0 +1,9 @@
1
+ #!/bin/bash
2
+
3
+ runner='irb'
4
+
5
+ if [ `which pry` ]; then
6
+ runner='pry'
7
+ fi
8
+
9
+ $runner -I "$0/../../lib" -r report_logic
@@ -0,0 +1,11 @@
1
+ class MyNestedSessions < ReportLogic::Base
2
+ def build
3
+ session :parent do
4
+ value "I'm a father!"
5
+
6
+ session :child, [1, 2] do
7
+ value "bééééé"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -13,7 +13,7 @@ class MyReport < ReportLogic::Base
13
13
  end
14
14
 
15
15
  session(:test_context) do
16
- context_value
16
+ report.context_value
17
17
  end
18
18
 
19
19
  session(:test_master_decorator) do
@@ -37,4 +37,14 @@ describe ReportLogic::Base do
37
37
  expect(s1.first.value).to eq(3)
38
38
  expect(s2.first.value).to eq(3)
39
39
  end
40
+
41
+ context 'nesting sessions' do
42
+ it 'nests sessions' do
43
+ report = MyNestedSessions.new
44
+ report.build
45
+ expect(report.children.size).to eq(1)
46
+ expect(report.children.values.first.children.size).to eq(1)
47
+ expect(report.children.values.first.children.values.first.fields.size).to eq(2)
48
+ end
49
+ end
40
50
  end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ module ReportLogic
4
+ describe Field do
5
+ it 'stores extra arguments in config' do
6
+ field = Field.new(nil, extra1: :a, extra2: :b)
7
+
8
+ expect(field.config).to eq({ extra1: :a, extra2: :b })
9
+ end
10
+ end
11
+ 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.2
4
+ version: 0.1.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: 2014-09-18 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -44,11 +44,14 @@ files:
44
44
  - lib/report_logic/i18n_support.rb
45
45
  - lib/report_logic/session.rb
46
46
  - report_logic.gemspec
47
+ - script/console
47
48
  - spec/app/my_decorator.rb
48
49
  - spec/app/my_i18n_report.rb
50
+ - spec/app/my_nested_sessions.rb
49
51
  - spec/app/my_report.rb
50
52
  - spec/report_logic/base_spec.rb
51
53
  - spec/report_logic/decorator_spec.rb
54
+ - spec/report_logic/field_spec.rb
52
55
  - spec/report_logic/i18n_support_spec.rb
53
56
  - spec/report_logic/session_spec.rb
54
57
  - spec/spec_helper.rb