rudash 2.8.3 → 2.9.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: dc7aaeca7e82e07a1facc3dad834177b254a6d2e56d1b494855c1922e4ec48d3
4
- data.tar.gz: 0f7aa124d2b72b1c12ec77f3c942f7c4315d6f19d5c91460ddd5d55056bf9d20
3
+ metadata.gz: 533e5c283a3565293e86f436ad14c5c31130a6e87ec4a604e2247dc8b33969c4
4
+ data.tar.gz: 7d5f63bdfac9d6b7759f99ca35e9be4ea07e0d7280bbc43f559c565fb7769810
5
5
  SHA512:
6
- metadata.gz: 77a0eb97316bb07b9703b7d3659a139834ab79483ecf225b68e089646e93ee267d7d5357653bbb6ba981557ae65c964eca482ab4eeb5172ca958ba120df6ad31
7
- data.tar.gz: d3b83b741229716e0dc356610564773d818169371dcf0b866d7db098c804f72782e34a3d44de04567011d7e8fece2176dfb638fff3a9c4271b672ddd201cc8ee
6
+ metadata.gz: 453931f3c25a2d5b7a2d6e86a71eeed859c1556cfd8006ad89e7d1876c0d0b0f8c1a90b62beacbe1ae98227efee6cca9a0149eb9a527ce6045afd1602d4d0a73
7
+ data.tar.gz: d86479387726933384808b41a435176eb271ec17fbab369e58ec6d71d9c8256a1986804a0271637d29b4336baf1da31ec0cd6051e1d4b037c635807b18a5809f
data/lib/rudash/filter.rb CHANGED
@@ -22,12 +22,12 @@ module Rudash
22
22
  end
23
23
  elsif collection.is_a?(Hash)
24
24
  begin
25
- return collection.select { |k, v| filter.(v, k) }
25
+ return collection.select { |k, v| filter.(v, k) }.values
26
26
  rescue ArgumentError => e
27
27
  begin
28
- return collection.select { |k, v| filter.(v) }
28
+ return collection.select { |k, v| filter.(v) }.values
29
29
  rescue ArgumentError => e
30
- return collection.select { filter.() }
30
+ return collection.select { filter.() }.values
31
31
  end
32
32
  end
33
33
  else
@@ -0,0 +1,18 @@
1
+ require_relative '../utils/subset_deep_match.rb'
2
+
3
+ module Rudash
4
+ module Reject
5
+ def reject(collection, *rest_args)
6
+ filter = self.head(rest_args) || self.method(:identity).to_proc
7
+
8
+ if filter.is_a?(Hash)
9
+ slice_matcher = Rudash::SubsetDeepMatch.subset_deep_match?.(filter)
10
+ return self.filter(collection, self.negate(slice_matcher))
11
+ elsif filter.is_a?(Proc)
12
+ return self.filter(collection, self.negate(filter))
13
+ else
14
+ return []
15
+ end
16
+ end
17
+ end
18
+ end
data/lib/rudash.rb CHANGED
@@ -44,7 +44,11 @@ require_relative './rudash/update.rb'
44
44
  require_relative './rudash/slice.rb'
45
45
  require_relative './rudash/remove.rb'
46
46
  require_relative './rudash/union.rb'
47
+ require_relative './rudash/reject.rb'
47
48
 
49
+ # This is the exposed Gem class that contains all Rudash methods.
50
+ # New methods can use already implemented methods in the library by refering to "self"
51
+ # in the method if and only if the method get extended into the R_ class.
48
52
  class R_
49
53
  extend Rudash::Map
50
54
  extend Rudash::IsNil
@@ -92,4 +96,5 @@ class R_
92
96
  extend Rudash::Slice
93
97
  extend Rudash::Remove
94
98
  extend Rudash::Union
99
+ extend Rudash::Reject
95
100
  end
data/lib/utils/index.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # This module will contain tiny general utilities
2
+
1
3
  module Rudash
2
4
  module Utils
3
5
  def self.match_number?(str)
@@ -1,6 +1,10 @@
1
1
  require_relative '../rudash'
2
2
  require_relative './index.rb'
3
3
 
4
+ # This module will create the nested path when using R_.set on not existing paths
5
+ # If we've a hash as { a: 1 } and we run R_.set(hash, 'a.b.c.d', 5) then we want to create the whole
6
+ # path ['a', 'b', 'c', 'd'] as embedded hashes. If we have some string that matches a number then we create an array.
7
+
4
8
  module Rudash
5
9
  module NestedPathCreator
6
10
  def self.create_path_if_not_exist(object, resolved_path)
@@ -13,13 +17,15 @@ module Rudash
13
17
  value = R_.get(object, path)
14
18
 
15
19
  if R_.is_nil?(value) || (!value.is_a?(Hash) && !value.is_a?(Array))
20
+ # If the next path item is numeric (index) then we want to create an array otherwise we create a hash
16
21
  if next_path && Utils.match_number?(next_path)
17
22
  object[path_key] = []
18
23
  else
19
24
  object[path_key] = {}
20
25
  end
21
26
  end
22
-
27
+
28
+ # Do the same recursively for next path until getting to the last path item
23
29
  self.create_path_if_not_exist(
24
30
  R_.get(object, path),
25
31
  rest_paths
@@ -1,5 +1,10 @@
1
1
  require_relative '../rudash'
2
2
 
3
+ # This module have the logic of resolving the paths for R_.get, R_.set and R_.update.
4
+ # The resolve method will transform the path 'a.b.c.d[0]' to ['a', 'b', 'c', 'd', '0'].
5
+ # The mentioned functions above can get the path in either way but we want to normilize the string path
6
+ # to the array shape in order to make the logic related only to one data structure.
7
+
3
8
  module Rudash
4
9
  module PathResolver
5
10
  def self.resolve(path)
@@ -1,7 +1,7 @@
1
1
  require_relative '../rudash'
2
2
 
3
3
  # This module was written to supply complex subset deep hash and array matching
4
- # in order to give filter, some?, every? and find the ability to deep match with complex hash queries.
4
+ # in order to give R_.filter, R_.some?, R_.every? and R_.find the ability to deep match with complex hash queries.
5
5
  # See test_filter_hashes_by_deep_hash (test/filter.rb)
6
6
 
7
7
  module Rudash
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rudash
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.3
4
+ version: 2.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Islam Attrash
@@ -52,6 +52,7 @@ files:
52
52
  - lib/rudash/pick.rb
53
53
  - lib/rudash/reduce.rb
54
54
  - lib/rudash/reduce_right.rb
55
+ - lib/rudash/reject.rb
55
56
  - lib/rudash/remove.rb
56
57
  - lib/rudash/reverse.rb
57
58
  - lib/rudash/set.rb