Module: Sequel::Plugins::CsvSerializer::ClassMethods
- Defined in:
- lib/sequel/plugins/csv_serializer.rb
Instance Attribute Summary collapse
-
#csv_serializer_opts ⇒ Object
readonly
The default opts to use when serializing model objects to CSV.
Instance Method Summary collapse
-
#array_from_csv(csv, opts = OPTS) ⇒ Object
Attempt to parse an array of instances from the given CSV string.
-
#freeze ⇒ Object
Freeze csv serializier opts when freezing model class.
-
#from_csv(csv, opts = OPTS) ⇒ Object
Attempt to parse a single instance from the given CSV string.
-
#process_csv_serializer_opts(opts) ⇒ Object
Convert the options hash to one that can be passed to CSV.
Instance Attribute Details
#csv_serializer_opts ⇒ Object (readonly)
The default opts to use when serializing model objects to CSV
96 97 98 |
# File 'lib/sequel/plugins/csv_serializer.rb', line 96 def csv_serializer_opts @csv_serializer_opts end |
Instance Method Details
#array_from_csv(csv, opts = OPTS) ⇒ Object
Attempt to parse an array of instances from the given CSV string
99 100 101 102 103 104 105 |
# File 'lib/sequel/plugins/csv_serializer.rb', line 99 def array_from_csv(csv, opts = OPTS) CsvSerializer.csv_call(:parse, csv, process_csv_serializer_opts(opts)).map do |row| row = row.to_hash row.delete(nil) new(row) end end |
#freeze ⇒ Object
Freeze csv serializier opts when freezing model class
108 109 110 111 112 113 114 |
# File 'lib/sequel/plugins/csv_serializer.rb', line 108 def freeze @csv_serializer_opts.freeze.each_value do |v| v.freeze if v.is_a?(Array) || v.is_a?(Hash) end super end |
#from_csv(csv, opts = OPTS) ⇒ Object
Attempt to parse a single instance from the given CSV string
117 118 119 |
# File 'lib/sequel/plugins/csv_serializer.rb', line 117 def from_csv(csv, opts = 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.
122 123 124 125 126 127 128 129 130 |
# File 'lib/sequel/plugins/csv_serializer.rb', line 122 def process_csv_serializer_opts(opts) opts = (csv_serializer_opts || OPTS).merge(opts) opts_cols = opts.delete(:columns) opts_include = opts.delete(:include) opts_except = opts.delete(:except) only = opts.delete(:only) opts[:headers] ||= Array(only || opts_cols || columns) + Array(opts_include) - Array(opts_except) opts end |