nutella 0.1.1 → 0.2

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.
data/Rakefile CHANGED
@@ -14,4 +14,5 @@ YARD::Rake::YardocTask.new do |doc|
14
14
  end
15
15
  task doc: :yard
16
16
 
17
+ task dist: :build
17
18
  task default: :spec
@@ -0,0 +1,42 @@
1
+ class Hash
2
+ # Grab the given keys out of a hash.
3
+ #
4
+ # @example Selects two keys out of a hash
5
+ # { a: 1, b: 2, c: 3, d: 4 }.slice :a, :c # => { a: 1, c: 3 }
6
+ #
7
+ # @example Limits an options hash to valid keys before passing to a method
8
+ # def search(criteria = {})
9
+ # criteria.assert_valid_keys(:mass, :velocity, :time)
10
+ # end
11
+ #
12
+ # search(options.slice(:mass, :velocity, :time))def slice(*keys)
13
+ #
14
+ # @example Splats an array of keys before passing to the #search method above
15
+ # valid_keys = [:mass, :velocity, :time]
16
+ # search(options.slice(*valid_keys))
17
+ #
18
+ # @param [*Object] keys the keys to slice out of the hash
19
+ # @return [Hash] the key/value pairs that exist in *keys
20
+ def slice(*keys)
21
+ select { |k, v| keys.include? k }
22
+ end
23
+ alias_method :grab, :slice
24
+
25
+ # Acts on the hash as described in Hash#slice, but modifies the hash in
26
+ # place.
27
+ #
28
+ # @example Demonstrates the action and its return value
29
+ # test = { a: 1, b: 2, c: 3, d: 4 }
30
+ # test.slice! :a, :c # => { b: 2, d: 4 }
31
+ # test # => { a: 1, c: 3 }
32
+ #
33
+ # @param [*Object] keys the keys to slice out of the hash
34
+ # @return [Hash] the removed key/value pairs
35
+ def slice!(*keys)
36
+ replace((original = self.dup).slice *keys)
37
+ original.reject { |k, v| keys.include? k }
38
+ end
39
+ # #grab! doesn't sound quite semantically correct, but keeping it for the
40
+ # sake of being consistent with #slice/#grab.
41
+ alias_method :grab!, :slice!
42
+ end
@@ -1,4 +1,4 @@
1
1
  module Nutella
2
2
  # The current version of Nutella.
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2"
4
4
  end
@@ -0,0 +1,49 @@
1
+ require "spec_helper"
2
+ require "nutella/core_ext/hash"
3
+
4
+ describe Hash do
5
+ describe "aliases" do
6
+ test_alias Hash, :slice, :grab
7
+ test_alias Hash, :slice!, :grab!
8
+ end
9
+
10
+ describe "#grab" do
11
+ hash = {}
12
+
13
+ before { hash = { a: 1, b: 2, c: 3, d: 4 } }
14
+
15
+ it "should select the given items from a hash" do
16
+ hash.slice(:a, :c).should == { a: 1, c: 3 }
17
+ end
18
+
19
+ it "should skip items that do not exist in the hash" do
20
+ hash.slice(:a, :d, :f).should == { a: 1, d: 4 }
21
+ end
22
+
23
+ it "should not modify in place" do
24
+ start = hash
25
+ hash.slice :a, :b
26
+ hash.should == start
27
+ end
28
+ end
29
+
30
+ describe "#grab!" do
31
+ hash = {}
32
+
33
+ before { hash = { a: 1, b: 2, c: 3, d: 4 } }
34
+
35
+ it "should modify in place" do
36
+ hash.slice! :a, :c
37
+ hash.should == { a: 1, c: 3 }
38
+ end
39
+
40
+ it "should return the removed pairs" do
41
+ hash.slice!(:a, :c).should == { b: 2, d: 4 }
42
+ end
43
+
44
+ it "should ignore pairs that did not affect the hash" do
45
+ hash.slice!(:a, :c, :g).should == { b: 2, d: 4 }
46
+ hash.should == { a: 1, c: 3 }
47
+ end
48
+ end
49
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nutella
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: '0.2'
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-28 00:00:00.000000000 Z
12
+ date: 2012-09-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -100,8 +100,10 @@ files:
100
100
  - lib/nutella.rb
101
101
  - lib/nutella/version.rb
102
102
  - lib/nutella/core_ext/string.rb
103
+ - lib/nutella/core_ext/hash.rb
103
104
  - lib/nutella/core_ext/enumerable.rb
104
105
  - lib/nutella/core_ext.rb
106
+ - spec/nutella/core_ext/hash_spec.rb
105
107
  - spec/nutella/core_ext/enumerable_spec.rb
106
108
  - spec/nutella/core_ext/string_spec.rb
107
109
  - spec/spec_helper.rb
@@ -131,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
131
133
  version: '0'
132
134
  segments:
133
135
  - 0
134
- hash: -2447363496143350435
136
+ hash: 1979489363505298404
135
137
  requirements: []
136
138
  rubyforge_project:
137
139
  rubygems_version: 1.8.24
@@ -139,6 +141,7 @@ signing_key:
139
141
  specification_version: 3
140
142
  summary: Spread some Nutella on Ruby to sweeten up its functionality.
141
143
  test_files:
144
+ - spec/nutella/core_ext/hash_spec.rb
142
145
  - spec/nutella/core_ext/enumerable_spec.rb
143
146
  - spec/nutella/core_ext/string_spec.rb
144
147
  - spec/spec_helper.rb