hash_extend 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -125,7 +125,7 @@ Demo
125
125
 
126
126
  ```ruby
127
127
  h = Hash[:one, 1, :two, 2, :three, 3] #=> {:one=>1, :two=>2, :three=>3}
128
- h.delete_many(:un, :trois) #=> [1, 3]
128
+ h.delete_many(:one, :three) #=> [1, 3]
129
129
  h #=> {:two=>2}
130
130
 
131
131
  h.delete_many(:six) #=> [nil]
@@ -188,19 +188,19 @@ Arguments : ':with' is recommended in %w{blank? empty?}, default is :nil?
188
188
  Demo
189
189
 
190
190
  ```ruby
191
- {:un=>1, :deux=>2, :trois=>3, :quatre=>nil, :cinq=>[]}.compact!
192
- #=> {:cinq=>[], :un=>1, :deux=>2, :trois=>3}
191
+ {:one=>1, :two=>2, :three=>3, :four=>nil, :five=>[]}.compact!
192
+ #=> {:five=>[], :one=>1, :two=>2, :three=>3}
193
193
 
194
- {:un=>1, :deux=>2, :trois=>3, :quatre=>nil, :cinq=>[]}.compact!(:with=>:blank?)
195
- #=> {:un=>1, :deux=>2, :trois=>3}
194
+ {:one=>1, :two=>2, :three=>3, :four=>nil, :five=>[]}.compact!(:with=>:blank?)
195
+ #=> {:one=>1, :two=>2, :three=>3}
196
196
 
197
197
  # for the perverts ones
198
- {:un=>1, :deux=>2, :trois=>3, :quatre=>nil, :cinq=>[]}.compact!(:with=>"is_a?(Fixnum)")
199
- #=> {:quatre=>nil, :cinq=>[]}
198
+ {:one=>1, :two=>2, :three=>3, :four=>nil, :five=>[]}.compact!(:with=>"is_a?(Fixnum)")
199
+ #=> {:four=>nil, :five=>[]}
200
200
 
201
201
  # And for the ones who REALLY care about memory
202
- {:un=>1, "deux"=>2, "trois"=>3, "quatre"=>nil, :cinq=>[]}.compact!(:compare => :key, :with=>"is_a?(String)")
203
- #=> {:un=>1, :cinq=>[]}
202
+ {:one=>1, "two"=>2, "three"=>3, "four"=>nil, :five=>[]}.compact!(:compare => :key, :with=>"is_a?(String)")
203
+ #=> {:one=>1, :five=>[]}
204
204
  ```
205
205
 
206
206
  Code
data/lib/hash_extend.rb CHANGED
@@ -2,8 +2,6 @@ require "hash_extend/version"
2
2
 
3
3
  class Hash
4
4
 
5
- # ?> {1=>1, 2=>2}.stealth_delete(1)
6
- # => {2=>2}
7
5
  # delete key(s) but return self instead of deleted value
8
6
  def stealth_delete! *keys
9
7
  keys.each do |key|
@@ -13,8 +11,6 @@ class Hash
13
11
  end
14
12
 
15
13
 
16
- # ?> {1=>1, 2=>2}.map_values{ |v| v**2 }
17
- # => {1=>1, 2=>4}
18
14
  # modify values fro hash through block
19
15
  def map_values!
20
16
  self.each do |key, value|
@@ -22,8 +18,6 @@ class Hash
22
18
  end
23
19
  end
24
20
 
25
- # ?> {1=>1, 2=>2}.map_keys{ |k| k.to_s }
26
- # => {"2"=>2, "1"=>1}
27
21
  # modify keys from hash through block
28
22
  def map_keys
29
23
  self.inject({}) do |hash, (key, value)|
@@ -37,23 +31,12 @@ class Hash
37
31
  end
38
32
 
39
33
 
40
- # ?> h = Hash[:un, 1, :deux, 2, :trois, 3]
41
- # => {:un=>1, :deux=>2, :trois=>3}
42
- # ?> h.delete_many(:un, :trois)
43
- # => [1, 3]
44
- # ?> h
45
- # => {:deux=>2}
46
- #
47
- # ?> h.delete_many(:six)
48
- # => [nil]
49
34
  # delete severals keys in one time
50
35
  def delete_many *keys
51
36
  keys.map{ |key| self.delete(key) }
52
37
  end
53
38
 
54
39
 
55
- # ?> insert("valeur", Hash.new, :key_one, :key_two, :key_wi)
56
- # => {:key_one=>{:key_two=>{:key_wi=>"valeur"}}}
57
40
  # insert a value trough severals "levels" of hashs
58
41
  def insert value, *keys
59
42
  if keys.size > 1
@@ -68,12 +51,7 @@ class Hash
68
51
  self
69
52
  end
70
53
 
71
- # ?> {:un=>1, :deux=>2, :trois=>3, :quatre=>nil, :cinq=>[]}.compact!
72
- # => {:cinq=>[], :un=>1, :deux=>2, :trois=>3}
73
- # ?> {:un=>1, :deux=>2, :trois=>3, :quatre=>nil, :cinq=>[]}.compact!(:with=>:blank?)
74
- # => {:un=>1, :deux=>2, :trois=>3}
75
- # ?> {:un=>1, :deux=>2, :trois=>3, :quatre=>nil, :cinq=>[]}.compact!(:with=>"is_a?(Fixnum)")
76
- # => {:quatre=>nil, :cinq=>[]}
54
+
77
55
  # allow to compact (like Array), default on value.nil?
78
56
  def compact! options = Hash.new
79
57
  compare = options.delete(:compare) || :value
@@ -92,13 +70,13 @@ class Hash
92
70
 
93
71
  self
94
72
  end
95
-
96
-
97
- # ?> {:action=>"index", :controller=>"home", :id=>"42", :firstname=>"thomas", :name=>"petrachi"}.select_from_collection! [:id, :firstname, :name]
98
- # => {:firstname=>"thomas", :name=>"petrachi", :id=>"42"}
73
+
99
74
  # something like #keep_if, but instead of block, pass collection of keys to select
100
- def select_by! collection, *args, &block
101
- self.keep_if{ |field, _| collection.include? field.to_sym }
75
+ def select_by! *collection
76
+ self.keep_if{ |field, _| collection.include? field.to_sym }
77
+
78
+ rescue NoMethodError => e
79
+ raise NoMethodError.new("undefined method `#{ __method__ }' for #{ self.inspect }:#{ self.class }") if e.name == :keep_if
102
80
  end
103
81
 
104
82
 
@@ -1,3 +1,3 @@
1
1
  module HashExtend
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_extend
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-30 00:00:00.000000000 Z
12
+ date: 2012-10-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Extend ruby Hash. No override.
15
15
  email: