Class: Cbuilder
- Inherits:
-
CbuilderProxy
- Object
- CbuilderProxy
- Cbuilder
- 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.
24 25 26 |
# File 'lib/cbuilder.rb', line 24 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
18 19 20 21 22 |
# File 'lib/cbuilder.rb', line 18 def self.encode(*args) cbuilder = new(*args) yield cbuilder cbuilder.target! end |
Instance Method Details
#set!(column, value) ⇒ Object Also known as: col
28 29 30 31 |
# File 'lib/cbuilder.rb', line 28 def set!(column, value) @headers.push column unless @headers.include?(column) @attributes.push value end |
#set_collection!(collection) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/cbuilder.rb', line 34 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.
44 45 46 47 48 49 50 51 |
# File 'lib/cbuilder.rb', line 44 def target! ::CbuilderCSVProxy.generate do |csv| csv << @headers # header row @attributes.each do |element| # body rows csv << (element.nil? ? '' : element) end end end |