Skip to content

Commit e2d2e8a

Browse files
authored
Fixed a problem where deleting slices was not working. (#90)
* Fixed a problem where deleting slices was not working. * Ran a rubocop fix. * Added rubocop run. * Added rubocop todo * Updated rescue.
1 parent 74a5a5b commit e2d2e8a

14 files changed

+318
-127
lines changed

.rubocop.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
inherit_from: .rubocop_todo.yml

.rubocop_todo.yml

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# This configuration was generated by
2+
# `rubocop --auto-gen-config`
3+
# on 2018-07-07 21:29:45 +0200 using RuboCop version 0.57.2.
4+
# The point is for the user to remove these configuration records
5+
# one by one as the offenses are removed from the code base.
6+
# Note that changes in the inspected code, or installation of new
7+
# versions of RuboCop, may require this file to be generated again.
8+
9+
# Offense count: 15
10+
# Configuration parameters: AllowSafeAssignment.
11+
Lint/AssignmentInCondition:
12+
Exclude:
13+
- 'lib/jsonpath.rb'
14+
- 'lib/jsonpath/parser.rb'
15+
16+
# Offense count: 1
17+
Lint/IneffectiveAccessModifier:
18+
Exclude:
19+
- 'lib/jsonpath.rb'
20+
21+
# Offense count: 16
22+
Metrics/AbcSize:
23+
Max: 54
24+
25+
# Offense count: 2
26+
# Configuration parameters: CountComments, ExcludedMethods.
27+
Metrics/BlockLength:
28+
Max: 33
29+
30+
# Offense count: 1
31+
# Configuration parameters: CountBlocks.
32+
Metrics/BlockNesting:
33+
Max: 4
34+
35+
# Offense count: 2
36+
# Configuration parameters: CountComments.
37+
Metrics/ClassLength:
38+
Max: 578
39+
40+
# Offense count: 6
41+
Metrics/CyclomaticComplexity:
42+
Max: 18
43+
44+
# Offense count: 24
45+
# Configuration parameters: CountComments.
46+
Metrics/MethodLength:
47+
Max: 52
48+
49+
# Offense count: 1
50+
# Configuration parameters: CountKeywordArgs.
51+
Metrics/ParameterLists:
52+
Max: 6
53+
54+
# Offense count: 6
55+
Metrics/PerceivedComplexity:
56+
Max: 20
57+
58+
# Offense count: 1
59+
# Configuration parameters: AllowedChars.
60+
Style/AsciiComments:
61+
Exclude:
62+
- 'lib/jsonpath/parser.rb'
63+
64+
# Offense count: 2
65+
Style/Documentation:
66+
Exclude:
67+
- 'spec/**/*'
68+
- 'test/**/*'
69+
- 'lib/jsonpath/enumerable.rb'
70+
- 'lib/jsonpath/proxy.rb'
71+
72+
# Offense count: 1
73+
# Configuration parameters: MinBodyLength.
74+
Style/GuardClause:
75+
Exclude:
76+
- 'lib/jsonpath/enumerable.rb'
77+
78+
# Offense count: 2
79+
# Cop supports --auto-correct.
80+
Style/IfUnlessModifier:
81+
Exclude:
82+
- 'lib/jsonpath/enumerable.rb'
83+
84+
# Offense count: 2
85+
# Cop supports --auto-correct.
86+
# Configuration parameters: AutoCorrect, EnforcedStyle.
87+
# SupportedStyles: predicate, comparison
88+
Style/NumericPredicate:
89+
Exclude:
90+
- 'spec/**/*'
91+
- 'lib/jsonpath/enumerable.rb'
92+
93+
# Offense count: 2
94+
# Cop supports --auto-correct.
95+
# Configuration parameters: EnforcedStyle, AllowInnerSlashes.
96+
# SupportedStyles: slashes, percent_r, mixed
97+
Style/RegexpLiteral:
98+
Exclude:
99+
- 'lib/jsonpath/parser.rb'
100+
101+
# Offense count: 3
102+
# Cop supports --auto-correct.
103+
Style/RescueModifier:
104+
Exclude:
105+
- 'lib/jsonpath/enumerable.rb'
106+
- 'lib/jsonpath/parser.rb'
107+
108+
# Offense count: 71
109+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
110+
# URISchemes: http, https
111+
Metrics/LineLength:
112+
Max: 175

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
language: ruby
12
rvm:
23
- 2.1.6
34
- 2.3.1

Gemfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
source "http://rubygems.org"
1+
# frozen_string_literal: true
2+
3+
source 'http://rubygems.org'
24
gemspec
3-
gem 'simplecov', :require => false, :group => :test
5+
gem 'rubocop', require: true, group: :test
6+
gem 'simplecov', require: false, group: :test

Rakefile

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
desc 'run rubocop'
4+
task(:rubocop) do
5+
require 'rubocop'
6+
cli = RuboCop::CLI.new
7+
cli.run
8+
end
9+
110
require 'simplecov'
211
SimpleCov.start do
312
add_filter '/test/'
@@ -11,4 +20,4 @@ task :test do
1120
Dir['./test/**/test_*.rb'].each { |test| require test }
1221
end
1322

14-
task default: :test
23+
task default: %i[test rubocop]

bin/jsonpath

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
23

34
require 'jsonpath'
45
require 'multi_json'

jsonpath.gemspec

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
# -*- encoding: utf-8 -*-
1+
# frozen_string_literal: true
22

33
require File.join(File.dirname(__FILE__), 'lib', 'jsonpath', 'version')
44

55
Gem::Specification.new do |s|
66
s.name = 'jsonpath'
77
s.version = JsonPath::VERSION
8-
s.required_rubygems_version =
9-
Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
8+
if s.respond_to? :required_rubygems_version=
9+
s.required_rubygems_version =
10+
Gem::Requirement.new('>= 0')
11+
end
1012
s.authors = ['Joshua Hull', 'Gergely Brautigam']
1113
s.summary = 'Ruby implementation of http://goessner.net/articles/JsonPath/'
1214
s.description = 'Ruby implementation of http://goessner.net/articles/JsonPath/.'
@@ -25,10 +27,9 @@ Gem::Specification.new do |s|
2527
# dependencies
2628
s.add_runtime_dependency 'multi_json'
2729
s.add_runtime_dependency 'to_regexp', '~> 0.2.1'
30+
s.add_development_dependency 'bundler'
2831
s.add_development_dependency 'code_stats'
29-
s.add_development_dependency 'rake'
3032
s.add_development_dependency 'minitest', '~> 2.2.0'
3133
s.add_development_dependency 'phocus'
32-
s.add_development_dependency 'bundler'
34+
s.add_development_dependency 'rake'
3335
end
34-

lib/jsonpath.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'strscan'
24
require 'multi_json'
35
require 'jsonpath/proxy'
@@ -8,7 +10,7 @@
810
# JsonPath: initializes the class with a given JsonPath and parses that path
911
# into a token array.
1012
class JsonPath
11-
PATH_ALL = '$..*'.freeze
13+
PATH_ALL = '$..*'
1214

1315
attr_accessor :path
1416

@@ -73,7 +75,7 @@ def enum_on(obj_or_str, mode = nil)
7375
JsonPath::Enumerable.new(self, self.class.process_object(obj_or_str), mode,
7476
@opts)
7577
end
76-
alias_method :[], :enum_on
78+
alias [] enum_on
7779

7880
def self.on(obj_or_str, path, opts = {})
7981
new(path, opts).on(process_object(obj_or_str))

lib/jsonpath/enumerable.rb

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class JsonPath
24
class Enumerable
35
include ::Enumerable
@@ -32,7 +34,7 @@ def each(context = @object, key = nil, pos = 0, &blk)
3234

3335
private
3436

35-
def handle_wildecard(node, expr, context, key, pos, &blk)
37+
def handle_wildecard(node, expr, _context, _key, pos, &blk)
3638
expr[1, expr.size - 2].split(',').each do |sub_path|
3739
case sub_path[0]
3840
when '\'', '"'
@@ -60,27 +62,32 @@ def handle_wildecard(node, expr, context, key, pos, &blk)
6062
end_idx %= node.size
6163
step = process_function_or_literal(array_args[2], 1)
6264
next unless step
63-
(start_idx..end_idx).step(step) { |i| each(node, i, pos + 1, &blk) }
65+
if @mode == :delete
66+
(start_idx..end_idx).step(step) { |i| node[i] = nil }
67+
node.compact!
68+
else
69+
(start_idx..end_idx).step(step) { |i| each(node, i, pos + 1, &blk) }
70+
end
6471
end
6572
end
6673
end
6774

6875
def handle_question_mark(sub_path, node, pos, &blk)
6976
case node
70-
when Array
71-
node.size.times do |index|
72-
@_current_node = node[index]
73-
# exps = sub_path[1, sub_path.size - 1]
74-
# if @_current_node.send("[#{exps.gsub(/@/, '@_current_node')}]")
75-
if process_function_or_literal(sub_path[1, sub_path.size - 1])
76-
each(@_current_node, nil, pos + 1, &blk)
77-
end
78-
end
79-
when Hash
77+
when Array
78+
node.size.times do |index|
79+
@_current_node = node[index]
80+
# exps = sub_path[1, sub_path.size - 1]
81+
# if @_current_node.send("[#{exps.gsub(/@/, '@_current_node')}]")
8082
if process_function_or_literal(sub_path[1, sub_path.size - 1])
8183
each(@_current_node, nil, pos + 1, &blk)
8284
end
83-
else
85+
end
86+
when Hash
87+
if process_function_or_literal(sub_path[1, sub_path.size - 1])
88+
each(@_current_node, nil, pos + 1, &blk)
89+
end
90+
else
8491
yield node if process_function_or_literal(sub_path[1, sub_path.size - 1])
8592
end
8693
end

lib/jsonpath/parser.rb

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'strscan'
24
require 'to_regexp'
35

@@ -23,7 +25,7 @@ def parse(exp)
2325
end
2426

2527
def parse_exp(exp)
26-
exp = exp.sub(/@/, '').gsub(/^\(/, '').gsub(/\)$/, '').gsub(/"/, '\'').strip
28+
exp = exp.sub(/@/, '').gsub(/^\(/, '').gsub(/\)$/, '').tr('"', '\'').strip
2729
scanner = StringScanner.new(exp)
2830
elements = []
2931
until scanner.eos?
@@ -39,25 +41,25 @@ def parse_exp(exp)
3941
operator = t
4042
elsif t = scanner.scan(/(\s+)?'?.*'?(\s+)?/)
4143
# If we encounter a node which does not contain `'` it means
42-
# that we are dealing with a boolean type.
43-
if t == "true"
44-
operand = true
45-
elsif t == "false"
46-
operand = false
47-
else
48-
operand = operator.strip == "=~" ? t.to_regexp : t.delete("'").strip
49-
end
44+
#  that we are dealing with a boolean type.
45+
operand = if t == 'true'
46+
true
47+
elsif t == 'false'
48+
false
49+
else
50+
operator.strip == '=~' ? t.to_regexp : t.delete("'").strip
51+
end
5052
elsif t = scanner.scan(/\/\w+\//)
5153
elsif t = scanner.scan(/.*/)
5254
raise "Could not process symbol: #{t}"
5355
end
5456
end
5557

56-
if elements.empty?
57-
el = @_current_node
58-
else
59-
el = dig(elements, @_current_node)
60-
end
58+
el = if elements.empty?
59+
@_current_node
60+
else
61+
dig(elements, @_current_node)
62+
end
6163
return false if el.nil?
6264
return true if operator.nil? && el
6365

lib/jsonpath/proxy.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class JsonPath
24
class Proxy
35
attr_reader :obj

lib/jsonpath/version.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
class JsonPath
2-
VERSION = '0.9.2'.freeze
4+
VERSION = '0.9.2'
35
end

0 commit comments

Comments
 (0)