simple_report 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f98c990ab100b1bbbbfe334f4ece710ed895f682
4
- data.tar.gz: b4f0681aaddd82c82a6731ba74670355b622cf1d
3
+ metadata.gz: '059e4443b7a7da77afb6aaf74a997fd3a701b309'
4
+ data.tar.gz: 6c0bcf4155da5e41b12f6394f31bcbbc4317956a
5
5
  SHA512:
6
- metadata.gz: f506009a364c546294118f3d5d97ebae95c70ff4b0d40aec84391c3fd47aba6335ffcaed42b113d2e6b8d174a529013c5e94bb1e5de3b726fa83cf2b544200ec
7
- data.tar.gz: 4a62e6e447fee563d3befd74bc28cfc03ba311c4cec22fdecd14346f3b53bb60bd45c8a649dc898eeca7a5cdad7f9de6f131929a054de990cc960385a1b4844d
6
+ metadata.gz: 441965e971b584be1c63a6e00779ee6032f7a6f4fb61a00d64f32f1a5f546230ba4506f60506ccaa9facc5389d396b096d80ce618dc064d0b802044eae83c5bd
7
+ data.tar.gz: 4163d38ba55e7f7a18f262ccee4ce4234f712b35d59373971a277e78de3f89371a4faf25921607ec669a9bb75f4dfb96a0bc84ff24f3a11bfb6ee93d4e35c31e
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # SimpleReports
1
+ # simple_report
2
2
 
3
3
  [![Build Status](https://travis-ci.org/lostapathy/simple_report.svg?branch=master)](https://travis-ci.org/lostapathy/simple_report)
4
4
  [![Code Climate](https://api.codeclimate.com/v1/badges/04ced70d7b66d1a7c42d/maintainability)](https://codeclimate.com/github/lostapathy/simple_report)
@@ -1,28 +1,17 @@
1
1
  module SimpleReport
2
2
  class Base
3
+ def initialize
4
+ @sheets = []
5
+ end
6
+
3
7
  def add_format(name, format)
4
8
  @formats ||= {}
5
9
  @formats[name] = format
6
10
  end
7
11
 
8
- def initialize
9
- @file = Tempfile.new('simple_report')
10
- @workbook = WriteXLSX.new(@file.path)
11
- money = @workbook.add_format
12
- money.set_num_format('$0.00')
13
- add_format :money, money
14
-
15
- heading = @workbook.add_format
16
- heading.set_bold
17
- add_format :heading, heading
18
-
19
- percent = @workbook.add_format
20
- percent.set_num_format('0%')
21
- add_format :percent, percent
22
- end
23
-
24
12
  def report_xls(*params)
25
13
  report(*params)
14
+ generate_report
26
15
  @workbook.close
27
16
  File.read @file.path
28
17
  end
@@ -30,41 +19,52 @@ module SimpleReport
30
19
  private
31
20
 
32
21
  def add_sheet(name, data)
33
- sheet = Sheet.new(name)
34
- @fields = []
35
- sheet = @workbook.add_worksheet(name)
36
- yield
37
- @sheets ||= []
22
+ sheet = Sheet.new(name, data)
23
+ yield sheet
38
24
  @sheets << sheet
25
+ end
39
26
 
40
- @fields.each_with_index do |f, index|
41
- sheet.set_column(index, index, f.width)
42
- sheet.write(0, index, f.name, @formats[:heading])
43
- end
44
-
45
- data.each_with_index do |ic, row|
46
- @fields.each_with_index do |field, column|
47
- if field.field
48
- value = ic.send(field.field)
49
- elsif field.value
50
- value = field.value
51
- elsif field.block
52
- value = field.block.call(ic)
27
+ def generate_report
28
+ @file = Tempfile.new('simple_report')
29
+ @workbook = WriteXLSX.new(@file.path)
30
+ add_formats
31
+ @sheets.each do |sheet|
32
+ output_sheet = @workbook.add_worksheet(sheet.name)
33
+ sheet.fields.each_with_index do |f, index|
34
+ output_sheet.set_column(index, index, f.width)
35
+ output_sheet.write(0, index, f.name, @formats[:heading])
36
+ end
37
+ sheet.collection.each_with_index do |ic, row|
38
+ sheet.fields.each_with_index do |field, column|
39
+ if field.field
40
+ value = ic.send(field.field)
41
+ elsif field.value
42
+ value = field.value
43
+ elsif field.block
44
+ value = field.block.call(ic)
45
+ end
46
+ output_sheet.write(row + 1, column, value, find_format(field.format))
53
47
  end
54
- sheet.write(row + 1, column, value, find_format(field.format))
55
48
  end
56
49
  end
57
50
  end
58
51
 
59
- def field(name, field = nil, width: nil, format: nil, value: nil, &block)
60
- rf = Field.new(name, field, width: width, format: format, value: value, &block)
61
- @fields << rf
52
+ def find_format(format)
53
+ @formats[format]
62
54
  end
63
55
 
64
- private
56
+ def add_formats
57
+ money = @workbook.add_format
58
+ money.set_num_format('$0.00')
59
+ add_format :money, money
65
60
 
66
- def find_format(format)
67
- @formats[format]
61
+ heading = @workbook.add_format
62
+ heading.set_bold
63
+ add_format :heading, heading
64
+
65
+ percent = @workbook.add_format
66
+ percent.set_num_format('0%')
67
+ add_format :percent, percent
68
68
  end
69
69
  end
70
70
  end
@@ -1,7 +1,15 @@
1
1
  module SimpleReport
2
2
  class Sheet
3
- def initialize(name)
3
+ attr_reader :name, :collection, :fields
4
+
5
+ def initialize(name, collection)
4
6
  @name = name
7
+ @collection = collection
8
+ @fields = []
9
+ end
10
+
11
+ def add_field(name, field = nil, width: nil, format: nil, value: nil, &block)
12
+ @fields << Field.new(name, field, width: width, format: format, value: value, &block)
5
13
  end
6
14
  end
7
15
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleReport
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/simple_report.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require "write_xlsx"
1
2
  require "simple_report/version"
2
3
  require "simple_report/base"
3
4
  require "simple_report/sheet"
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
- f.match(%r{^(test|spec|features)/})
18
+ f.match(%r{^(test|spec|features)/}) || f.match(%r{gem$})
19
19
  end
20
20
  spec.bindir = "exe"
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_report
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Francis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-14 00:00:00.000000000 Z
11
+ date: 2017-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler