Module: Sequel::Plugins::CsvSerializer::ClassMethods

Defined in:
lib/sequel/plugins/csv_serializer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#csv_serializer_optsObject (readonly)

The default opts to use when serializing model objects to CSV



87
88
89
# File 'lib/sequel/plugins/csv_serializer.rb', line 87

def csv_serializer_opts
  @csv_serializer_opts
end

Instance Method Details

#array_from_csv(csv, opts = {}) ⇒ Object

Attempt to parse an array of instances from the given CSV string



90
91
92
93
94
95
96
# File 'lib/sequel/plugins/csv_serializer.rb', line 90

def array_from_csv(csv, opts = {})
  CSV.parse(csv, process_csv_serializer_opts(opts)).map do |row|
    row = row.to_hash
    row.delete(nil)
    new(row)
  end
end

#from_csv(csv, opts = {}) ⇒ Object

Attempt to parse a single instance from the given CSV string



99
100
101
# File 'lib/sequel/plugins/csv_serializer.rb', line 99

def from_csv(csv, opts = {})
  new.from_csv(csv, opts)
end

#process_csv_serializer_opts(opts) ⇒ Object

Convert the options hash to one that can be passed to CSV.



104
105
106
107
108
109
110
111
# File 'lib/sequel/plugins/csv_serializer.rb', line 104

def process_csv_serializer_opts(opts)
  opts = (csv_serializer_opts || {}).merge(opts)
  opts_cols = opts.delete(:columns)
  opts_include = opts.delete(:include)
  opts_except = opts.delete(:except)
  opts[:headers] ||= Array(opts.delete(:only) || opts_cols || columns) + Array(opts_include) - Array(opts_except)
  opts
end