diff --git a/Gemfile b/Gemfile index 8c93a7b..4c96862 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,13 @@ source 'http://rubygems.org' # See json_csv.gemspec for dependency info gemspec + +gem "rake", "~> 13.0" + +gem "rspec", "~> 3.12" + +gem "rubocop", "~> 0.51.0" + +gem "rubocop-rspec", "~> 1.20" + +gem "simplecov", "~> 0.22.0" diff --git a/json_csv.gemspec b/json_csv.gemspec index 027ae78..72c90c8 100644 --- a/json_csv.gemspec +++ b/json_csv.gemspec @@ -12,12 +12,6 @@ Gem::Specification.new do |s| s.homepage = 'https://github.com/cul/json_csv' s.license = 'MIT' - s.add_development_dependency("rake", ">= 10.1") - s.add_development_dependency("rspec", "~>3.7") - s.add_development_dependency("rubocop", "~> 0.51.0") - s.add_development_dependency("rubocop-rspec", "~> 1.20.1") - s.add_development_dependency("simplecov", "~> 0.15.1") - s.files = Dir["lib/**/*.rb", "lib/tasks/**/*.rake", "bin/*", "LICENSE", "*.md"] s.require_paths = ['lib'] end diff --git a/lib/json_csv/csv_to_json.rb b/lib/json_csv/csv_to_json.rb index d5f9f00..831774d 100644 --- a/lib/json_csv/csv_to_json.rb +++ b/lib/json_csv/csv_to_json.rb @@ -22,7 +22,7 @@ module ClassMethods # Sample usage: csv_file_to_hierarchical_json_hash(path_to_csv, field_casting_rules = {}, strip_value_whitespace = true) do |row_json_hash, row_number| def csv_file_to_hierarchical_json_hash(path_to_csv, field_casting_rules = {}, strip_value_whitespace = true) i = 2 # start with row 2 because this corresponds to the SECOND row of 1-indexed CSV data (where headers are row 1) - CSV.foreach(path_to_csv, headers: true, header_converters: lambda { |header| + CSV.foreach(path_to_csv, converters: %i[numeric date], headers: true, header_converters: lambda { |header| header.strip # remove leading and trailing header whitespace }) do |row_data_hash| yield csv_row_hash_to_hierarchical_json_hash(row_data_hash, field_casting_rules, strip_value_whitespace), i @@ -54,7 +54,7 @@ def put_value_at_json_path(obj, json_path, value, field_casting_rules = {}, full # If the full_json_path_from_top matches one of the field_casting_rules, # then case this field to the specified cast type full_json_path_from_top_as_field_casting_rule_pattern = real_json_path_to_field_casting_rule_pattern(full_json_path_from_top) - obj[json_path_pieces[0]] = field_casting_rules.key?(full_json_path_from_top_as_field_casting_rule_pattern) ? apply_field_casting_type(value, field_casting_rules[full_json_path_from_top_as_field_casting_rule_pattern]) : value + obj[json_path_pieces[0]] = field_casting_rules.key?(full_json_path_from_top_as_field_casting_rule_pattern) ? apply_field_casting_type(value, field_casting_rules[full_json_path_from_top_as_field_casting_rule_pattern]) : value.to_s else obj[json_path_pieces[0]] ||= (json_path_pieces[1].is_a?(Integer) ? [] : {}) put_value_at_json_path(obj[json_path_pieces[0]], pieces_to_json_path(json_path_pieces[1..-1]), value, field_casting_rules, full_json_path_from_top) @@ -72,10 +72,10 @@ def apply_field_casting_type(value, field_casting_type) case field_casting_type when TYPE_INTEGER - raise ArgumentError, "\"#{value}\" is not an integer" unless value =~ /^[0-9]+$/ + raise ArgumentError, "\"#{value}\" is not an integer" unless value.to_s =~ /^[0-9]+$/ value.to_i when TYPE_FLOAT - raise ArgumentError, "\"#{value}\" is not a float" unless value =~ /^[0-9]+(\.[0-9]+)*$/ || value =~ /^\.[0-9]+$/ + raise ArgumentError, "\"#{value}\" is not a float" unless value.to_s =~ /^[0-9]+(\.[0-9]+)*$/ || value.to_s =~ /^\.[0-9]+$/ value.to_f when TYPE_BOOLEAN if value.downcase == 'true' diff --git a/lib/json_csv/version.rb b/lib/json_csv/version.rb index 33ae428..2201123 100644 --- a/lib/json_csv/version.rb +++ b/lib/json_csv/version.rb @@ -1,6 +1,6 @@ module JsonCsv - VERSION = '1.0.0'.freeze + VERSION = '1.0.1'.freeze def self.version VERSION diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b5ee78d..09757d1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -11,7 +11,7 @@ def absolute_fixture_path(file) end def fixture(file) path = absolute_fixture_path(file) - raise "No fixture file at #{path}" unless File.exists? path + raise "No fixture file at #{path}" unless File.exist? path File.new(path) end