csv_party 0.0.1.pre2 → 0.0.1.pre3

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/csv_party.rb +29 -18
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0ddb8c3205a36c4ed2b5dd97baf32e60d1da2bb
4
- data.tar.gz: f7bd204ac0a1f74b0f7cd5e90df88fa36edc8e7c
3
+ metadata.gz: 1e5823d9eec07faf638ab898d7082be890c73373
4
+ data.tar.gz: f3b115f8fd4055e141629fc3fc92ac39278dbb2f
5
5
  SHA512:
6
- metadata.gz: d00c4e83bf4f3564f15c9abf6159e209652a1f04b99df349211a7040b47d8670804a6b4c38c77594e2d75fa1735581b706baa0fa45a1ff449180d5112881e474
7
- data.tar.gz: 7778782ffea56a91250478b23fb8ad5634c5b17f1c94b0ef94910c6544e25d242f6c457cbc0f644316eaa1fc28d0827cfeb99d732ba72ea5b27182c8507f456f
6
+ metadata.gz: 3c89169637d692dec361b79b87e5e392050224e43869f0d7a6f66decb3ca1edaed8e7dab777a288ac17c5557ef2964bc5f51a80a841f73601158e22021bf44e5
7
+ data.tar.gz: 4b677735ca562a25d30fd1a46b999d2803b4f73cf187db340f33f373c7acda071f96ffe1614dc6c51dbde7f2ceddc40c88a4738afb44f97ba40b3703f31accce
data/lib/csv_party.rb CHANGED
@@ -35,7 +35,13 @@ class CSVParty
35
35
  parser = options[:parser]
36
36
 
37
37
  unparsed_row[name] = unparsed_value
38
- parsed_row[name] = instance_exec(unparsed_value, &parser)
38
+ parsed_row[name] = if options[:blanks_as_nil] && is_blank?(unparsed_value)
39
+ nil
40
+ elsif parser.is_a? Symbol
41
+ send(parser, unparsed_value)
42
+ else
43
+ instance_exec(unparsed_value, &parser)
44
+ end
39
45
  end
40
46
 
41
47
  parsed_row['unparsed'] = unparsed_row
@@ -57,20 +63,22 @@ class CSVParty
57
63
  raise_if_duplicate_column(name)
58
64
  raise_if_missing_header(name, options)
59
65
 
60
- if block_given?
61
- columns[name] = { header: options[:header], parser: block }
62
- else
63
- parser_method = if options.has_key?(:as)
64
- "#{options[:as]}_parser".to_sym
65
- else
66
- :string_parser
67
- end
68
- columns[name] = {
69
- header: options[:header],
70
- parser: proc { |value| send(parser_method, value) },
71
- parser_method: parser_method
72
- }
73
- end
66
+ options = {
67
+ blanks_as_nil: (options[:as] == :raw ? false : true),
68
+ as: :string
69
+ }.merge(options)
70
+
71
+ parser = if block_given?
72
+ block
73
+ else
74
+ "#{options[:as]}_parser".to_sym
75
+ end
76
+
77
+ columns[name] = {
78
+ header: options[:header],
79
+ parser: parser,
80
+ blanks_as_nil: options[:blanks_as_nil]
81
+ }
74
82
  end
75
83
 
76
84
  def self.columns
@@ -120,6 +128,10 @@ class CSVParty
120
128
 
121
129
  private
122
130
 
131
+ def is_blank?(value)
132
+ value.nil? || value.strip.empty?
133
+ end
134
+
123
135
  def raw_parser(value)
124
136
  value
125
137
  end
@@ -133,7 +145,6 @@ class CSVParty
133
145
  end
134
146
 
135
147
  def integer_parser(value)
136
- return nil if value.nil? || value.strip.empty?
137
148
  value.to_i
138
149
  end
139
150
 
@@ -147,7 +158,7 @@ class CSVParty
147
158
  end
148
159
 
149
160
  def columns_with_named_parsers
150
- columns.select { |_name, options| options.has_key?(:parser_method) }
161
+ columns.select { |_name, options| options[:parser].is_a? Symbol }
151
162
  end
152
163
 
153
164
  # This error has to be raised at runtime because, when the class body
@@ -155,7 +166,7 @@ class CSVParty
155
166
  # they are defined above the column definitions in the class body
156
167
  def raise_unless_named_parsers_are_valid
157
168
  columns_with_named_parsers.each do |name, options|
158
- parser = options[:parser_method]
169
+ parser = options[:parser]
159
170
  next if named_parsers.include? parser
160
171
 
161
172
  parser = parser.to_s.gsub('_parser', '')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_party
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.pre2
4
+ version: 0.0.1.pre3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rico Jones
@@ -17,7 +17,7 @@ extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
19
  - lib/csv_party.rb
20
- homepage: http://rubygems.org/gems/csv_party
20
+ homepage: https://github.com/toasterlovin/csv_party
21
21
  licenses:
22
22
  - MIT
23
23
  metadata: {}