Class: AsCSV::CSVBuilder
- Inherits:
-
Object
- Object
- AsCSV::CSVBuilder
- Defined in:
- lib/as_csv/csv_builder.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
Instance Method Summary collapse
- #data_rows ⇒ Object
- #headers ⇒ Object
-
#initialize(records, options = {}) ⇒ CSVBuilder
constructor
A new instance of CSVBuilder.
- #rows ⇒ Object
- #to_csv ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(records, options = {}) ⇒ CSVBuilder
Returns a new instance of CSVBuilder.
5 6 7 8 |
# File 'lib/as_csv/csv_builder.rb', line 5 def initialize(records, ={}) @records = [*records] @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/as_csv/csv_builder.rb', line 3 def @options end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
3 4 5 |
# File 'lib/as_csv/csv_builder.rb', line 3 def records @records end |
Instance Method Details
#data_rows ⇒ Object
22 23 24 |
# File 'lib/as_csv/csv_builder.rb', line 22 def data_rows @data_rows ||= csv_hashes.collect { |csv_hash| data_row csv_hash } if valid? end |
#headers ⇒ Object
18 19 20 |
# File 'lib/as_csv/csv_builder.rb', line 18 def headers @headers ||= csv_hashes.collect(&:keys).flatten.uniq if valid? end |
#rows ⇒ Object
14 15 16 |
# File 'lib/as_csv/csv_builder.rb', line 14 def rows @rows ||= [headers] + data_rows if valid? end |
#to_csv ⇒ Object
10 11 12 |
# File 'lib/as_csv/csv_builder.rb', line 10 def to_csv rows.collect { |row| CSVProxy.generate_line row }.join if valid? end |
#valid? ⇒ Boolean
26 27 28 |
# File 'lib/as_csv/csv_builder.rb', line 26 def valid? csv_hashes.any? && validate_hashes! end |