buff-extensions 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,5 @@
1
- require 'buff/extensions/object'
2
-
3
1
  module Buff
4
- module Extensions
2
+ module Extensions::Hash
5
3
  module DottedPaths
6
4
  class << self
7
5
  def included(base)
@@ -122,3 +120,7 @@ module Buff
122
120
  end
123
121
  end
124
122
  end
123
+
124
+ class Hash
125
+ include Buff::Extensions::Hash::DottedPaths
126
+ end
@@ -1,5 +1,5 @@
1
1
  module Buff
2
- module Extensions
2
+ module Extensions::Hash
3
3
  # Borrowd and modified from
4
4
  # {https://raw.github.com/rails/rails/master/activesupport/lib/active_support/core_ext/hash/keys.rb}
5
5
  module KeyTransforms
@@ -142,3 +142,7 @@ module Buff
142
142
  end
143
143
  end
144
144
  end
145
+
146
+ class Hash
147
+ include Buff::Extensions::Hash::KeyTransforms
148
+ end
@@ -1,5 +1,5 @@
1
1
  module Buff
2
- module Extensions
2
+ module Extensions::Hash
3
3
  module ReverseMerge
4
4
  # @param [Hash] other
5
5
  #
@@ -17,3 +17,7 @@ module Buff
17
17
  end
18
18
  end
19
19
  end
20
+
21
+ class Hash
22
+ include Buff::Extensions::Hash::ReverseMerge
23
+ end
@@ -0,0 +1,50 @@
1
+ module Buff
2
+ module Extensions::Hash
3
+ # Borrowd and modified from
4
+ # {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/hash/slice.rb}
5
+ module Slice
6
+ # Slice a hash to include only the given keys. This is useful for
7
+ # limiting an options hash to valid keys before passing to a method:
8
+ #
9
+ # def search(criteria = {})
10
+ # criteria.assert_valid_keys(:mass, :velocity, :time)
11
+ # end
12
+ #
13
+ # search(options.slice(:mass, :velocity, :time))
14
+ #
15
+ # If you have an array of keys you want to limit to, you should splat them:
16
+ #
17
+ # valid_keys = [:mass, :velocity, :time]
18
+ # search(options.slice(*valid_keys))
19
+ def slice(*keys)
20
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
21
+ keys.each_with_object(self.class.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
22
+ end
23
+
24
+ # Replaces the hash with only the given keys.
25
+ # Returns a hash containing the removed key/value pairs.
26
+ #
27
+ # { a: 1, b: 2, c: 3, d: 4 }.slice!(:a, :b)
28
+ # # => {:c=>3, :d=>4}
29
+ def slice!(*keys)
30
+ keys.map! { |key| convert_key(key) } if respond_to?(:convert_key, true)
31
+ omit = slice(*self.keys - keys)
32
+ hash = slice(*keys)
33
+ replace(hash)
34
+ omit
35
+ end
36
+
37
+ # Removes and returns the key/value pairs matching the given keys.
38
+ #
39
+ # { a: 1, b: 2, c: 3, d: 4 }.extract!(:a, :b) # => {:a=>1, :b=>2}
40
+ # { a: 1, b: 2 }.extract!(:a, :x) # => {:a=>1}
41
+ def extract!(*keys)
42
+ keys.each_with_object(self.class.new) { |key, result| result[key] = delete(key) if has_key?(key) }
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ class Hash
49
+ include Buff::Extensions::Hash::Slice
50
+ end
@@ -1,9 +1,4 @@
1
1
  require_relative 'hash/dotted_paths'
2
2
  require_relative 'hash/key_transforms'
3
3
  require_relative 'hash/reverse_merge'
4
-
5
- class Hash
6
- include Buff::Extensions::DottedPaths
7
- include Buff::Extensions::KeyTransforms
8
- include Buff::Extensions::ReverseMerge
9
- end
4
+ require_relative 'hash/slice'
@@ -1,5 +1,5 @@
1
1
  module Buff
2
2
  module Extensions
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Buff::Extensions::DottedPaths do
3
+ describe Buff::Extensions::Hash::DottedPaths do
4
4
  describe "ClassMethods" do
5
5
  subject { Hash }
6
6
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buff-extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -195,6 +195,7 @@ files:
195
195
  - lib/buff/extensions/hash/dotted_paths.rb
196
196
  - lib/buff/extensions/hash/key_transforms.rb
197
197
  - lib/buff/extensions/hash/reverse_merge.rb
198
+ - lib/buff/extensions/hash/slice.rb
198
199
  - lib/buff/extensions/object.rb
199
200
  - lib/buff/extensions/object/blank.rb
200
201
  - lib/buff/extensions/version.rb
@@ -221,7 +222,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
221
222
  version: '0'
222
223
  segments:
223
224
  - 0
224
- hash: -4518863559578760213
225
+ hash: 802397933752407242
225
226
  requirements: []
226
227
  rubyforge_project:
227
228
  rubygems_version: 1.8.23