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



85
86
87
# File 'lib/sequel/plugins/csv_serializer.rb', line 85

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



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

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



97
98
99
# File 'lib/sequel/plugins/csv_serializer.rb', line 97

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.



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

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