Module: DataShift::ColumnPacker
- Includes:
- Delimiters
- Included in:
- CSV, CsvExporter, ExcelExporter
- Defined in:
- lib/datashift/column_packer.rb
Instance Method Summary collapse
-
#escape_for_csv(value) ⇒ Object
Ensure a value is written to CSV correctly TODO - better ways ?? - see transcoding and String#encode.
-
#escape_text_delim ⇒ Object
Return opposite of text delim - “hello, ‘barry’” => ‘“hello, ”barry“”’.
-
#record_to_column(record, options = {}) ⇒ Object
Convert an AR instance to a single column e.g User : “:name = > ‘tom’, :role => ‘developer’”.
-
#record_to_csv(record, options = {}) ⇒ Object
Convert an AR instance to a set of CSV columns.
- #to_headers(records, associations = nil, options = {}) ⇒ Object
Methods included from Delimiters
attribute_list_end, attribute_list_end=, attribute_list_start, attribute_list_start=, column_delim, csv_delim, csv_delim=, eol, key_value_sep, key_value_sep=, multi_assoc_delim, multi_facet_delim, multi_value_delim, name_value_delim, set_column_delim, set_csv_delim, set_multi_assoc_delim, set_multi_value_delim, set_name_value_delim, setmulti_facet_delim, #text_delim, #text_delim=
Instance Method Details
#escape_for_csv(value) ⇒ Object
Ensure a value is written to CSV correctly TODO - better ways ?? - see transcoding and String#encode
25 26 27 28 29 30 |
# File 'lib/datashift/column_packer.rb', line 25 def escape_for_csv(value) text = value.to_s.gsub(text_delim, escape_text_delim()).gsub("\n", "\\n") text = "#{text_delim}#{text}#{text_delim}" if(text.include?(Delimiters::csv_delim)) text end |
#escape_text_delim ⇒ Object
Return opposite of text delim - “hello, ‘barry’” => ‘“hello, ”barry“”’
16 17 18 19 |
# File 'lib/datashift/column_packer.rb', line 16 def escape_text_delim return '"' if text_delim == "\'" "\'" end |
#record_to_column(record, options = {}) ⇒ Object
Convert an AR instance to a single column
e.g User : ":name = > 'tom', :role => 'developer'"
OPTIONS
with_only Specify (as symbols) columns for association types to export
json: Export association data in single column in JSON format
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/datashift/column_packer.rb', line 70 def record_to_column(record, = {}) return "" if(record.nil? || (record.respond_to?(:each) && record.empty?) ) with_only = *[:with_only] ? [*[:with_only]] : nil return record.to_json if([:json] && !with_only) # packs associations into single column if( record.respond_to?(:each) ) return "" if(record.empty?) data = [] record.each { |r| data << record_to_column(r, ); } if([:json]) return data.to_json else return "#{data.join(Delimiters::multi_assoc_delim)}" end else data = [:json] ? {} : [] record.serializable_hash.each do |name, value| next if(with_only && !with_only.include?( name.to_sym ) ) if([:json]) data[name] = value else data << "#{name.to_sym} #{Delimiters::key_value_sep} #{value.to_s.gsub(text_delim, escape_text_delim)}" end end if([:json])# return data.to_json else "#{Delimiters::attribute_list_start}#{data.join(Delimiters::multi_value_delim)}#{Delimiters::attribute_list_end}" end end end |
#record_to_csv(record, options = {}) ⇒ Object
Convert an AR instance to a set of CSV columns
118 119 120 121 122 123 124 |
# File 'lib/datashift/column_packer.rb', line 118 def record_to_csv(record, = {}) csv_data = record.serializable_hash.values.collect { |value| escape_for_csv(value) } [*[:methods]].each { |x| csv_data << escape_for_csv(record.send(x)) if(record.respond_to?(x)) } if([:methods]) csv_data.join( Delimiters::csv_delim ) end |
#to_headers(records, associations = nil, options = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/datashift/column_packer.rb', line 33 def to_headers( records, associations = nil, = {} ) return if( !records.first.is_a?(ActiveRecord::Base) || records.empty?) only = *[:only] ? [*[:only]] : nil headers =[] if associations details_mgr = DataShift::MethodDictionary.method_details_mgrs[records.first.class] [*associations].each do |a| details_mgr.get_list(a).each do |md| next if(only && !only.include?( md.name.to_sym ) ) headers << "#{md.operator}" end end if(details_mgr) else headers = records.first.class.columns.collect( &:name ) end headers end |