report_logic 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5e25770ff6e1144dbbdba31d2cd5ce7c18b754fc
4
+ data.tar.gz: 01b93068112f0428369d7bf05f7c595cfc483e71
5
+ SHA512:
6
+ metadata.gz: e7cc1c97d098b1595c8799f45c1d69954976b6628655c9868e30eff3038c1ba33a9680b41ba919a71c50256e0c49b455ec9dc2391214b3d507589b5476ff5ed4
7
+ data.tar.gz: fff161688583ee20d397030165b647b15e3b419ee196d81892e311b717fb0b38b264975a05f645394053bf0e190ac784ed2898734d04140e54b981838b4f4c5a
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.2
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # ReportLogic
2
+
3
+ This gem provides an easy way to generate reports' logic.
4
+ Using this, you can so export this report to any format you want.
5
+
6
+ ## Installation
7
+
8
+ ```bash
9
+ gem install report_logic
10
+ ```
11
+
12
+ Using bundler:
13
+
14
+ ```ruby
15
+ gem 'report_logic'
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ Building...
@@ -0,0 +1,59 @@
1
+ module ReportLogic
2
+ class Base
3
+ include Decorable
4
+
5
+ attr_reader :collection
6
+
7
+ def initialize(collection = nil)
8
+ @collection = collection
9
+ end
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
+ end
21
+
22
+ def count
23
+ @collection.size
24
+ end
25
+
26
+ def method_missing(method_name, *args, &block)
27
+ if @current_session && @current_session.public_methods.include?(method_name)
28
+ @current_session.public_send(method_name, *args, &block)
29
+ else
30
+ super
31
+ end
32
+ end
33
+
34
+ protected
35
+
36
+ def ensure_built_and_decorated
37
+ build unless @built
38
+ decorate unless @decorated
39
+ @built = true
40
+ @decorated = true
41
+ end
42
+
43
+ def build
44
+ raise NotImplementedError
45
+ end
46
+
47
+ def decorate
48
+ sessions.values.each do |sess|
49
+ sess.decorate(@decorators)
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def sessions
56
+ @sessions ||= {}
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,17 @@
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
@@ -0,0 +1,26 @@
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
@@ -0,0 +1,35 @@
1
+ module ReportLogic
2
+ class Field
3
+ include Decorable
4
+
5
+ attr_accessor :key, :value, :type, :name
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)
12
+ self.decorate_with(decorate_with)
13
+ end
14
+
15
+ def type
16
+ @type ||= guess_type
17
+ end
18
+
19
+ def guess_type
20
+ @value.class
21
+ end
22
+
23
+ def decorate(master_decorators = nil)
24
+ apply_decorators(master_decorators) if master_decorators
25
+ apply_decorators(decorators)
26
+ end
27
+
28
+ def apply_decorators(decorators)
29
+ decorators.each do |dec|
30
+ dec.decorate_if_matches(self)
31
+ end
32
+ self
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,14 @@
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
@@ -0,0 +1,7 @@
1
+ module ReportLogic
2
+ module I18nSupport
3
+ def i18n_decorate
4
+ decorators << I18nDecorator.new
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,51 @@
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
@@ -0,0 +1,10 @@
1
+ module ReportLogic
2
+ autoload :Base , 'report_logic/base'
3
+ autoload :Decorable , 'report_logic/decorable'
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'
10
+ end
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'report_logic'
3
+ s.version = '0.0.1'
4
+ s.date = '2014-09-17'
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
+ s.email = 'diegoselzlein@gmail.com'
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+ s.require_paths = ["lib"]
13
+
14
+ s.homepage = 'https://github.com/nerde/report_logic'
15
+ s.license = 'MIT'
16
+
17
+ s.add_dependency('i18n', '~> 0')
18
+ end
@@ -0,0 +1,5 @@
1
+ class MyDecorator < ReportLogic::Decorator
2
+ def decorate(field)
3
+ field.value = 3
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ class MyI18nReport < ReportLogic::Base
2
+ include ReportLogic::I18nSupport
3
+
4
+ def build
5
+ i18n_decorate
6
+
7
+ session(:header) do
8
+ field 'ID'
9
+ field :name, :test
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ Record = Struct.new(:id, :name)
2
+
3
+ class MyReport < ReportLogic::Base
4
+ def build
5
+ session(:header) do
6
+ field 'ID'
7
+ field 'Name'
8
+ end
9
+
10
+ session(:rows, collection) do |record|
11
+ value record.id
12
+ value record.name
13
+ end
14
+
15
+ session(:test_context) do
16
+ context_value
17
+ end
18
+ end
19
+
20
+ def context_value
21
+ 'A'
22
+ end
23
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe ReportLogic::Base do
4
+ let(:rec1) { Record.new(1, 'Diego') }
5
+ let(:rec2) { Record.new(2, 'Andressa') }
6
+
7
+ let(:report) { MyReport.new([rec1, rec2]) }
8
+
9
+ let(:headers) { report.each(:header) }
10
+ let(:rows) { report.each(:rows) }
11
+
12
+ it "generates grouped fields" do
13
+ expect(headers.count ).to eql(2)
14
+ expect(headers.next.name).to eql('ID')
15
+ expect(headers.next.name).to eql('Name')
16
+
17
+
18
+ expect(rows.count ).to eql(2)
19
+
20
+ first_row = rows.next
21
+ expect(first_row.first.value).to eql(1)
22
+ expect(first_row.last.value ).to eql('Diego')
23
+
24
+ last_row = rows.next
25
+ expect(last_row.first.value ).to eql(2)
26
+ expect(last_row.last.value ).to eql('Andressa')
27
+ end
28
+
29
+ it "counts records" do
30
+ expect(report.count).to eq 2
31
+ end
32
+ end
@@ -0,0 +1,49 @@
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
@@ -0,0 +1,27 @@
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
@@ -0,0 +1,73 @@
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
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../../lib/report_logic', __FILE__)
2
+
3
+ Dir[File.expand_path('../app/*.rb', __FILE__)].each { |f| require f }
4
+
5
+ RSpec.configure do |config|
6
+ config.order = "random"
7
+ config.before(:each) { GC.disable }
8
+ config.after(:each) { GC.enable }
9
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: report_logic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Diego Aguir Selzlein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: This gem provides an easy way to generate reports' logic. Using this,
28
+ you can so export this report to any format you want.
29
+ email: diegoselzlein@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".ruby-version"
36
+ - README.md
37
+ - lib/report_logic.rb
38
+ - lib/report_logic/base.rb
39
+ - lib/report_logic/decorable.rb
40
+ - lib/report_logic/decorator.rb
41
+ - lib/report_logic/field.rb
42
+ - lib/report_logic/i18n_decorator.rb
43
+ - lib/report_logic/i18n_helper.rb
44
+ - lib/report_logic/i18n_support.rb
45
+ - lib/report_logic/session.rb
46
+ - report_logic.gemspec
47
+ - spec/app/my_decorator.rb
48
+ - spec/app/my_i18n_report.rb
49
+ - spec/app/my_report.rb
50
+ - spec/report_logic/base_spec.rb
51
+ - spec/report_logic/decorator_spec.rb
52
+ - spec/report_logic/i18n_support_spec.rb
53
+ - spec/report_logic/session_spec.rb
54
+ - spec/spec_helper.rb
55
+ homepage: https://github.com/nerde/report_logic
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.2.2
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Generating report logic.
79
+ test_files: []