qig 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f92505d2904e0032d3fcd5b33f03540a7ce8d09080bc50a534ede7e2daecf686
4
- data.tar.gz: 16caa89894a03819be080591bd729b89ddfb743712122fff401b18e2e2ba0ae9
3
+ metadata.gz: 56ef9b278c456d21a26c5aa5c4af9c3fd44ac5c5b8ebc6f680d528c2380e1af3
4
+ data.tar.gz: c5bee04e6937170225b362ae777a7456c488b85afec37695fd5167530d0eaa13
5
5
  SHA512:
6
- metadata.gz: ab2d2ace58c87bc6117a20d9ca2e6b1d329398cd5ce895fb2f206abb5f66a06782b4f8d3d48716719e2a67f7091f6e938aebc9bc7e329652782583952b82533a
7
- data.tar.gz: 9362297e8f6feb6a9f390302ae7d12093877fdd3215f5894b366a05b41330093479b027745b6579ff546bba4f27a79f539d5f985ed89c1a87c07d663acdbe9c7
6
+ metadata.gz: f01814583296ea94b8839b6fb057b27880c92dc23870d533c192d1ed36a34a6d9650a58390f1c5e85cc2a6d92ed3356678f7bb069419015416087df208890d19
7
+ data.tar.gz: c4b2a0d910a66ba27e2b6f24963e015b404759c092f1915126d3c7aebccdf50205d6ab96e6ee480ef20709e361e475bf27b9702f0918b7fded24762e12766526
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ # 0.4.0 (Jan 27, 2022)
2
+
3
+ ## Features
4
+
5
+ - `Qig::Qiggable` mixin for adding chainable `.qig` syntax to subject classes.
6
+ - Allow stepping into enumerables.
7
+
8
+ ## Fixes
9
+
10
+ - make unboxing (`[]`) compatible with `Enumerable`. Previously in collection-context this used `flatten(1)`, which
11
+ `Enumerable` does not support. Now uses `flat_map(&:itself)` instead
12
+
13
+ ## Documentation
14
+
15
+ - Literate spec covering compatibility with lazy enumerators.
16
+
1
17
  # 0.3.0 (January 24, 2022)
2
18
 
3
19
  ## BREAKING CHANGES
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- qig (0.3.0)
4
+ qig (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -3,22 +3,37 @@
3
3
  [![Gem Version](https://badge.fury.io/rb/qig.svg)](https://badge.fury.io/rb/qig)
4
4
  [![CircleCI](https://circleci.com/gh/NAR8789/qig-ruby/tree/main.svg?style=shield)](https://circleci.com/gh/NAR8789/qig-ruby/tree/main)
5
5
 
6
- qig is dig extended with jq's "value iterator" `[]` operator.
6
+ qig is dig extended with jq's "value iterator" `[]` operator and some other goodies.
7
+
8
+ ## Usage
9
+
10
+ ```ruby
11
+ Qig.qig(subject, *path) # => contents of subject at path
12
+ ```
13
+
14
+ examples:
15
+ ```ruby
16
+ # dig-like usage
17
+ Qig.qig({a: { b: { c: 1 } } }, :a, :b, :c) # => 1
18
+
19
+ # dig-like usage augmented with jq's [] operator
20
+ Qig.qig({a: { b: [ { c: 1 }, { c: 2 } ] } }, :a, :b, [], :c) # => [1, 2]
21
+
22
+ # after expanding values, collect them back into an array for indexing into with `[[]]`
23
+ Qig.qig({ a: { b: [{ c: 1 }, { c: 2 }] } }, :a, :b, [], :c, [[]], 1) # => 2
24
+ ```
25
+
26
+ More documentation in the [literate specs](spec/literate)
7
27
 
8
28
  ## Features
9
29
 
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
30
+ - [x] compatible with dig (see (dig conformance specs)[spec/literate/qig/02_conformance/dig_conformance_spec.rb])
31
+ - [x] jq-like value iteration (`[]` operator)
19
32
  - [x] invoke ruby methods during inside the filter chain
20
33
  - [x] value collect: `[[]]`, inverse of the `[]` operator. Collect streamed values back into an array
21
- - [ ] option to monkey patch this for more dig-like `subject.qig(*path)` syntax
34
+ - [x] `Qig::Qiggable` mixin for more dig-like `subject.qig(*path)` syntax
35
+ - [ ] extensive literate specs
36
+ - [ ] works with lazy collections
22
37
 
23
38
  ## Installation
24
39
 
@@ -36,18 +51,6 @@ Or install it yourself as:
36
51
 
37
52
  $ gem install qig
38
53
 
39
- ## Usage
40
-
41
- ```ruby
42
- Qig.qig(subject, *path) # => contents of subject at path
43
- ```
44
-
45
- examples:
46
- ```ruby
47
- Qig.qig({a: { b: { c: 1 } } }, :a, :b, :c) # => 1
48
- Qig.qig({a: { b: [ { c: 1 }, { c: 2 } ] } }, :a, :b, [], :c) # => [1, 2]
49
- ```
50
-
51
54
  ## Development
52
55
 
53
56
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Qig
4
+ # mix-in to add a `.qig` method to any class.
5
+ # NOTE: Target class should support `#[]`, `#map`, and either `#flat_map` or `#values` for full qig functionality
6
+ # - `#[]`: used for stepping in unit context
7
+ # - `#map`: used for stepping in collection context
8
+ # - `#flat_map` or `#values`: used for unboxing in collection context
9
+ module Qiggable
10
+ # @param path [Array<String, Symbol, Array, Object>] retrieval path to apply to `subject`
11
+ #
12
+ # @return [Object, Array<Object>] the value(s) of `self` located at `path`
13
+ def qig(*path)
14
+ Qig.qig(self, *path)
15
+ end
16
+
17
+ # see Enumerable#lazy
18
+ #
19
+ # This version extends the underlying lazy enumerable with Qig::Qiggable
20
+ def lazy
21
+ super.extend(Qig::Qiggable)
22
+ end
23
+ end
24
+ end
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.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/qig.rb CHANGED
@@ -5,6 +5,8 @@ require_relative 'qig/version'
5
5
  # Combining the powers of `dig` and `jq`.
6
6
  # Qig is dig extended with jq's value iteration `[]` operator.
7
7
  module Qig
8
+ autoload :Qiggable, 'qig/qiggable.rb'
9
+
8
10
  class << self
9
11
  # @param subject [Array, Hash, #[]] `subject` to be qug into.
10
12
  # @param path [Array<String, Symbol, Array, Object>] retrieval path to apply to `subject`
@@ -50,13 +52,13 @@ module Qig
50
52
  end
51
53
  end
52
54
 
53
- def collection_qig(subjects, *path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
55
+ def collection_qig(subjects, *path) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
54
56
  head, *rest = path
55
57
  case head
56
58
  in nil
57
59
  subjects
58
60
  in []
59
- collection_qig(subjects.map(&method(:values)).flatten(1), *rest)
61
+ collection_qig(subjects.map(&method(:values)).flat_map(&:itself), *rest)
60
62
  in [[]]
61
63
  unit_qig(subjects, *rest)
62
64
  in ['', key]
@@ -79,7 +81,17 @@ module Qig
79
81
  end
80
82
 
81
83
  def step(subject, key)
82
- subject&.[](key)
84
+ case subject
85
+ in Enumerable unless subject.respond_to?(:[])
86
+ case key
87
+ in Integer
88
+ subject.drop(key).first
89
+ in Range
90
+ subject.drop(key.begin).take(key.count)
91
+ end
92
+ else
93
+ subject&.[](key)
94
+ end
83
95
  rescue NameError, IndexError
84
96
  # Struct's [] is strict and raises on missing key.
85
97
  # TODO: more efficient / prettier way of doing this. How does struct itself implement dig?
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.3.0
4
+ version: 0.4.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-24 00:00:00.000000000 Z
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -30,6 +30,7 @@ files:
30
30
  - bin/console
31
31
  - bin/setup
32
32
  - lib/qig.rb
33
+ - lib/qig/qiggable.rb
33
34
  - lib/qig/version.rb
34
35
  - qig.gemspec
35
36
  - sig/qig.rbs