Class: Cbuilder
- Inherits:
- ActiveSupport::BasicObject
- Defined in:
- lib/cbuilder.rb
Direct Known Subclasses
Class Method Summary collapse
-
.encode(*args) {|cbuilder| ... } ⇒ Object
Yields a builder and automatically turns the result into a CSV file.
Instance Method Summary collapse
-
#initialize ⇒ Cbuilder
constructor
A new instance of Cbuilder.
- #set!(column, value) ⇒ Object (also: #col)
- #set_collection!(collection) ⇒ Object
-
#target! ⇒ Object
Encodes the current builder as CSV.
Constructor Details
#initialize ⇒ Cbuilder
Returns a new instance of Cbuilder.
14 15 16 |
# File 'lib/cbuilder.rb', line 14 def initialize @attributes, @headers = ::Array.new, ::Array.new end |
Class Method Details
.encode(*args) {|cbuilder| ... } ⇒ Object
Yields a builder and automatically turns the result into a CSV file
8 9 10 11 12 |
# File 'lib/cbuilder.rb', line 8 def self.encode(*args) cbuilder = new(*args) yield cbuilder cbuilder.target! end |
Instance Method Details
#set!(column, value) ⇒ Object Also known as: col
18 19 20 21 |
# File 'lib/cbuilder.rb', line 18 def set!(column, value) @headers.push column unless @headers.include?(column) @attributes.push value end |
#set_collection!(collection) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/cbuilder.rb', line 24 def set_collection!(collection) @collection = collection @attributes = if ::Kernel::block_given? _map_collection(collection) { |element| if ::Proc.new.arity == 2 then yield self, element else yield element end } else collection end end |
#target! ⇒ Object
Encodes the current builder as CSV.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/cbuilder.rb', line 34 def target! if ::RUBY_VERSION > '1.9' ::CSV.generate do |csv| csv << @headers # header row @attributes.each do |element| # body rows csv << (element.nil? ? '' : element) end end else FasterCSV.generate do |csv| csv << @headers # pop header row @attributes.each do |element| # body rows csv << (element.nil? ? '' : element) end end end end |