more_core_extensions 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,4 +2,6 @@ require 'more_core_extensions/version'
2
2
 
3
3
  require 'more_core_extensions/core_ext/array'
4
4
  require 'more_core_extensions/core_ext/hash'
5
+ require 'more_core_extensions/core_ext/module'
6
+ require 'more_core_extensions/core_ext/object'
5
7
  require 'more_core_extensions/core_ext/string'
@@ -1,3 +1,5 @@
1
+ require 'more_core_extensions/core_ext/array/element_counts'
2
+
1
3
  module MoreCoreExtensions
2
4
  module ArrayDuplicates
3
5
  #
@@ -5,7 +7,7 @@ module MoreCoreExtensions
5
7
  #
6
8
  # [1, 2, 3, 4, 2, 4].duplicates #=> [2, 4]
7
9
  def duplicates
8
- self.inject(Hash.new(0)) { |h, v| h[v] += 1; h }.reject { |k, v| v == 1 }.keys
10
+ element_counts.reject { |k, v| v == 1 }.keys
9
11
  end
10
12
  end
11
13
  end
@@ -0,0 +1,12 @@
1
+ module MoreCoreExtensions
2
+ module ArrayElementCounts
3
+ # Returns a Hash of each element to the count of those elements.
4
+ #
5
+ # [1, 2, 3, 1, 3, 1].counts # => {1 => 3, 2 => 1, 3 => 2}
6
+ def element_counts
7
+ each_with_object(Hash.new(0)) { |i, h| h[i] += 1 }
8
+ end
9
+ end
10
+ end
11
+
12
+ Array.send(:include, MoreCoreExtensions::ArrayElementCounts)
@@ -1,5 +1,6 @@
1
1
  require 'more_core_extensions/core_ext/array/deletes'
2
2
  require 'more_core_extensions/core_ext/array/duplicates'
3
+ require 'more_core_extensions/core_ext/array/element_counts'
3
4
  require 'more_core_extensions/core_ext/array/inclusions'
4
5
  require 'more_core_extensions/core_ext/array/nested'
5
6
  require 'more_core_extensions/core_ext/array/random'
@@ -0,0 +1,13 @@
1
+ module MoreCoreExtensions
2
+ module ModuleNamespace
3
+ #
4
+ # Returns an Array with the namespace to the current Module.
5
+ #
6
+ # Aaa::Bbb::Ccc::Ddd.namespace #=> ["Aaa", "Bbb", "Ccc", "Ddd"]
7
+ def namespace
8
+ name.to_s.split("::")
9
+ end
10
+ end
11
+ end
12
+
13
+ Module.send(:include, MoreCoreExtensions::ModuleNamespace)
@@ -0,0 +1 @@
1
+ require 'more_core_extensions/core_ext/module/namespace'
@@ -0,0 +1,27 @@
1
+ require 'more_core_extensions/core_ext/module/namespace'
2
+
3
+ module MoreCoreExtensions
4
+ module ObjectNamespace
5
+ #
6
+ # Returns whether or not the object is in the given namespace.
7
+ #
8
+ # Aaa::Bbb::Ccc::Ddd.in_namespace?(Aaa::Bbb) #=> true
9
+ # Aaa::Bbb::Ccc::Ddd.new.in_namespace?(Aaa::Bbb) #=> true
10
+ # Aaa::Bbb::Ccc::Eee.in_namespace?("Aaa::Bbb") #=> true
11
+ # Aaa::Bbb::Ccc::Eee.in_namespace?(Aaa::Bbb::Ccc::Ddd) #=> false
12
+ def in_namespace?(val)
13
+ val_ns = val.to_s.split("::")
14
+ val_ns == namespace[0, val_ns.length]
15
+ end
16
+
17
+ #
18
+ # Returns an Array with the namespace to an Instance.
19
+ #
20
+ # Aaa::Bbb::Ccc::Ddd.new.namespace #=> ["Aaa", "Bbb", "Ccc", "Ddd"]
21
+ def namespace
22
+ self.class.namespace
23
+ end
24
+ end
25
+ end
26
+
27
+ Object.send(:include, MoreCoreExtensions::ObjectNamespace)
@@ -0,0 +1 @@
1
+ require 'more_core_extensions/core_ext/object/namespace'
@@ -1,3 +1,3 @@
1
1
  module MoreCoreExtensions
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -0,0 +1,10 @@
1
+ require "spec_helper"
2
+
3
+ describe Array do
4
+ it "#element_counts" do
5
+ [].element_counts.should == {}
6
+ [1].element_counts.should == {1 => 1}
7
+ [nil].element_counts.should == {nil => 1}
8
+ [1, 2, 3, 1, 3, 1].element_counts.should == {1 => 3, 2 => 1, 3 => 2}
9
+ end
10
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ describe Class do
4
+ before do
5
+ module Aaa
6
+ module Bbb
7
+ module Ccc
8
+ class Ddd; end
9
+ module Eee; end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ context "in_namespace?" do
16
+ it "Class in Module" do
17
+ Aaa::Bbb::Ccc::Ddd.in_namespace?(Aaa::Bbb).should be_true
18
+ end
19
+
20
+ it "Module in Module" do
21
+ Aaa::Bbb::Ccc::Eee.in_namespace?("Aaa::Bbb").should be_true
22
+ end
23
+
24
+ it "Instance of Class" do
25
+ Aaa::Bbb::Ccc::Ddd.new.in_namespace?(Aaa::Bbb).should be_true
26
+ end
27
+
28
+ it "Not in namespace" do
29
+ Aaa::Bbb::Ccc::Eee.in_namespace?(Aaa::Bbb::Ccc::Ddd).should be_false
30
+ end
31
+ end
32
+
33
+ context "namespace" do
34
+ it "Class in Module" do
35
+ expect(Aaa::Bbb::Ccc::Ddd.namespace).to eq(["Aaa", "Bbb", "Ccc", "Ddd"])
36
+ end
37
+
38
+ it "Module in Module" do
39
+ expect(Aaa::Bbb::Ccc::Eee.namespace).to eq(["Aaa", "Bbb", "Ccc", "Eee"])
40
+ end
41
+
42
+ it "Instance of a Class" do
43
+ expect(Aaa::Bbb::Ccc::Ddd.new.namespace).to eq(["Aaa", "Bbb", "Ccc", "Ddd"])
44
+ end
45
+ end
46
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: more_core_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.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: 2013-12-11 00:00:00.000000000 Z
12
+ date: 2014-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -111,6 +111,7 @@ files:
111
111
  - lib/more_core_extensions/core_ext/array.rb
112
112
  - lib/more_core_extensions/core_ext/array/deletes.rb
113
113
  - lib/more_core_extensions/core_ext/array/duplicates.rb
114
+ - lib/more_core_extensions/core_ext/array/element_counts.rb
114
115
  - lib/more_core_extensions/core_ext/array/inclusions.rb
115
116
  - lib/more_core_extensions/core_ext/array/nested.rb
116
117
  - lib/more_core_extensions/core_ext/array/random.rb
@@ -119,6 +120,10 @@ files:
119
120
  - lib/more_core_extensions/core_ext/hash.rb
120
121
  - lib/more_core_extensions/core_ext/hash/deletes.rb
121
122
  - lib/more_core_extensions/core_ext/hash/nested.rb
123
+ - lib/more_core_extensions/core_ext/module.rb
124
+ - lib/more_core_extensions/core_ext/module/namespace.rb
125
+ - lib/more_core_extensions/core_ext/object.rb
126
+ - lib/more_core_extensions/core_ext/object/namespace.rb
122
127
  - lib/more_core_extensions/core_ext/shared/nested.rb
123
128
  - lib/more_core_extensions/core_ext/string.rb
124
129
  - lib/more_core_extensions/core_ext/string/formats.rb
@@ -127,6 +132,7 @@ files:
127
132
  - more_core_extensions.gemspec
128
133
  - spec/core_ext/array/deletes_spec.rb
129
134
  - spec/core_ext/array/duplicates_spec.rb
135
+ - spec/core_ext/array/element_counts_spec.rb
130
136
  - spec/core_ext/array/inclusions_spec.rb
131
137
  - spec/core_ext/array/nested_spec.rb
132
138
  - spec/core_ext/array/random_spec.rb
@@ -134,6 +140,7 @@ files:
134
140
  - spec/core_ext/array/tableize_spec.rb
135
141
  - spec/core_ext/hash/deletes_spec.rb
136
142
  - spec/core_ext/hash/nested_spec.rb
143
+ - spec/core_ext/object/namespace_spec.rb
137
144
  - spec/core_ext/string/formats_spec.rb
138
145
  - spec/core_ext/string/hex_dump_spec.rb
139
146
  - spec/spec_helper.rb
@@ -152,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
159
  version: '0'
153
160
  segments:
154
161
  - 0
155
- hash: 4076695105766579248
162
+ hash: -2354551733193230348
156
163
  required_rubygems_version: !ruby/object:Gem::Requirement
157
164
  none: false
158
165
  requirements:
@@ -161,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
168
  version: '0'
162
169
  segments:
163
170
  - 0
164
- hash: 4076695105766579248
171
+ hash: -2354551733193230348
165
172
  requirements: []
166
173
  rubyforge_project:
167
174
  rubygems_version: 1.8.23
@@ -172,6 +179,7 @@ summary: MoreCoreExtensions are a set of core extensions beyond those provided b
172
179
  test_files:
173
180
  - spec/core_ext/array/deletes_spec.rb
174
181
  - spec/core_ext/array/duplicates_spec.rb
182
+ - spec/core_ext/array/element_counts_spec.rb
175
183
  - spec/core_ext/array/inclusions_spec.rb
176
184
  - spec/core_ext/array/nested_spec.rb
177
185
  - spec/core_ext/array/random_spec.rb
@@ -179,6 +187,7 @@ test_files:
179
187
  - spec/core_ext/array/tableize_spec.rb
180
188
  - spec/core_ext/hash/deletes_spec.rb
181
189
  - spec/core_ext/hash/nested_spec.rb
190
+ - spec/core_ext/object/namespace_spec.rb
182
191
  - spec/core_ext/string/formats_spec.rb
183
192
  - spec/core_ext/string/hex_dump_spec.rb
184
193
  - spec/spec_helper.rb