decisive 0.6.3 → 0.7.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
  SHA256:
3
- metadata.gz: 23b80a2524bbbb5ca658e58f19aace65cc19a912261b32110c7745c72e2cea17
4
- data.tar.gz: b42ea7da05da22d7658b6ce5daf38029a5704f75cea8e6d3b0872a38a1466b65
3
+ metadata.gz: 742329de188507c7ce038ec1ea99f1ba3cea0bb19d2e04c7beefc71508dd6c54
4
+ data.tar.gz: 7afeea4003a213b3d5b4abd385e4fc02c434c9ea7fdcae117ff2688b89aa19ac
5
5
  SHA512:
6
- metadata.gz: 02f001f7d3efdeb06a04890a7504316b0c24665b7438b2571f9f133d7cf7521612f7a807002334313f31b575be9cb9697f2ebe0ce5a11a72f15bf305c271f735
7
- data.tar.gz: a8f98008525bf3537becd12f430c89213f4326e03e1432037486c837d3e94a4c8b499eddfefd6a1ea387b8e1ac45a81f0da2ab79cf0b0c01b3ccc2605fb76182
6
+ metadata.gz: a0f51f83d168340baa2c8d8e430c0f89255ee87cbd95d42e4164843425a3d82354f492923c8a1f7288d28bead52aa25fee7c8fbed5181d6208e86a6930b7e2e8
7
+ data.tar.gz: 237224e0ff57647f4aadd4d6ea5e470378fd8863c6e6a6d73e7e341450e6f0c68b348f89f34ab921ddd23ba74faf4b4dc3e15051379cdea796bfb0cfea8a1e55
@@ -64,11 +64,75 @@ module Decisive
64
64
  end
65
65
  end
66
66
 
67
- def xls worksheets, filename:, &block
68
- XLSContext.new(worksheets, filename, block)
67
+ def xls worksheets=nil, filename:, &block
68
+ if worksheets
69
+ XLSContext.new(worksheets, filename, block)
70
+ else
71
+ XLSWithWorksheetsContext.new(filename, [], &block)
72
+ end
69
73
  end
70
74
  end
71
75
 
76
+ class XLSWithWorksheetsContext < Struct.new(:filename, :worksheets)
77
+ class Worksheet < Struct.new(:records, :name, :block); end
78
+
79
+ def initialize *args, &block
80
+ super
81
+ instance_eval &block
82
+ end
83
+
84
+ def to_xls
85
+ to_string(render(Spreadsheet::Workbook.new))
86
+ end
87
+
88
+ def csv?
89
+ false
90
+ end
91
+
92
+ private
93
+
94
+ def worksheet records, name:, &block
95
+ worksheets.push Worksheet.new(records, name, block)
96
+ end
97
+
98
+ def render xls
99
+ worksheets.each do |worksheet|
100
+ sheet = xls.create_worksheet(name: sanitize_name(worksheet.name))
101
+
102
+ rows = to_array(worksheet)
103
+
104
+ rows.each.with_index do |row, index|
105
+ sheet.row(index).concat row
106
+ end
107
+ end
108
+ xls
109
+ end
110
+
111
+ def sanitize_name name
112
+ name
113
+ .gsub(/[\[\]\*\?:\/\\\t\n\r]/, " ")
114
+ .gsub(/^'/, "")
115
+ .gsub(/'$/, "")
116
+ .strip
117
+ .slice(0,31)
118
+ end
119
+
120
+ def to_array worksheet
121
+ context = RenderContext.new(worksheet.records, nil, worksheet.block)
122
+ context.send(:header) + context.send(:body)
123
+ end
124
+
125
+ def to_string xls
126
+ io = StringIO.new
127
+ xls.write(io)
128
+ io.rewind
129
+ string = io.read
130
+ string.force_encoding(Encoding::ASCII_8BIT)
131
+ string
132
+ end
133
+ end
134
+
135
+
72
136
  class XLSContext < Struct.new(:worksheets, :filename, :block)
73
137
  def to_xls
74
138
  to_string(render(Spreadsheet::Workbook.new))
@@ -1,3 +1,3 @@
1
1
  module Decisive
2
- VERSION = "0.6.3"
2
+ VERSION = "0.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decisive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-12 00:00:00.000000000 Z
11
+ date: 2021-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview