decisive 0.1.0 → 0.2.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: 3700264badd95fb4a4cece6399f68caf4f68258f
4
- data.tar.gz: 5971eda2dfeb494f0990e4d96823926d4a0faafa
3
+ metadata.gz: 8a160d81797dc99c36ca0d8eb02e977325bc3394
4
+ data.tar.gz: f99f21ffbea49f1ca3526867a9d7ee29d17e5207
5
5
  SHA512:
6
- metadata.gz: ff8952f02b479c471a23015fdfd969217fcdb4690b4b979d4043698fbf431c64108dddc91af8bd2138af37ae5ebac8d35388e4ad57710274d32f2cbf239ed32c
7
- data.tar.gz: 49702ed4d64bf5ef20c7d408d7a4f2ab56f2cedce256436c0747f8ac89028089826cad4dc60bc3701c78c0353ed52aa42d0f90d8be772d46b3a3806d9a79993d
6
+ metadata.gz: e16f242edf38c7942d040cef45497b4b3a50ce55a0ef6786fbddf713483e5254c4f78f2071c84861ee2c7eac56baa83cf03ed10309476ee79d917f6e23fa1146
7
+ data.tar.gz: a8b6ba7952d1155978b1ddf2d707535f0761aa42851a0c126b5faebd39b5f36855ac21c340bf36d73868769c1a993c50653120dc43b329fa7aae581b2efa1fd9
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /.byebug_history
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
data/Gemfile CHANGED
@@ -4,3 +4,5 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in decisive.gemspec
6
6
  gemspec
7
+
8
+ gem "byebug"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- decisive (0.1.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 = "users-#{Time.zone.now.strftime("%Y_%m_%d")}.csv"
39
- @records = @users
40
-
41
- column :email
42
- column :name, label: "Full name"
43
- column :signed_up do |user|
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,"Frodo Baggins",2010-01-01
52
- sam@example.com,"Samwise Gamgee",2010-01-01
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="\#{@filename}")
16
- rows
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 self.extended object
26
- object.instance_variable_set :@columns, []
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
- @columns << Column.new(field, label, block)
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
- @columns.map(&:label)
51
+ columns.map(&:label)
38
52
  end
39
53
 
40
54
  def body
41
- @records.map do |record|
42
- @columns.map do |column|
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
 
@@ -1,3 +1,3 @@
1
1
  module Decisive
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decisive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel