csv_madness 0.0.9 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f4f9b06af750b458f7eb43b10053dfce2b60a69cc55ea4410d4466c72680f0c
4
- data.tar.gz: 23ddbf472f0b52973769e21eb72939622bee0e5b13e43e8c292ae28203106145
3
+ metadata.gz: 42f85a8b4ddb7980a36703eb43e7ecd8cb3c03d7c178ed0f52cc94ad95a2df74
4
+ data.tar.gz: 395986cb7c3b79a131b419d0325667b538d77920fd2cca254ab0a2dcbcb2676c
5
5
  SHA512:
6
- metadata.gz: 1e44f1666c4118cf68a16f254ea5ad8808f9a0036b346755d187c74baf24a65cff718323d81741cd5478b036fcea3518daa3b2b5f72f6de2bf89b3b435392de8
7
- data.tar.gz: b44d0fa470ec59317f5a93b0e73d1ec6a032f300083734e2b0aef27b2077b51303dce1b504e77102e6cfb5f79cc2c229d136d4a5d7eca049402bb88e740e711a
6
+ metadata.gz: 6cecc00b6235ed7e9239007334dede81e8d531b448971920bd436dfeec39124951d6ad35769c5281615a6aeffc6428f54ae9bd0a6bad0a7be11a8636abff5dc7
7
+ data.tar.gz: 2cfe890a54be62402d34faa546525393e8e70fd3923131f422b0a39d58541d9969b222d510aa379886cc33bf7902248bfe43f337c2078efb70dceb051dde6ff5
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
  require 'fun_with_gems'
3
3
  require_relative File.join( "lib", "csv_madness" )
4
- self.extend( FunWith::Gems::Rakefile)
4
+ extend FunWith::Gems::Rakefile
5
5
 
6
6
  rakefile_setup CsvMadness
7
7
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.9
1
+ 0.0.12
@@ -24,20 +24,22 @@ module CsvMadness
24
24
  end
25
25
  end
26
26
 
27
+ # verbose: true - reports errors to stdout as they're encountered
28
+ #
27
29
  # Three :on_error values:
28
30
  # :print => Put an error message in the cell instead of a value
29
31
  # :raise => Raise the error, halting the process
30
32
  # :ignore => Hand back an empty cell
31
33
  #
32
34
  # Although ideally it should be configurable by column...
33
- def build( objects, opts = { :on_error => :print } )
35
+ def build( objects, build_opts: { :on_error => :print, :verbose => false } )
34
36
  spreadsheet = CsvMadness::Sheet.new( @column_syms )
35
37
 
36
38
  for object in objects
37
39
  STDOUT << "."
38
40
  record = {}
39
41
  for sym in @column_syms
40
- record[sym] = build_cell( object, sym, opts )
42
+ record[sym] = build_cell( object, sym, build_opts: build_opts )
41
43
  end
42
44
 
43
45
  spreadsheet.add_record( record ) # hash form
@@ -46,14 +48,14 @@ module CsvMadness
46
48
  spreadsheet
47
49
  end
48
50
 
49
- def build_cell( object, sym, opts = { :on_error => :print } )
51
+ def build_cell( object, sym, build_opts: { :on_error => :print } )
50
52
  column = @columns[sym]
51
53
 
52
54
  case column
53
55
  when String
54
- build_cell_by_pathstring( object, column, opts )
56
+ build_cell_by_pathstring( object, column, build_opts )
55
57
  when Proc
56
- build_cell_by_proc( object, column, opts )
58
+ build_cell_by_proc( object, column, build_opts )
57
59
  else
58
60
  "no idea what to do"
59
61
  end
@@ -64,8 +66,8 @@ module CsvMadness
64
66
  end
65
67
 
66
68
  protected
67
- def build_cell_by_pathstring( object, str, opts )
68
- handle_cell_build_error( :build_cell_by_pathstring, opts ) do
69
+ def build_cell_by_pathstring( object, str, build_opts )
70
+ handle_cell_build_error( :build_cell_by_pathstring, build_opts ) do
69
71
  for method in str.split(".").map(&:to_sym)
70
72
  object = object.send(method)
71
73
  end
@@ -74,23 +76,23 @@ module CsvMadness
74
76
  end
75
77
  end
76
78
 
77
- def build_cell_by_proc( object, proc, opts )
78
- handle_cell_build_error( :build_cell_by_proc, opts ) do
79
+ def build_cell_by_proc( object, proc, build_opts )
80
+ handle_cell_build_error( :build_cell_by_proc, build_opts ) do
79
81
  proc.call(object)
80
82
  end
81
83
  end
82
84
 
83
- def handle_cell_build_error( caller, opts, &block )
85
+ def handle_cell_build_error( caller, build_opts, &block )
84
86
  begin
85
87
  yield
86
88
  end
87
89
  rescue Exception => e
88
- case opts[:on_error]
90
+ case build_opts[:on_error]
89
91
  when nil, :print
90
- puts "error #{e.message} #{caller}" if opts[:verbose]
92
+ puts "error #{e.message} #{caller}" if build_opts[:verbose]
91
93
  "ERROR: #{e.message} (#{caller}())"
92
94
  when :raise
93
- puts "Re-raisinge error #{e.message}. Set opts[:on_error] to :print or :ignore if you want Builder to continue on errors."
95
+ puts "Re-raisinge error #{e.message}. Set build_opts[:on_error] to :print or :ignore if you want Builder to continue on errors."
94
96
  raise e
95
97
  when :ignore
96
98
  ""
@@ -50,7 +50,7 @@ module CsvMadness
50
50
  end
51
51
 
52
52
  def to_csv( opts = {} )
53
- self.columns.map{|col| self.send(col) }.to_csv( opts )
53
+ self.columns.map{|col| self.send(col) }.to_csv( **opts )
54
54
  end
55
55
 
56
56
  def to_hash
@@ -54,9 +54,9 @@ module CsvMadness
54
54
 
55
55
 
56
56
 
57
- def to_csv( opts = {} )
58
- self.records.inject( self.columns.to_csv( opts ) ) do |output, record|
59
- output << record.to_csv( opts )
57
+ def to_csv
58
+ self.records.inject( self.columns.to_csv ) do |output, record|
59
+ output << record.to_csv
60
60
  end
61
61
  end
62
62
 
@@ -143,11 +143,10 @@ module CsvMadness
143
143
  attr_writer :columns, :index_columns, :records, :record_class, :opts
144
144
 
145
145
  def load_csv
146
-
147
146
  # encoding seems to solve a specific problem with a specific spreadsheet, at an unknown cost.
148
147
  @csv = CSV.new( File.read(@spreadsheet_file).force_encoding("ISO-8859-1").encode("UTF-8"),
149
- { write_headers: true,
150
- headers: ( @opts[:header] ? :first_row : false ) } )
148
+ write_headers: true,
149
+ headers: ( @opts[:header] ? :first_row : false ))
151
150
  end
152
151
 
153
152
  # Each spreadsheet has its own anonymous record class, and each CSV row instantiates
@@ -62,15 +62,15 @@ module CsvMadness
62
62
 
63
63
  # opts are passed to underlying CSV (:row_sep, :encoding, :force_quotes)
64
64
  def to_csv( spreadsheet, opts = {} )
65
- out = spreadsheet.columns.to_csv( opts )
65
+ out = spreadsheet.columns.to_csv
66
66
  spreadsheet.records.inject( out ) do |output, record|
67
- output << record.to_csv( opts )
67
+ output << record.to_csv
68
68
  end
69
69
  end
70
70
 
71
- def write_to_file( spreadsheet, file, opts = {} )
71
+ def write_to_file( spreadsheet, file )
72
72
  file = file.fwf_filepath.expand_path
73
- file.write( spreadsheet.to_csv( opts ) )
73
+ file.write( spreadsheet.to_csv )
74
74
  end
75
75
  end
76
76
  end
@@ -1,8 +1,8 @@
1
1
  module CsvMadness
2
2
  module SheetMethods
3
3
  module FileMethods
4
- def write_to_file( file, opts = {} )
5
- self.class.write_to_file( self, file, opts )
4
+ def write_to_file( file )
5
+ self.class.write_to_file( self, file )
6
6
  end
7
7
 
8
8
  # pass an arbitrary filepath to save sheet to that filepath
@@ -20,7 +20,7 @@ module CsvMadness
20
20
  end
21
21
 
22
22
  if file
23
- self.write_to_file( self.spreadsheet_file, @opts || {} )
23
+ self.write_to_file( file )
24
24
  else
25
25
  raise "CsvMadness.save(:timestamp) - Spreadsheet must be specified by spreadsheet_file"
26
26
  end
data/lib/csv_madness.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require 'csv'
2
2
  require 'fun_with_gems'
3
- # require 'fun_with_version_strings'
4
3
  require 'time' # to use Time.parse to parse cells to get the date
5
4
 
6
5
  FunWith::Gems.make_gem_fun( "CsvMadness" )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_madness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryce Anderson