nuggets 1.5.0 → 1.6.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
- SHA1:
3
- metadata.gz: 5af6231048355058097a53aac9443148a84e7bbc
4
- data.tar.gz: c6976ef57d142740afed95d49bfdd366992d5bbb
2
+ SHA256:
3
+ metadata.gz: dad45b4d4a9ccf28b262ca7b38cb728d04fcbc03cd9ba1d761ac40bded7ce1db
4
+ data.tar.gz: a35c07426a38a092e2a6159f961574d9fd25a55d9b36e639807397b0c2baa877
5
5
  SHA512:
6
- metadata.gz: ea703b2f4bd23a05391a67d6ce8694fa63ab59d02d8e7f7766de2516838a1a7bd8ce31c6a42e09ea78cfb4ddc4009956852e3845ec61f4eccbe6d6f793ef1259
7
- data.tar.gz: 702b6c308c17b5fd8e54f7538bf6057f573cadf60326ace6a55b40cbe805e45b67e581a13a9618e5900f8ec461512d85ff287fa2e39a6ee83f532cf922844510
6
+ metadata.gz: a603785020efbd4ac9e8fb45c95207527f8e02857b76bf0cd201559169c1ea49a41a8e5ae7e5bfe1d48419eff6e72b95a8699c1ffcf49bb28b797881ebbc32f8
7
+ data.tar.gz: 3c16dbd738a21dc8ace62e6916e603fec2853b947c54753033e18d47f8379dcbafaa9f838e69dd48153136907815232a46a7281610bd5f239da47e826b730869
data/ChangeLog CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  = Revision history for nuggets
4
4
 
5
+ == 1.6.0 [2018-07-12]
6
+
7
+ * Added <tt>JSON.*_{multi,canonical}</tt>.
8
+
5
9
  == 1.5.0 [2016-04-19]
6
10
 
7
11
  * Added String#format.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to nuggets version 1.5.0.
5
+ This documentation refers to nuggets version 1.6.0.
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -58,7 +58,7 @@ Travis CI:: https://travis-ci.org/blackwinter/nuggets
58
58
 
59
59
  == LICENSE AND COPYRIGHT
60
60
 
61
- Copyright (C) 2007-2016 Jens Wille
61
+ Copyright (C) 2007-2018 Jens Wille
62
62
 
63
63
  nuggets is free software: you can redistribute it and/or modify it
64
64
  under the terms of the GNU Affero General Public License as published by
@@ -0,0 +1,5 @@
1
+ require 'json'
2
+
3
+ require_relative 'canonical_mixin'
4
+
5
+ JSON.extend(Nuggets::JSON::CanonicalMixin)
@@ -0,0 +1,61 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2018 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ module JSON
29
+ module CanonicalMixin
30
+
31
+ def generate_canonical(obj, opts = {})
32
+ generate(_nuggets_json_canonical(obj), opts)
33
+ end
34
+
35
+ def pretty_generate_canonical(obj, opts = {})
36
+ pretty_generate(_nuggets_json_canonical(obj), opts)
37
+ end
38
+
39
+ def pretty_print_canonical(source, opts = {})
40
+ pretty_generate_canonical(parse_multi(source, opts), opts)
41
+ end
42
+
43
+ alias_method :pc, :pretty_print_canonical
44
+
45
+ private
46
+
47
+ def _nuggets_json_canonical(obj)
48
+ case obj
49
+ when ::Hash
50
+ obj.class.new.tap { |res|
51
+ obj.keys.sort.each { |k| res[k] = send(__method__, obj[k]) } }
52
+ when ::Array
53
+ obj.map { |v| send(__method__, v) }.sort_by(&:to_s)
54
+ else
55
+ obj
56
+ end
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ require 'json'
2
+
3
+ require_relative 'multi_mixin'
4
+
5
+ JSON.extend(Nuggets::JSON::MultiMixin)
@@ -0,0 +1,71 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # nuggets -- Extending Ruby #
5
+ # #
6
+ # Copyright (C) 2007-2018 Jens Wille #
7
+ # #
8
+ # Authors: #
9
+ # Jens Wille <jens.wille@gmail.com> #
10
+ # #
11
+ # nuggets is free software; you can redistribute it and/or modify it under #
12
+ # the terms of the GNU Affero General Public License as published by the Free #
13
+ # Software Foundation; either version 3 of the License, or (at your option) #
14
+ # any later version. #
15
+ # #
16
+ # nuggets is distributed in the hope that it will be useful, but WITHOUT ANY #
17
+ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
18
+ # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for #
19
+ # more details. #
20
+ # #
21
+ # You should have received a copy of the GNU Affero General Public License #
22
+ # along with nuggets. If not, see <http://www.gnu.org/licenses/>. #
23
+ # #
24
+ ###############################################################################
25
+ #++
26
+
27
+ module Nuggets
28
+ module JSON
29
+ module MultiMixin
30
+
31
+ # Preserves multiple (non-unique) names in
32
+ # {"non-interoperable"}[https://tools.ietf.org/html/rfc7159#section-4] JSON objects.
33
+ def parse_multi(source, opts = {})
34
+ parse(source, opts.merge(object_class: MULTI_OBJECT))
35
+ end
36
+
37
+ def pretty_print_multi(source, opts = {})
38
+ pretty_generate(parse_multi(source, opts), opts)
39
+ end
40
+
41
+ alias_method :pp, :pretty_print_multi
42
+
43
+ private
44
+
45
+ class MULTI_OBJECT < ::Hash
46
+
47
+ def []=(k, v)
48
+ super(MULTI_KEY.new(k), v)
49
+ end
50
+
51
+ def each_multi(k)
52
+ block_given? ? each { |l, v| yield v if k == l } : enum_for(__method__, k)
53
+ end
54
+
55
+ def fetch_multi(k)
56
+ each_multi(k).to_a
57
+ end
58
+
59
+ end
60
+
61
+ class MULTI_KEY < ::String
62
+
63
+ def eql?(*)
64
+ false
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -3,7 +3,7 @@ module Nuggets
3
3
  module Version
4
4
 
5
5
  MAJOR = 1
6
- MINOR = 5
6
+ MINOR = 6
7
7
  TINY = 0
8
8
 
9
9
  class << self
@@ -3,6 +3,7 @@ require 'nuggets/array/format'
3
3
  describe Array, 'format' do
4
4
 
5
5
  def self.test_format(*f)
6
+ return if RUBY_ENGINE == 'jruby' && JRUBY_VERSION.include?('pre')
6
7
  f.pop.each { |x, y| example { (f.dup % x).should == y } }
7
8
  end
8
9
 
@@ -7,7 +7,7 @@ describe Enumerable, 'minmax' do
7
7
 
8
8
  e.max.should == 'quux'
9
9
  e.max_by(:length).should == 'quuux'
10
- e.max(:length).should == 5
10
+ e.max(:length).should == 5 if RUBY_VERSION < '2.4'
11
11
  }
12
12
 
13
13
  example {
@@ -15,7 +15,7 @@ describe Enumerable, 'minmax' do
15
15
 
16
16
  e.max.should == 222
17
17
  e.max_by(b).should == 45
18
- e.max(b).should == 5
18
+ e.max(b).should == 5 if RUBY_VERSION < '2.4'
19
19
  }
20
20
 
21
21
  end
@@ -7,36 +7,16 @@ describe_extended ENV, Nuggets::Env::UserHomeMixin, true do
7
7
  end
8
8
 
9
9
  after do
10
- ENV.clear
11
- ENV.update(@old_env)
10
+ ENV.clear.update(@old_env)
12
11
  end
13
12
 
14
13
  example do
15
14
  ENV.user_home.should be_an_instance_of(String)
16
15
  end
17
16
 
18
- example do
19
- ENV.clear
20
- ENV.user_home.should be_an_instance_of(String)
21
- end
22
-
23
17
  example do
24
18
  ENV['HOME'] = 'foo'
25
19
  ENV.user_home.should == 'foo'
26
20
  end
27
21
 
28
- unless RUBY_ENGINE == 'jruby'
29
-
30
- example do
31
- ENV.clear
32
- ENV.user_home('bar').should == 'bar'
33
- end
34
-
35
- example do
36
- ENV.clear
37
- ENV.user_home(nil).should be_nil
38
- end
39
-
40
- end
41
-
42
22
  end
@@ -0,0 +1,74 @@
1
+ require 'nuggets/json/canonical'
2
+ require 'nuggets/json/multi'
3
+
4
+ describe_extended JSON, Nuggets::JSON::CanonicalMixin, true do
5
+
6
+ let(:source) { '{"a":1,"b":[42,23],"a":{"b":2,"a":[3,5,4]}}' }
7
+
8
+ subject { JSON.parse(source) }
9
+
10
+ describe 'plain' do
11
+
12
+ example { expect(JSON.generate(subject)).to eq('{"a":{"b":2,"a":[3,5,4]},"b":[42,23]}') }
13
+
14
+ example { expect(JSON.pretty_generate(subject)).to eq(<<-EOT.chomp) }
15
+ {
16
+ "a": {
17
+ "b": 2,
18
+ "a": [
19
+ 3,
20
+ 5,
21
+ 4
22
+ ]
23
+ },
24
+ "b": [
25
+ 42,
26
+ 23
27
+ ]
28
+ }
29
+ EOT
30
+
31
+ end
32
+
33
+ describe 'canonical' do
34
+
35
+ example { expect(JSON.generate_canonical(subject)).to eq('{"a":{"a":[3,4,5],"b":2},"b":[23,42]}') }
36
+
37
+ example { expect(JSON.pretty_generate_canonical(subject)).to eq(<<-EOT.chomp) }
38
+ {
39
+ "a": {
40
+ "a": [
41
+ 3,
42
+ 4,
43
+ 5
44
+ ],
45
+ "b": 2
46
+ },
47
+ "b": [
48
+ 23,
49
+ 42
50
+ ]
51
+ }
52
+ EOT
53
+
54
+ example { expect(JSON.pretty_print_canonical(source)).to eq(<<-EOT.chomp) }
55
+ {
56
+ "a": 1,
57
+ "a": {
58
+ "a": [
59
+ 3,
60
+ 4,
61
+ 5
62
+ ],
63
+ "b": 2
64
+ },
65
+ "b": [
66
+ 23,
67
+ 42
68
+ ]
69
+ }
70
+ EOT
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,67 @@
1
+ require 'nuggets/json/multi'
2
+
3
+ describe_extended JSON, Nuggets::JSON::MultiMixin, true do
4
+
5
+ let(:source) { '{"a":1,"b":[42,23],"a":{"b":2}}' }
6
+
7
+ describe 'plain' do
8
+
9
+ subject { JSON.parse(source) }
10
+
11
+ example { expect(subject).to be_a(Hash) }
12
+ example { expect(subject.size).to eq(2) }
13
+ example { expect(subject.keys).to eq(%w[a b]) }
14
+
15
+ example { expect(subject['a']).to eq('b' => 2) }
16
+ example { expect(subject['b']).to eq([42, 23]) }
17
+
18
+ example { expect(JSON.pretty_generate(subject)).to eq(<<-EOT.chomp) }
19
+ {
20
+ "a": {
21
+ "b": 2
22
+ },
23
+ "b": [
24
+ 42,
25
+ 23
26
+ ]
27
+ }
28
+ EOT
29
+
30
+ end
31
+
32
+ describe 'multi' do
33
+
34
+ subject { JSON.parse_multi(source) }
35
+
36
+ let(:multi) { subject.fetch_multi('a') }
37
+
38
+ let(:pretty) { <<-EOT.chomp }
39
+ {
40
+ "a": 1,
41
+ "b": [
42
+ 42,
43
+ 23
44
+ ],
45
+ "a": {
46
+ "b": 2
47
+ }
48
+ }
49
+ EOT
50
+
51
+ example { expect(subject).to be_a(Hash) }
52
+ example { expect(subject.size).to eq(3) }
53
+ example { expect(subject.keys).to eq(%w[a b a]) }
54
+
55
+ example { expect(subject['a']).to eq(1) }
56
+ example { expect(subject['b']).to eq([42, 23]) }
57
+
58
+ example { expect(multi[0]).to eq(1) }
59
+ example { expect(multi[1]).to be_a(Hash) }
60
+ example { expect(multi[1]['b']).to eq(2) }
61
+
62
+ example { expect(JSON.pretty_generate(subject)).to eq(pretty) }
63
+ example { expect(JSON.pretty_print_multi(source)).to eq(pretty) }
64
+
65
+ end
66
+
67
+ end
@@ -2,6 +2,8 @@ require 'nuggets/string/highlight'
2
2
 
3
3
  describe_extended String, Nuggets::String::HighlightMixin do
4
4
 
5
+ unless RUBY_ENGINE == 'jruby' && JRUBY_VERSION.include?('pre')
6
+
5
7
  example do
6
8
  s = 'lingo go do the go go'; t = s.dup
7
9
  expect(s.highlight('lingo')).to eq('|lingo| go do the go go')
@@ -41,3 +43,5 @@ describe_extended String, Nuggets::String::HighlightMixin do
41
43
  end
42
44
 
43
45
  end
46
+
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nuggets
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-19 00:00:00.000000000 Z
11
+ date: 2018-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mime-types
@@ -44,20 +44,20 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0.8'
47
+ version: '0.9'
48
48
  - - ">="
49
49
  - !ruby/object:Gem::Version
50
- version: 0.8.3
50
+ version: 0.9.0
51
51
  type: :development
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
54
54
  requirements:
55
55
  - - "~>"
56
56
  - !ruby/object:Gem::Version
57
- version: '0.8'
57
+ version: '0.9'
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
- version: 0.8.3
60
+ version: 0.9.0
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rake
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -207,6 +207,10 @@ files:
207
207
  - lib/nuggets/io/modes.rb
208
208
  - lib/nuggets/io/redirect.rb
209
209
  - lib/nuggets/io/redirect_mixin.rb
210
+ - lib/nuggets/json/canonical.rb
211
+ - lib/nuggets/json/canonical_mixin.rb
212
+ - lib/nuggets/json/multi.rb
213
+ - lib/nuggets/json/multi_mixin.rb
210
214
  - lib/nuggets/lazy_attr.rb
211
215
  - lib/nuggets/log_parser.rb
212
216
  - lib/nuggets/log_parser/apache.rb
@@ -323,6 +327,8 @@ files:
323
327
  - spec/nuggets/integer/map_spec.rb
324
328
  - spec/nuggets/integer/roman_spec.rb
325
329
  - spec/nuggets/integer/to_binary_s_spec.rb
330
+ - spec/nuggets/json/canonical_spec.rb
331
+ - spec/nuggets/json/multi_spec.rb
326
332
  - spec/nuggets/module/lazy_attr_spec.rb
327
333
  - spec/nuggets/module/query_attr_spec.rb
328
334
  - spec/nuggets/numeric/duration_spec.rb
@@ -358,15 +364,13 @@ licenses:
358
364
  metadata: {}
359
365
  post_install_message: |2+
360
366
 
361
- nuggets-1.5.0 [2016-04-19]:
367
+ nuggets-1.6.0 [2018-07-12]:
362
368
 
363
- * Added String#format.
364
- * Added String#highlight.
365
- * Dropped IO::NULL, available as File::NULL since Ruby 1.9.3.
369
+ * Added <tt>JSON.*_{multi,canonical}</tt>.
366
370
 
367
371
  rdoc_options:
368
372
  - "--title"
369
- - nuggets Application documentation (v1.5.0)
373
+ - nuggets Application documentation (v1.6.0)
370
374
  - "--charset"
371
375
  - UTF-8
372
376
  - "--line-numbers"
@@ -387,7 +391,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
387
391
  version: '0'
388
392
  requirements: []
389
393
  rubyforge_project:
390
- rubygems_version: 2.6.3
394
+ rubygems_version: 2.7.7
391
395
  signing_key:
392
396
  specification_version: 4
393
397
  summary: Extending Ruby.