qig 0.2.0 → 0.3.0

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: baaf3d2d15ea2c35ff975d3b1f373b307269ca866faed4363ffc5648320c415f
4
- data.tar.gz: 195d83a355ddcf760fd66afb6e3e791b0571d48f01190c9e15fd5b890b83b937
3
+ metadata.gz: f92505d2904e0032d3fcd5b33f03540a7ce8d09080bc50a534ede7e2daecf686
4
+ data.tar.gz: 16caa89894a03819be080591bd729b89ddfb743712122fff401b18e2e2ba0ae9
5
5
  SHA512:
6
- metadata.gz: 1124ac020d0e6ad9c8b6be999d8d25c1b1423e5482bcab1c24d832724d566e7783ad1c8780cf7dbf2e84b41e68bc92652896ec1d94f88ef903da267447d2d273
7
- data.tar.gz: 946766e1e920e92226c5994efb64a435da1c42dbe132b26116bf3065f9f1f77c3d2612eaeb812fa8cda5d9318ea432cf4ed3ba61daeda00e19c322f951a7e4b0
6
+ metadata.gz: ab2d2ace58c87bc6117a20d9ca2e6b1d329398cd5ce895fb2f206abb5f66a06782b4f8d3d48716719e2a67f7091f6e938aebc9bc7e329652782583952b82533a
7
+ data.tar.gz: 9362297e8f6feb6a9f390302ae7d12093877fdd3215f5894b366a05b41330093479b027745b6579ff546bba4f27a79f539d5f985ed89c1a87c07d663acdbe9c7
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.6
2
+ TargetRubyVersion: 2.7
3
3
  NewCops: enable
4
4
 
5
5
  Layout/LineLength:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,25 @@
1
+ # 0.3.0 (January 24, 2022)
2
+
3
+ ## BREAKING CHANGES
4
+
5
+ - drop ruby 2.6 support, because Ruby 2.6 will be EOL in 2 months, and Ruby 2.7 pattern matching syntax makes
6
+ command parsing cases *much* easier to maintain, keeping the main switching logic a single-level `case`
7
+
8
+ ## Features
9
+
10
+ - method invocation syntax (e.g. for filtering)
11
+ - quoting syntax to use operators as literal keys
12
+ - reboxing operator or value collection operator, inverse of the value iteration operator
13
+
14
+ ## Documentation
15
+
16
+ - Reorganize and explain literate specs
17
+ - Specs for dig conformance
18
+
19
+ ## Fixes
20
+
21
+ - On invalid access to struct, return nil, for better conformance to Struct#dig. Previously this raised error.
22
+
1
23
  # 0.2.0 (January 14, 2022)
2
24
 
3
25
  ## Features
@@ -33,4 +55,4 @@
33
55
  ## Features
34
56
 
35
57
  - Ability to qig into arrays and hashes
36
- - Value iteration (`[]`) over arrays
58
+ - Value iteration (`[]`) over arrays
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qig (0.2.0)
4
+ qig (0.3.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -7,13 +7,17 @@ qig is dig extended with jq's "value iterator" `[]` operator.
7
7
 
8
8
  ## Features
9
9
 
10
- - [x] qig into arrays and hashes
11
- - [x] qig into structs
12
- - [x] qig into OpenStructs
13
- - [ ] qig into CSV::Tables
14
- - [ ] qig into CSV::Rows
15
- - [x] value iteration over arrays
16
- - [x] value iteration over hashes
10
+ - [x] compatible with dig
11
+ - [x] qig into arrays and hashes
12
+ - [x] qig into structs
13
+ - [x] qig into OpenStructs
14
+ - [x] qig into CSV::Tables
15
+ - [x] qig into CSV::Rows
16
+ - [x] nifty jq extensions
17
+ - [x] value iteration over arrays
18
+ - [x] value iteration over hashes
19
+ - [x] invoke ruby methods during inside the filter chain
20
+ - [x] value collect: `[[]]`, inverse of the `[]` operator. Collect streamed values back into an array
17
21
  - [ ] option to monkey patch this for more dig-like `subject.qig(*path)` syntax
18
22
 
19
23
  ## Installation
data/lib/qig/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Qig
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/qig.rb CHANGED
@@ -14,33 +14,76 @@ module Qig
14
14
  unit_qig(subject, *path)
15
15
  end
16
16
 
17
+ # "values" as in jq's "value iterator".
18
+ #
19
+ # Coerce to array by taking the .values. Intuitively, get all possible values of `arrayish[x]`.
20
+ #
21
+ # @param arrayish [Array, Hash, #values, Object] array or hash or other to coerce to values
22
+ # @return [Array, Array, Array, Object] array of coerced values
23
+ def values(arrayish)
24
+ arrayish.respond_to?(:values) ? arrayish.values : arrayish
25
+ end
26
+
17
27
  private
18
28
 
19
- def unit_qig(subject, *path)
29
+ def unit_qig(subject, *path) # rubocop:disable Metrics/MethodLength
20
30
  head, *rest = path
21
31
  case head
22
- when nil
32
+ in nil
23
33
  subject
24
- when []
25
- subject = subject&.values if subject.is_a? Hash
26
- collection_qig(subject, *rest)
34
+ in []
35
+ collection_qig(values(subject), *rest)
36
+ in [[]]
37
+ unit_qig([subject], *rest)
38
+ in ['', key]
39
+ unit_qig(step(subject, key), *rest)
40
+ in [[method, [*args], block]]
41
+ unit_qig(subject.public_send(method, *args, &block), *rest)
42
+ in [[method, [*args]]]
43
+ unit_qig(subject.public_send(method, *args), *rest)
44
+ in [[method]]
45
+ unit_qig(subject.public_send(method), *rest)
46
+ in [method, *]
47
+ raise ArgumentError, 'stream method invocation not applicable in unit context'
27
48
  else
28
- unit_qig(subject&.[](head), *rest)
49
+ unit_qig(step(subject, head), *rest)
29
50
  end
30
51
  end
31
52
 
32
- def collection_qig(subjects, *path)
53
+ def collection_qig(subjects, *path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
33
54
  head, *rest = path
34
55
  case head
35
- when nil
56
+ in nil
36
57
  subjects
37
- when []
38
- collection_qig(subjects.map { |s| s.is_a?(Hash) ? s.values : s }.flatten(1), *rest)
39
- # not sure this is quite jq-compliant... [] refuses to iterate over atoms, but flatten will just preserve them.
40
- # maybe more in the spirit of `dig` though?
58
+ in []
59
+ collection_qig(subjects.map(&method(:values)).flatten(1), *rest)
60
+ in [[]]
61
+ unit_qig(subjects, *rest)
62
+ in ['', key]
63
+ collection_qig(subjects.map { |s| step(s, key) }, *rest)
64
+ in [[method, [*args], block]]
65
+ collection_qig(subjects.map { |s| s.public_send(method, *args, &block) }, *rest)
66
+ in [[method, [*args]]]
67
+ collection_qig(subjects.map { |s| s.public_send(method, *args) }, *rest)
68
+ in [[method]]
69
+ collection_qig(subjects.map { |s| s.public_send(method) }, *rest)
70
+ in [method, [*args], block]
71
+ collection_qig(subjects.public_send(method, *args, &block), *rest)
72
+ in [method, [*args]]
73
+ collection_qig(subjects.public_send(method, *args), *rest)
74
+ in [method]
75
+ collection_qig(subjects.public_send(method), *rest)
41
76
  else
42
- collection_qig(subjects.map { |e| e.nil? ? e : e[head] }, *rest)
77
+ collection_qig(subjects.map { |s| step(s, head) }, *rest)
43
78
  end
44
79
  end
80
+
81
+ def step(subject, key)
82
+ subject&.[](key)
83
+ rescue NameError, IndexError
84
+ # Struct's [] is strict and raises on missing key.
85
+ # TODO: more efficient / prettier way of doing this. How does struct itself implement dig?
86
+ nil
87
+ end
45
88
  end
46
89
  end
data/qig.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  # spec.description = "TODO: Write a longer description or delete this line."
13
13
  spec.homepage = 'https://github.com/NAR8789/qig-ruby'
14
14
  spec.license = 'MIT'
15
- spec.required_ruby_version = '>= 2.6.0'
15
+ spec.required_ruby_version = '>= 2.7.0'
16
16
 
17
17
  spec.metadata['homepage_uri'] = spec.homepage
18
18
  spec.metadata['source_code_uri'] = spec.homepage
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xiao Fan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-14 00:00:00.000000000 Z
11
+ date: 2022-01-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -49,7 +49,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
49
  requirements:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: 2.6.0
52
+ version: 2.7.0
53
53
  required_rubygems_version: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - ">="