active_object 5.13.0 → 5.14.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: 96356d62c77b8ed4fa8ebc6718d62cfb21fd4aa04128bc1417e6c1dde058b34d
4
- data.tar.gz: 12f5957033dc488fd53560bd154ec3a9e68fe1a292e393ff9783feefa133ef89
3
+ metadata.gz: 4e9e4059ac3952fe095804bde2546b1b3c454fb765d75127c3b9be2755eba4e9
4
+ data.tar.gz: 4eb14d7f25aa5cc39e47fc2d1905c3e5eafb9a0a2eace29d1dc6491f44b4225b
5
5
  SHA512:
6
- metadata.gz: 138bd49f77c6e44eb6df253860c74562e3d5f2c2cfc1c4f7bf2aaa470d5e991a59119d1f454e54024a32778328ef4cf944d3d908f37de2552eeb6caf3f47b07e
7
- data.tar.gz: 7e52ecc8abfced234babad08334700ff50197a16234f43666010a4ed04734c58a1433406738e78e77c25128c73cd69613344f7f77eddbf5f11cd94585c5cd97d
6
+ metadata.gz: cea40c891e09ff82a93f9d14e38b8d36588d5171466decd3a47a75001c16c16563b9341d94f628c55231c5f6d07db3e27bef3478244480b450648ef5ef02bf44
7
+ data.tar.gz: 1616bc2b796cd183f6a43fa7c1f3a5ec4b89d99fedf159152948a4fac0f770cd5e46e20e2bbfacf2e66f92b8a71535be4d277f38eefc954fe440af3c6278fb20
data/README.md CHANGED
@@ -101,6 +101,15 @@ end
101
101
  ['1', '2', '3'].after('4') #=> nil
102
102
  ```
103
103
 
104
+ **Bury:**
105
+ `bury` updates a deeply nested value.
106
+
107
+ ```ruby
108
+ ['1', ['2']].bury(1, '3') #=> ['1', '3']
109
+ ['1', ['2']].bury(1, 0, '3') #=> ['1', ['3']]
110
+ ['1', ['2']].bury(1) #=> raises ArgumentError: '2 or more arguments required'
111
+ ```
112
+
104
113
  **Before:**
105
114
  `before` returns the value before the given value.
106
115
 
@@ -586,9 +595,18 @@ end
586
595
 
587
596
  ```ruby
588
597
  {}.assert_valid_keys(:foo) #=> {}
589
- {}.assert_valid_keys!(:foo) #=> raises 'ArgumentError: Empty hash. Valid keys are: :foo'
598
+ {}.assert_valid_keys!(:foo) #=> raises ArgumentError: 'Empty hash. Valid keys are: :foo'
590
599
  { foo: 'bar' }.assert_valid_keys(:foo) #=> { foo: 'bar' }
591
- { foo: 'bar', baz: 'boz' }.assert_valid_keys(:foo, :boo) #=> raises 'ArgumentError: Unknown key: :baz. Valid keys are: :foo, :boo'
600
+ { foo: 'bar', baz: 'boz' }.assert_valid_keys(:foo, :boo) #=> raises ArgumentError: 'Unknown key: :baz. Valid keys are: :foo, :boo'
601
+ ```
602
+
603
+ **Bury:**
604
+ `bury` updates a deeply nested value.
605
+
606
+ ```ruby
607
+ { foo: { baz: 'boo' } }.bury(:foo, :moo) #=> { foo: :moo }
608
+ { foo: { baz: 'boo' } }.bury(:foo, :baz, :moo) #=> { foo: { baz: :moo } }
609
+ { foo: { baz: 'boo' } }.bury(:foo) #=> raises ArgumentError: '2 or more arguments required'
592
610
  ```
593
611
 
594
612
  **Collect Keys:**
@@ -15,6 +15,33 @@ module ActiveObject
15
15
  self[(index(value).to_i - 1) % length]
16
16
  end
17
17
 
18
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity,
19
+ # rubocop:disable Style/GuardClause, Style/IfInsideElse
20
+ def bury(*args)
21
+ if args.count < 2
22
+ raise ArgumentError, '2 or more arguments required'
23
+ elsif args.count == 2
24
+ if args[0].is_a?(Integer)
25
+ self[args[0]] = args[1]
26
+ else
27
+ self << { args[0] => args[1] }
28
+ end
29
+ else
30
+ if args[0].is_a?(Integer)
31
+ arg = args.shift
32
+
33
+ self[arg] = [] unless self[arg]
34
+ self[arg].bury(*args)
35
+ else
36
+ self << {}.bury(*args)
37
+ end
38
+ end
39
+
40
+ self
41
+ end
42
+ # rubocop:enable Style/GuardClause, Style/IfInsideElse
43
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
44
+
18
45
  def delete_first
19
46
  self[1..-1]
20
47
  end
@@ -25,6 +25,23 @@ module ActiveObject
25
25
  end
26
26
  end
27
27
 
28
+ # rubocop:disable Style/GuardClause
29
+ def bury(*args)
30
+ if args.count < 2
31
+ raise ArgumentError, '2 or more arguments required'
32
+ elsif args.count == 2
33
+ self[args[0]] = args[1]
34
+ else
35
+ arg = args.shift
36
+
37
+ self[arg] = {} unless self[arg]
38
+ self[arg].bury(*args) unless args.empty?
39
+ end
40
+
41
+ self
42
+ end
43
+ # rubocop:enable Style/GuardClause
44
+
28
45
  def compact
29
46
  select { |_, val| !val.nil? }
30
47
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveObject
4
- VERSION ||= '5.13.0'
4
+ VERSION ||= '5.14.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.13.0
4
+ version: 5.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-17 00:00:00.000000000 Z
11
+ date: 2019-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler