vidibus-core_extensions 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Andre Pankratz
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,72 @@
1
+ = vidibus-core_extensions
2
+
3
+ This gem is part of the open source SOA framework Vidibus: http://www.vidibus.org
4
+
5
+ It provides some extensions to the ruby core.
6
+
7
+
8
+ == Installation
9
+
10
+ Add the dependency to the Gemfile of your application:
11
+
12
+ gem "vidibus-core_extensions"
13
+
14
+ Then call bundle install on your console.
15
+
16
+
17
+ = Usage
18
+
19
+ == Array
20
+
21
+ === Array#flatten_once
22
+
23
+ Flattens first level of an array. Example:
24
+
25
+ [1, [2, [3]]].flatten_once # => [1, 2, [3]]
26
+
27
+
28
+ === Array#merge
29
+
30
+ Merges given array with current one.
31
+
32
+ It will perform insertion of new items by three rules:
33
+
34
+ 1. If the item's predecessor is present, insert item after it.
35
+ 2. If the item's follower is present, insert item before it.
36
+ 3. Insert item at end of list.
37
+
38
+ Examples:
39
+
40
+ [].merge([1, 2]) # => [1, 2]
41
+ ['a'].merge([1, 2]) # => ['a', 1, 2]
42
+ [1, 'a'].merge([1, 2]) # => [1, 2, 'a']
43
+ [1, 'a'].merge([3, 1, 2]) # => [3, 1, 2, 'a']
44
+
45
+
46
+ == Hash
47
+
48
+ === Hash#to_uri
49
+
50
+ Returns URL-encoded string of uri params. Examples:
51
+
52
+ { :some => :value, :another => "speciál" }.to_uri # => "some=value&another=speci%C3%A1l"
53
+ { :some => { :nested => :thing } }.to_uri # => "some=[nested=thing]"
54
+
55
+
56
+ === Hash#only
57
+
58
+ Returns a copy of self including only the given keys. Example:
59
+
60
+ { :name => "Rodrigo", :age => 21 }.only(:name) # => { :name => "Rodrigo" }
61
+
62
+
63
+ === Hash#except
64
+
65
+ Returns a copy of self including all but the given keys. Example:
66
+
67
+ { :name => "Rodrigo", :age = 21 }.except(:name) # => { :age => 21 }
68
+
69
+
70
+ == Copyright
71
+
72
+ Copyright (c) 2010 Andre Pankratz. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "vidibus-core_extensions"
8
+ gem.summary = %Q{Extends the ruby core.}
9
+ gem.description = %Q{Provides some extensions to the ruby core.}
10
+ gem.email = "andre@vidibus.com"
11
+ gem.homepage = "http://github.com/vidibus/vidibus-core_extensions"
12
+ gem.authors = ["Andre Pankratz"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |t|
28
+ t.spec_files = FileList['spec/vidibus/**/*_spec.rb']
29
+ t.rcov = true
30
+ t.rcov_opts = ['--exclude', '^spec,/gems/']
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+ task :default => :spec
35
+
36
+ require "rake/rdoctask"
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?("VERSION") ? File.read("VERSION") : ""
39
+ rdoc.rdoc_dir = "rdoc"
40
+ rdoc.title = "vidibus-uuid #{version}"
41
+ rdoc.rdoc_files.include("README*")
42
+ rdoc.rdoc_files.include("lib/**/*.rb")
43
+ rdoc.options << "--charset=utf-8"
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.1
@@ -0,0 +1,67 @@
1
+ module Vidibus
2
+ module CoreExtensions
3
+ module Array
4
+
5
+ # Flattens first level.
6
+ #
7
+ # Example:
8
+ #
9
+ # [1, [2, [3]]].flatten_once # => [1, 2, [3]]
10
+ #
11
+ # Inspired by: http://snippets.dzone.com/posts/show/302#comment-1417
12
+ #
13
+ def flatten_once
14
+ inject([]) do |v,e|
15
+ if e.is_a?(Array)
16
+ v.concat(e)
17
+ else
18
+ v << e
19
+ end
20
+ end
21
+ end
22
+
23
+ # Merges given array with current one.
24
+ #
25
+ # Will perform insertion of new items by three rules:
26
+ # 1. If the item's predecessor is present, insert item after it.
27
+ # 2. If the item's follower is present, insert item before it.
28
+ # 3. Insert item at end of list.
29
+ #
30
+ # Examples:
31
+ #
32
+ # [].merge([1, 2]) # => [1, 2]
33
+ # ['a'].merge([1, 2]) # => ['a', 1, 2]
34
+ # [1, 'a'].merge([1, 2]) # => [1, 2, 'a']
35
+ # [1, 'a'].merge([3, 1, 2]) # => [3, 1, 2, 'a']
36
+ #
37
+ def merge(array)
38
+ for value in array
39
+ next if include?(value)
40
+ if found = merging_predecessor(value, array)
41
+ position = index(found) + 1
42
+ elsif found = merging_follower(value, array)
43
+ position = index(found)
44
+ end
45
+ insert(position || length, value)
46
+ end
47
+ self
48
+ end
49
+
50
+ private
51
+
52
+ # Returns predecessor of given needle from haystack.
53
+ # Helper method for #merge
54
+ def merging_predecessor(needle, haystack)
55
+ list = haystack[0..haystack.index(needle)].reverse
56
+ (list & self).first
57
+ end
58
+
59
+ # Returns follower of given needle from haystack.
60
+ # Helper method for #merge
61
+ def merging_follower(needle, haystack)
62
+ list = haystack[haystack.index(needle)+1..-1]
63
+ (list & self).first
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,58 @@
1
+ require "uri"
2
+
3
+ module Vidibus
4
+ module CoreExtensions
5
+ module Hash
6
+
7
+ # Returns URL-encoded string of uri params.
8
+ #
9
+ # Examples:
10
+ #
11
+ # { :some => :value, :another => "speciál" }.to_uri # => "some=value&another=speci%C3%A1l"
12
+ # { :some => { :nested => :thing } }.to_uri # => "some=[nested=thing]"
13
+ #
14
+ def to_uri
15
+ list = self.to_a.map do |arg|
16
+ value = arg[1]
17
+ if value.is_a?(Hash)
18
+ value = "[#{value.to_uri}]"
19
+ else
20
+ value = URI.escape(value.to_s)
21
+ end
22
+ "#{URI.escape(arg[0].to_s)}=#{value}"
23
+ end
24
+ list.join("&")
25
+ end
26
+
27
+ # Returns a copy of self including only the given keys.
28
+ #
29
+ # Example:
30
+ #
31
+ # { :name => "Rodrigo", :age => 21 }.only(:name) # => { :name => "Rodrigo" }
32
+ #
33
+ # Inspired by:
34
+ # http://www.koders.com/ruby/fid80243BF76758F830B298E0E681B082B3408AB185.aspx?s=%22Rodrigo+Kochenburger%22#L9
35
+ # and
36
+ # http://snippets.dzone.com/posts/show/302
37
+ #
38
+ def only(*keys)
39
+ keys.flatten!
40
+ self.class[*self.select { |k,v| keys.include?(k) }.flatten_once]
41
+ end
42
+
43
+ # Returns a copy of self including all but the given keys.
44
+ #
45
+ # Example:
46
+ #
47
+ # { :name => "Rodrigo", :age = 21 }.except(:name) # => { :age => 21 }
48
+ #
49
+ # Inspired by:
50
+ # http://www.koders.com/ruby/fid80243BF76758F830B298E0E681B082B3408AB185.aspx?s=%22Rodrigo+Kochenburger%22#L9
51
+ #
52
+ def except(*keys)
53
+ keys.flatten!
54
+ self.class[*self.select { |k,v| !keys.include?(k) }.flatten_once]
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ require "core_extensions/hash"
2
+ require "core_extensions/array"
3
+
4
+ Hash.send :include, Vidibus::CoreExtensions::Hash
5
+ Array.send :include, Vidibus::CoreExtensions::Array
@@ -0,0 +1,2 @@
1
+ $:.unshift(File.join(File.dirname(__FILE__), "..", "lib", "vidibus"))
2
+ require "core_extensions"
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+ require "vidibus-core_extensions"
4
+ require "spec"
5
+ require "spec/autorun"
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::CoreExtensions::Array" do
4
+ describe "#flatten_once" do
5
+ it "should return array" do
6
+ array = ['go', 'for', 'it']
7
+ array.flatten_once.should eql(['go', 'for', 'it'])
8
+ end
9
+
10
+ it "should return a flattened array" do
11
+ array = ['go', ['for', 'it']]
12
+ array.flatten_once.should eql(['go', 'for', 'it'])
13
+ end
14
+
15
+ it "should flatten first level only" do
16
+ array = ['go', ['for', ['it']]]
17
+ array.flatten_once.should eql(['go', 'for', ['it']])
18
+ end
19
+
20
+ it "should accept array with mixed values" do
21
+ array = ["go", [1,2], { :it => "dude" }]
22
+ array.flatten_once.should eql(["go", 1, 2, { :it => "dude" }])
23
+ end
24
+ end
25
+
26
+ describe "#merge" do
27
+ it "should merge [] with [1,2]" do
28
+ [].merge([1,2]).should eql([1,2])
29
+ end
30
+
31
+ it "should merge [a] with [1,2]" do
32
+ ['a'].merge([1,2]).should eql(['a',1,2])
33
+ end
34
+
35
+ it "should merge [1,'a'] with [1,2]" do
36
+ [1,'a'].merge([1,2]).should eql([1,2,'a'])
37
+ end
38
+
39
+ it "should merge [1,'a'] with [3,1,2]" do
40
+ [1,'a'].merge([3,1,2]).should eql([3,1,2,'a'])
41
+ end
42
+
43
+ it "should merge ['b',1,'a'] with [3,1,2]" do
44
+ ['b',1,'a'].merge([3,1,2]).should eql(['b',3,1,2,'a'])
45
+ end
46
+
47
+ it "should merge [2,'b',1,'a'] with [3,1,2]" do
48
+ [2,'b',1,'a'].merge([3,1,2]).should eql([2,'b',3,1,'a'])
49
+ end
50
+
51
+ it "should merge [2,'b',1,'a'] with [3,1,2,4]" do
52
+ [2,'b',1,'a'].merge([3,1,2,4]).should eql([2,4,'b',3,1,'a'])
53
+ end
54
+
55
+ it "should merge [2,'b',1,'a'] with [5,3,6,7,1,2,4]" do
56
+ [2,'b',1,'a'].merge([5,3,6,7,1,2,4]).should eql([2,4,'b',5,3,6,7,1,'a'])
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ describe "Vidibus::CoreExtensions::Hash" do
4
+ describe "#to_uri" do
5
+ it "should join params with '&'" do
6
+ hash = { :some => "value", :another => "thing" }
7
+ hash.to_uri.should eql("some=value&another=thing")
8
+ end
9
+
10
+ it "should return items as urlencoded string" do
11
+ hash = { :another => "speciál" }
12
+ hash.to_uri.should eql("another=speci%C3%A1l")
13
+ end
14
+
15
+ it "should support multi-level hashes" do
16
+ hash = { :some => { :nested => :thing } }
17
+ hash.to_uri.should eql("some=[nested=thing]")
18
+ end
19
+ end
20
+
21
+ describe "#only" do
22
+ it "should return a copy of self but including only the given keys" do
23
+ hash = { :name => "rodrigo", :age => 21 }
24
+ hash.only(:name).should eql({ :name => "rodrigo" })
25
+ end
26
+
27
+ it "should work with array as parameter" do
28
+ hash = { :name => "rodrigo", :age => 21 }
29
+ hash.only([:name, :something]).should eql({ :name => "rodrigo" })
30
+ end
31
+
32
+ it "should work for nested hash" do
33
+ hash = { :name => "rodrigo", :girlfriends => ["Anna", "Maria"] }
34
+ hash.only(:name, :girlfriends).should eql({ :name => "rodrigo", :girlfriends => ["Anna", "Maria"] })
35
+ end
36
+ end
37
+
38
+ describe "#except" do
39
+ it "should return a copy of self but including only the given keys" do
40
+ hash = { :name => "rodrigo", :age => 21 }
41
+ hash.except(:name).should eql({ :age => 21 })
42
+ end
43
+
44
+ it "should work with array as parameter" do
45
+ hash = { :name => "rodrigo", :age => 21 }
46
+ hash.except([:name, :something]).should eql({ :age => 21 })
47
+ end
48
+
49
+ it "should work for nested hash" do
50
+ hash = { :name => "rodrigo", :girlfriends => ["Anna", "Maria"] }
51
+ hash.except(:name).should eql({ :girlfriends => ["Anna", "Maria"] })
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{vidibus-core_extensions}
8
+ s.version = "0.3.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Andre Pankratz"]
12
+ s.date = %q{2010-08-09}
13
+ s.description = %q{Provides some extensions to the ruby core.}
14
+ s.email = %q{andre@vidibus.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/vidibus-core_extensions.rb",
27
+ "lib/vidibus/core_extensions.rb",
28
+ "lib/vidibus/core_extensions/array.rb",
29
+ "lib/vidibus/core_extensions/hash.rb",
30
+ "spec/spec.opts",
31
+ "spec/spec_helper.rb",
32
+ "spec/vidibus/core_extensions/array_spec.rb",
33
+ "spec/vidibus/core_extensions/hash_spec.rb",
34
+ "vidibus-core_extensions.gemspec"
35
+ ]
36
+ s.homepage = %q{http://github.com/vidibus/vidibus-core_extensions}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Extends the ruby core.}
41
+ s.test_files = [
42
+ "spec/spec_helper.rb",
43
+ "spec/vidibus/core_extensions/array_spec.rb",
44
+ "spec/vidibus/core_extensions/hash_spec.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
53
+ else
54
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
58
+ end
59
+ end
60
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vidibus-core_extensions
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 3
9
+ - 1
10
+ version: 0.3.1
11
+ platform: ruby
12
+ authors:
13
+ - Andre Pankratz
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-09 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: Provides some extensions to the ruby core.
38
+ email: andre@vidibus.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - .gitignore
49
+ - LICENSE
50
+ - README.rdoc
51
+ - Rakefile
52
+ - VERSION
53
+ - lib/vidibus-core_extensions.rb
54
+ - lib/vidibus/core_extensions.rb
55
+ - lib/vidibus/core_extensions/array.rb
56
+ - lib/vidibus/core_extensions/hash.rb
57
+ - spec/spec.opts
58
+ - spec/spec_helper.rb
59
+ - spec/vidibus/core_extensions/array_spec.rb
60
+ - spec/vidibus/core_extensions/hash_spec.rb
61
+ - vidibus-core_extensions.gemspec
62
+ has_rdoc: true
63
+ homepage: http://github.com/vidibus/vidibus-core_extensions
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options:
68
+ - --charset=UTF-8
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ hash: 3
77
+ segments:
78
+ - 0
79
+ version: "0"
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.7
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Extends the ruby core.
96
+ test_files:
97
+ - spec/spec_helper.rb
98
+ - spec/vidibus/core_extensions/array_spec.rb
99
+ - spec/vidibus/core_extensions/hash_spec.rb