cbuilder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cbuilder.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nate Berkopec
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # cbuilder
2
+
3
+ Cbuilder is an ultralight (< 100 LOC) CSV builder/template handler inspired by dhh's excellent
4
+ Jbuilder. The goal is a simple DSL for creating CSV files that's better than creating
5
+ massive custom arrays.
6
+
7
+ If you're doing any more than 4-5 columns, generating CSV files can be a real pain.
8
+ God forbid you're not simply outputting the attributes of a simple array, and need
9
+ helpers or other niceties.
10
+
11
+ You don't need to re-invent the wheel with CSV. Cbuilder weighs in at less than 100 LOC.
12
+
13
+ Have you ever found yourself doing this?
14
+
15
+ ```ruby
16
+ Customer Name,Products Purchased, Product Names
17
+ <% @orders.each do |order|%>
18
+ <%= order.customer.name %>,<%= products_count(order) %>,<%= product_names(order.products) %>,...
19
+ <% end %>
20
+ ```
21
+
22
+ What if you could do this instead?
23
+
24
+ ```ruby
25
+ Cbuilder.encode do |csv|
26
+ csv.set_collection!(@orders) do |order|
27
+ csv.col 'Customer Name', order.customer.name
28
+ csv.col 'Products Purchased', products_count(order)
29
+ csv.col 'Product Names', product_names(order.products)
30
+ end
31
+ end
32
+ ```
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ gem 'cbuilder'
39
+
40
+ And then execute:
41
+
42
+ $ bundle
43
+
44
+ Or install it yourself as:
45
+
46
+ $ gem install cbuilder
47
+
48
+ ## Usage
49
+
50
+ Just drop into your Rails Gemfile and start adding templates with *.csv.cbuilder.
51
+
52
+ If you'd like the CSV to be downloaded as an attachment, add the following to your
53
+ controller action:
54
+
55
+ ```ruby
56
+ response.headers["Content-Disposition"] = 'attachment; filename="my_spreadsheet.csv'
57
+ ```
58
+
59
+ ## TODO
60
+
61
+ * Test Ruby 1.8 support
62
+ * Test use as a partial
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create new Pull Request
71
+
72
+ ## Credits
73
+
74
+ ![http://i.imgur.com/Jd7pg.jpg](http://i.imgur.com/Jd7pg.jpg)
75
+
76
+ Cbuilder was built by [Craft Coffee](http://craftcoffee.com)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ task :test do
5
+ system 'bundle exec ruby test/cbuilder_test.rb'
6
+ end
data/cbuilder.gemspec ADDED
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.name = "cbuilder"
5
+ gem.version = "0.0.1"
6
+ gem.authors = ["Nate Berkopec"]
7
+ gem.email = ["nate.berkopec@gmail.com"]
8
+ gem.summary = "Create CSV structures with a Builder-style DSL"
9
+ gem.homepage = "http://github.com/nateberkopec/cbuilder"
10
+
11
+ gem.add_dependency 'activesupport', '>= 3.0.0'
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.require_paths = ["lib"]
17
+ end
data/lib/cbuilder.rb ADDED
@@ -0,0 +1,74 @@
1
+ require 'active_support/basic_object'
2
+ require 'active_support/core_ext/array/access'
3
+ require 'active_support/core_ext/enumerable'
4
+ require 'csv'
5
+
6
+ class Cbuilder < ActiveSupport::BasicObject
7
+ #Yields a builder and automatically turns the result into a CSV file
8
+ def self.encode(*args)
9
+ cbuilder = new(*args)
10
+ yield cbuilder
11
+ cbuilder.target!
12
+ end
13
+
14
+ def initialize
15
+ @attributes, @headers = ::Array.new, ::Array.new
16
+ end
17
+
18
+ def set!(column, value)
19
+ @headers.push column unless @headers.include?(column)
20
+ @attributes.push value
21
+ end
22
+ alias_method :col, :set!
23
+
24
+ def set_collection!(collection)
25
+ @collection = collection
26
+ @attributes = if ::Kernel::block_given?
27
+ _map_collection(collection) { |element| if ::Proc.new.arity == 2 then yield self, element else yield element end }
28
+ else
29
+ collection
30
+ end
31
+ end
32
+
33
+ # Encodes the current builder as CSV.
34
+ def target!
35
+ if ::RUBY_VERSION > '1.9'
36
+ ::CSV.generate do |csv|
37
+ csv << @headers # header row
38
+ @attributes.each do |element| # body rows
39
+ csv << (element.nil? ? '' : element)
40
+ end
41
+ end
42
+ else
43
+ FasterCSV.generate do |csv|
44
+ csv << @headers # pop header row
45
+ @attributes.each do |element| # body rows
46
+ csv << (element.nil? ? '' : element)
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ private
53
+ def _evaluate_for(element, values)
54
+ values.map { |value| element.send(value)}
55
+ end
56
+
57
+ def _map_collection(collection)
58
+ collection.each.map do |element|
59
+ _scope { yield element }
60
+ end
61
+ end
62
+
63
+ def _scope
64
+ parent_attributes = @attributes
65
+ @attributes = ::Array.new
66
+ yield
67
+ @attributes
68
+ ensure
69
+ @attributes= parent_attributes
70
+ end
71
+
72
+ end
73
+
74
+ require 'cbuilder_template' if defined? ActionView::Template
@@ -0,0 +1,29 @@
1
+ class CbuilderTemplate < Cbuilder
2
+ def initialize(context, *args)
3
+ @context = context
4
+ super(*args)
5
+ end
6
+
7
+ def partial!(options, locals = {})
8
+ case options
9
+ when Hash
10
+ options[:locals] ||= {}
11
+ options[:locals].merge!(:csv => self)
12
+ @context.render(options.reverse_merge(:formats => [:csv]))
13
+ else
14
+ @context.render(:partial => options, :locals => locals.merge(:csv => self), :formats => [:csv])
15
+ end
16
+ end
17
+ end
18
+
19
+ class CbuilderHandler
20
+ cattr_accessor :default_format
21
+ self.default_format = Mime::CSV
22
+
23
+ def self.call(template)
24
+ %{__already_defined = defined?(csv); csv||=CbuilderTemplate.new(self); #{template.source}
25
+ csv.target! unless __already_defined}
26
+ end
27
+ end
28
+
29
+ ActionView::Template.register_template_handler :cbuilder, CbuilderHandler
@@ -0,0 +1,71 @@
1
+ require 'test/unit'
2
+ require 'active_support/test_case'
3
+
4
+ require 'cbuilder'
5
+
6
+ class CbuilderTest < ActiveSupport::TestCase
7
+
8
+ @@orders = [
9
+ Struct.new(:number, :name).new(1138, 'Joe Schmo'),
10
+ Struct.new(:number, :name).new(6875309, 'Nate Berkopec')
11
+ ]
12
+
13
+ test "single column" do
14
+ csv = Cbuilder.encode do |csv|
15
+ csv.set_collection!(@@orders) do |order|
16
+ csv.col 'Order Number', order.number
17
+ end
18
+ end
19
+
20
+ assert_equal ["Order Number"], CSV.parse(csv)[0]
21
+ assert_equal ["1138"], CSV.parse(csv)[1]
22
+ end
23
+
24
+ test "single col with nil value" do
25
+ csv = Cbuilder.encode do |csv|
26
+ csv.set_collection!(@@orders) do |order|
27
+ csv.col 'Order Number', nil
28
+ end
29
+ end
30
+
31
+ assert CSV.parse(csv)[0] == ["Order Number"]
32
+ assert_equal [], CSV.parse(csv)[1]
33
+ end
34
+
35
+ test "multiple cols" do
36
+ csv = Cbuilder.encode do |csv|
37
+ csv.set_collection!(@@orders) do |order|
38
+ csv.col "Order Number", order.number
39
+ csv.col "Name", order.name
40
+ end
41
+ end
42
+
43
+ CSV.parse(csv).tap do |parsed|
44
+ assert_equal ["1138", "Joe Schmo"], parsed[1]
45
+ assert_equal ["6875309", "Nate Berkopec"], parsed[2]
46
+ end
47
+ end
48
+
49
+ test "protects commas" do
50
+ orders = [Struct.new(:number, :name).new(1138, 'Joe, Schmo')]
51
+ csv = Cbuilder.encode do |csv|
52
+ csv.set_collection!(orders) do |order|
53
+ csv.col "Name", order.name
54
+ end
55
+ end
56
+
57
+ assert_equal ["Joe, Schmo"], CSV.parse(csv)[1]
58
+ end
59
+
60
+ test "escaping quotes" do
61
+ orders = [Struct.new(:number, :name).new(1138, %{"Joe Schmo"})]
62
+ csv = Cbuilder.encode do |csv|
63
+ csv.set_collection!(orders) do |order|
64
+ csv.col "Name", order.name
65
+ end
66
+ end
67
+
68
+ assert_equal [%{"Joe Schmo"}], CSV.parse(csv)[1]
69
+ end
70
+
71
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cbuilder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nate Berkopec
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ description:
31
+ email:
32
+ - nate.berkopec@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - cbuilder.gemspec
43
+ - lib/cbuilder.rb
44
+ - lib/cbuilder_template.rb
45
+ - test/cbuilder_test.rb
46
+ homepage: http://github.com/nateberkopec/cbuilder
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.23
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Create CSV structures with a Builder-style DSL
70
+ test_files:
71
+ - test/cbuilder_test.rb