Skip to content

Support Ruby 3.2.x - small bug fixes #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 0 additions & 6 deletions json_csv.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions lib/json_csv/csv_to_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion lib/json_csv/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module JsonCsv

VERSION = '1.0.0'.freeze
VERSION = '1.0.1'.freeze

def self.version
VERSION
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down