decisive 0.1.0 → 0.2.0
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 +4 -4
- data/.gitignore +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +3 -1
- data/README.md +8 -9
- data/lib/decisive/template_handler.rb +26 -16
- data/lib/decisive/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8a160d81797dc99c36ca0d8eb02e977325bc3394
|
4
|
+
data.tar.gz: f99f21ffbea49f1ca3526867a9d7ee29d17e5207
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e16f242edf38c7942d040cef45497b4b3a50ce55a0ef6786fbddf713483e5254c4f78f2071c84861ee2c7eac56baa83cf03ed10309476ee79d917f6e23fa1146
|
7
|
+
data.tar.gz: a8b6ba7952d1155978b1ddf2d707535f0761aa42851a0c126b5faebd39b5f36855ac21c340bf36d73868769c1a993c50653120dc43b329fa7aae581b2efa1fd9
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
decisive (0.
|
4
|
+
decisive (0.2.0)
|
5
5
|
actionview
|
6
6
|
|
7
7
|
GEM
|
@@ -19,6 +19,7 @@ GEM
|
|
19
19
|
minitest (~> 5.1)
|
20
20
|
tzinfo (~> 1.1)
|
21
21
|
builder (3.2.3)
|
22
|
+
byebug (10.0.2)
|
22
23
|
concurrent-ruby (1.0.5)
|
23
24
|
crass (1.0.4)
|
24
25
|
diff-lcs (1.3)
|
@@ -60,6 +61,7 @@ PLATFORMS
|
|
60
61
|
|
61
62
|
DEPENDENCIES
|
62
63
|
bundler (~> 1.16)
|
64
|
+
byebug
|
63
65
|
decisive!
|
64
66
|
rake (~> 10.0)
|
65
67
|
rspec (~> 3.0)
|
data/README.md
CHANGED
@@ -35,21 +35,20 @@ end
|
|
35
35
|
```ruby
|
36
36
|
# app/views/users/index.csv.decisive
|
37
37
|
|
38
|
-
@filename
|
39
|
-
|
40
|
-
|
41
|
-
column :
|
42
|
-
|
43
|
-
|
44
|
-
user.created_at.to_date
|
38
|
+
csv @users, filename: "users-#{Time.zone.now.strftime("%Y_%m_%d")}.csv" do
|
39
|
+
column :email
|
40
|
+
column :name, label: "Full name"
|
41
|
+
column :signed_up do |user|
|
42
|
+
user.created_at.to_date
|
43
|
+
end
|
45
44
|
end
|
46
45
|
```
|
47
46
|
|
48
47
|
Then visit /users.csv to get file named "users-2010_01_01.csv" with the following contents:
|
49
48
|
```csv
|
50
49
|
Email,Full name,Signed up
|
51
|
-
frodo@example.com,
|
52
|
-
sam@example.com,
|
50
|
+
frodo@example.com,Frodo Baggins,2010-01-01
|
51
|
+
sam@example.com,Samwise Gamgee,2010-01-01
|
53
52
|
```
|
54
53
|
|
55
54
|
## Development
|
@@ -11,43 +11,53 @@ module Decisive
|
|
11
11
|
def self.call template
|
12
12
|
<<~RUBY
|
13
13
|
extend Decisive::DSL
|
14
|
-
#{template.source}
|
15
|
-
response.headers["Content-Disposition"] = %(attachment; filename="\#{
|
16
|
-
|
17
|
-
.map { |rows| rows.map(&:to_s) }
|
18
|
-
.map(&:to_csv)
|
19
|
-
.join
|
14
|
+
context = (#{template.source})
|
15
|
+
response.headers["Content-Disposition"] = %(attachment; filename="\#{context.filename}")
|
16
|
+
context.to_csv
|
20
17
|
RUBY
|
21
18
|
end
|
22
19
|
end
|
23
20
|
|
24
21
|
module DSL
|
25
|
-
def
|
26
|
-
|
22
|
+
def csv records, filename:, &block
|
23
|
+
Context.new([], records, filename).tap do |context|
|
24
|
+
context.instance_eval &block
|
25
|
+
end
|
27
26
|
end
|
27
|
+
end
|
28
28
|
|
29
|
+
class Context < Struct.new(:columns, :records, :filename)
|
29
30
|
class Column < Struct.new(:field, :label, :block); end
|
30
31
|
|
31
32
|
def column field, label: field.to_s.humanize, &block
|
32
33
|
block ||= ->(record) { record.send(field) }
|
33
|
-
|
34
|
+
columns << Column.new(field, label, block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_csv
|
38
|
+
rows
|
39
|
+
.map { |rows| rows.map(&:to_s) }
|
40
|
+
.map(&:to_csv)
|
41
|
+
.join
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def rows
|
47
|
+
[header] + body
|
34
48
|
end
|
35
49
|
|
36
50
|
def header
|
37
|
-
|
51
|
+
columns.map(&:label)
|
38
52
|
end
|
39
53
|
|
40
54
|
def body
|
41
|
-
|
42
|
-
|
55
|
+
records.map do |record|
|
56
|
+
columns.map do |column|
|
43
57
|
column.block.call(record)
|
44
58
|
end
|
45
59
|
end
|
46
60
|
end
|
47
|
-
|
48
|
-
def rows
|
49
|
-
[header] + body
|
50
|
-
end
|
51
61
|
end
|
52
62
|
end
|
53
63
|
|
data/lib/decisive/version.rb
CHANGED