Module: DumbDelimited::ClassMethods
- Defined in:
- lib/dumb_delimited.rb
Instance Method Summary collapse
-
#append(path, models) ⇒ void
Appends a collection of model objects to a file in delimited format.
-
#delimiter ⇒ String
Returns the column delimiter used in IO operations.
-
#delimiter=(delim) ⇒ String
Sets the column delimiter used in IO operations.
-
#options ⇒ Hash<Symbol, Object>
Returns the CSV options Hash.
-
#options=(opts) ⇒ Hash<Symbol, Object>
Sets the CSV options Hash.
-
#parse(data) ⇒ Array<Struct>
(also: #parse_text)
Parses a string or IO object into an array of model objects.
-
#parse_each(data, &block) ⇒ Object
Parses a string or IO object one line at a time, yielding a model object for each line.
-
#parse_line(line) ⇒ Struct
Parses a single delimited line into a model object.
-
#read(path) ⇒ Array<Struct>
(also: #parse_file)
Parses a file into an array of model objects.
-
#read_each(path, &block) ⇒ Object
Parses a file one line at a time, yielding a model object for each line.
-
#write(path, models, append: false) ⇒ void
Writes a collection of model objects to a file in delimited format.
Instance Method Details
#append(path, models) ⇒ void
This method returns an undefined value.
Appends a collection of model objects to a file in delimited format. Convenience shortcut for #write with append: true.
295 296 297 |
# File 'lib/dumb_delimited.rb', line 295 def append(path, models) write(path, models, append: true) end |
#delimiter ⇒ String
Returns the column delimiter used in IO operations. Defaults to a comma (",").
Equivalent to options[:col_sep].
140 141 142 |
# File 'lib/dumb_delimited.rb', line 140 def delimiter self.[:col_sep] end |
#delimiter=(delim) ⇒ String
Sets the column delimiter used in IO operations. The new value will be used in all future IO operations for the model class. Any delimiter can be safely chosen, and all IO operations will quote field values as necessary.
Equivalent to options[:col_sep] = delim.
160 161 162 |
# File 'lib/dumb_delimited.rb', line 160 def delimiter=(delim) self.[:col_sep] = delim end |
#options ⇒ Hash<Symbol, Object>
Returns the CSV options Hash. The Hash is not duped and can be modified directly. Any modifications will be applied to all future IO operations for the model class.
For detailed information about available options, see Ruby’s CSV class.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dumb_delimited.rb', line 109 def @options ||= if superclass == Struct CSV::DEFAULT_OPTIONS.merge( skip_blanks: true, liberal_parsing: true, ) else superclass..dup end end |
#options=(opts) ⇒ Hash<Symbol, Object>
130 131 132 |
# File 'lib/dumb_delimited.rb', line 130 def (opts) @options = opts end |
#parse(data) ⇒ Array<Struct> Also known as: parse_text
Parses a string or IO object into an array of model objects.
189 190 191 |
# File 'lib/dumb_delimited.rb', line 189 def parse(data) parse_each(data).to_a end |
#parse_each(data) {|model| ... } ⇒ void #parse_each(data) ⇒ Enumerator<Struct>
Parses a string or IO object one line at a time, yielding a model object for each line.
An Enumerator is returned if no block is given.
208 209 210 211 212 |
# File 'lib/dumb_delimited.rb', line 208 def parse_each(data, &block) return to_enum(__method__, data) unless block_given? csv_each(CSV.new(data, self.), &block) end |
#parse_line(line) ⇒ Struct
Parses a single delimited line into a model object.
172 173 174 |
# File 'lib/dumb_delimited.rb', line 172 def parse_line(line) parse_each(line).first end |
#read(path) ⇒ Array<Struct> Also known as: parse_file
Parses a file into an array of model objects. This will load the entire contents of the file into memory, and may not be suitable for large files. To iterate over file contents without loading it all into memory at once, use #read_each.
235 236 237 |
# File 'lib/dumb_delimited.rb', line 235 def read(path) read_each(path).to_a end |
#read_each(path) {|model| ... } ⇒ void #read_each(path) ⇒ Enumerator<Struct>
Parses a file one line at a time, yielding a model object for each line. This avoids loading the entire contents of the file into memory at once.
An Enumerator is returned if no block is given. Note that some Enumerator methods, such as Enumerator#to_a, can cause the entire contents of the file to be loaded into memory.
257 258 259 260 261 262 263 |
# File 'lib/dumb_delimited.rb', line 257 def read_each(path, &block) return to_enum(__method__, path) unless block_given? CSV.open(path, self.) do |csv| csv_each(csv, &block) end end |
#write(path, models, append: false) ⇒ void
This method returns an undefined value.
Writes a collection of model objects to a file in delimited format. The previous contents of the file are overwritten, unless append is set to true.
Column headers are written to the file if :write_headers in #options is set to true and either append is false or the file is empty / non-existent. The column headers will be derived from either the value of :headers in #options if it is an Array, or otherwise from the columns defined by the model.
279 280 281 282 283 284 285 286 287 |
# File 'lib/dumb_delimited.rb', line 279 def write(path, models, append: false) mode = append ? "a" : "w" write_headers = [:write_headers] && !(append && File.exist?(path) && File.size(path) > 0) headers = (![:headers].is_a?(Array) && write_headers) ? members : [:headers] CSV.open(path, mode, **, write_headers: write_headers, headers: headers) do |csv| models.each{|model| csv << model } end end |