cool_extensions 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,7 @@
1
+ === 0.0.2 2010-03-08
2
+ * just needed to change version b/c gemcutter doesn't allow to repush a version
3
+
4
+ === 0.0.1 2010-03-08
5
+
6
+ * 1 major enhancement:
7
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,18 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ cool_extensions.gemspec
7
+ lib/cool_extensions.rb
8
+ lib/cool_extensions/array.rb
9
+ lib/cool_extensions/enumerable.rb
10
+ lib/cool_extensions/hash.rb
11
+ lib/cool_extensions/object.rb
12
+ script/console
13
+ script/destroy
14
+ script/generate
15
+ test/test_array_extensions.rb
16
+ test/test_cool_extensions.rb
17
+ test/test_fputs.rb
18
+ test/test_helper.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on cool_extensions, see http://cool_extensions.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,50 @@
1
+ = cool_extensions
2
+
3
+ * http://github.com/#{github_username}/#{project_name}
4
+
5
+ == DESCRIPTION:
6
+
7
+ Some interesting extensions to your favorite ruby object types
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Needs more testing
12
+ * Needs more testing
13
+ * Needs more testing
14
+
15
+ == SYNOPSIS:
16
+
17
+ TODO
18
+
19
+ == REQUIREMENTS:
20
+
21
+ * TODO
22
+
23
+ == INSTALL:
24
+
25
+ gem install cool_extensions
26
+
27
+ == LICENSE:
28
+
29
+ (The MIT License)
30
+
31
+ Copyright (c) 2010 FIXME full name
32
+
33
+ Permission is hereby granted, free of charge, to any person obtaining
34
+ a copy of this software and associated documentation files (the
35
+ 'Software'), to deal in the Software without restriction, including
36
+ without limitation the rights to use, copy, modify, merge, publish,
37
+ distribute, sublicense, and/or sell copies of the Software, and to
38
+ permit persons to whom the Software is furnished to do so, subject to
39
+ the following conditions:
40
+
41
+ The above copyright notice and this permission notice shall be
42
+ included in all copies or substantial portions of the Software.
43
+
44
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
45
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
46
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
47
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
48
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
49
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
50
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ require 'rubygems'
2
+ gem 'hoe', '>= 2.1.0'
3
+ require 'hoe'
4
+ require 'fileutils'
5
+ require './lib/cool_extensions'
6
+
7
+ Hoe.plugin :newgem
8
+ # Hoe.plugin :website
9
+ # Hoe.plugin :cucumberfeatures
10
+
11
+ # Generate all the Rake tasks
12
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
13
+ $hoe = Hoe.spec 'cool_extensions' do
14
+ self.developer 'Jason Amster', 'jayamster@gmail.com'
15
+ self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
16
+ self.rubyforge_name = self.name # TODO this is default value
17
+ self.extra_deps = [['activesupport','>= 2.0.2'], ['fastercsv'], ['shoulda']]
18
+
19
+
20
+ end
21
+
22
+ require 'newgem/tasks'
23
+ Dir['tasks/**/*.rake'].each { |t| load t }
24
+
25
+ # TODO - want other tests/tasks run by default? Add them to the list
26
+ # remove_task :default
27
+ # task :default => [:spec, :features]
28
+
29
+
30
+ namespace :j do
31
+ desc "Installing without SUDO for those with RVM"
32
+ task :install => [:manifest, :clean, :package] do
33
+ `gem install --local pkg/cool_extensions-0.0.1.gem `
34
+ end
35
+ end
36
+
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{cool_extensions}
5
+ s.version = "0.0.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jason Amster"]
9
+ s.date = %q{2010-03-17}
10
+ s.description = %q{Some interesting extensions to your favorite ruby object types}
11
+ s.email = ["jayamster@gmail.com"]
12
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "PostInstall.txt"]
13
+ s.files = ["History.txt", "Manifest.txt", "PostInstall.txt", "README.rdoc", "Rakefile", "cool_extensions.gemspec", "lib/cool_extensions.rb", "lib/cool_extensions/array.rb", "lib/cool_extensions/enumerable.rb", "lib/cool_extensions/hash.rb", "lib/cool_extensions/object.rb", "script/console", "script/destroy", "script/generate", "test/test_array_extensions.rb", "test/test_cool_extensions.rb", "test/test_fputs.rb", "test/test_helper.rb"]
14
+ s.homepage = %q{http://github.com/#{github_username}/#{project_name}}
15
+ s.post_install_message = %q{PostInstall.txt}
16
+ s.rdoc_options = ["--main", "README.rdoc"]
17
+ s.require_paths = ["lib"]
18
+ s.rubyforge_project = %q{cool_extensions}
19
+ s.rubygems_version = %q{1.3.6}
20
+ s.summary = %q{Some interesting extensions to your favorite ruby object types}
21
+ s.test_files = ["test/test_array_extensions.rb", "test/test_cool_extensions.rb", "test/test_fputs.rb", "test/test_helper.rb"]
22
+
23
+ if s.respond_to? :specification_version then
24
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
25
+ s.specification_version = 3
26
+
27
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.0.2"])
29
+ s.add_runtime_dependency(%q<fastercsv>, [">= 0"])
30
+ s.add_runtime_dependency(%q<shoulda>, [">= 0"])
31
+ s.add_development_dependency(%q<rubyforge>, [">= 2.0.3"])
32
+ s.add_development_dependency(%q<gemcutter>, [">= 0.5.0"])
33
+ s.add_development_dependency(%q<hoe>, [">= 2.5.0"])
34
+ else
35
+ s.add_dependency(%q<activesupport>, [">= 2.0.2"])
36
+ s.add_dependency(%q<fastercsv>, [">= 0"])
37
+ s.add_dependency(%q<shoulda>, [">= 0"])
38
+ s.add_dependency(%q<rubyforge>, [">= 2.0.3"])
39
+ s.add_dependency(%q<gemcutter>, [">= 0.5.0"])
40
+ s.add_dependency(%q<hoe>, [">= 2.5.0"])
41
+ end
42
+ else
43
+ s.add_dependency(%q<activesupport>, [">= 2.0.2"])
44
+ s.add_dependency(%q<fastercsv>, [">= 0"])
45
+ s.add_dependency(%q<shoulda>, [">= 0"])
46
+ s.add_dependency(%q<rubyforge>, [">= 2.0.3"])
47
+ s.add_dependency(%q<gemcutter>, [">= 0.5.0"])
48
+ s.add_dependency(%q<hoe>, [">= 2.5.0"])
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ Dir.glob(File.expand_path(File.dirname(__FILE__))+'/cool_extensions/*').each do |extension|
5
+ require extension
6
+ end
7
+
8
+ module CoolExtensions
9
+ VERSION = '0.0.2'
10
+ end
11
+
12
+
13
+
@@ -0,0 +1,56 @@
1
+ class Array
2
+
3
+ # # Example
4
+ #
5
+ # class Person
6
+ # attr_accessor :type, :name, :source
7
+ # def initialize(options)
8
+ # @type=options[:type]
9
+ # @name=options[:name]
10
+ # @source=options[:source]
11
+ # end
12
+ # end
13
+ #
14
+ # type_sort_list = %w(User Admin Salesman)
15
+ # source_sort_list = %w(web email direct mail television)
16
+ # myarr = []
17
+ # myarr << Person.new(:source=>"direct", :type=>"Admin", :name=>"Jason")
18
+ # myarr << Person.new(:source=>"mail", :type=>"User", :name=>"Xavier")
19
+ # myarr << Person.new(:source=>"email", :type=>"User", :name=>"Josh")
20
+ # myarr << Person.new(:source=>"email", :type=>"Salesman", :name=>"Pablo")
21
+ # myarr << Person.new(:source=>"web", :type=>"User", :name=>"Pat")
22
+ # myarr << Person.new(:source=>"television", :type=>"Salesman", :name=>"Ross")
23
+ #
24
+ # # Sort by type
25
+ # sorted_by_type_myarr = myarr.sort_by_list(type_sort_list) do |item|
26
+ # item.type
27
+ # end
28
+ # puts sorted_by_type_myarr.to_yaml
29
+ #
30
+ #
31
+ # # Sort by source
32
+ # sorted_by_source_myarr = myarr.sort_by_list(source_sort_list) do |item|
33
+ # item.source
34
+ # end
35
+ # puts sorted_by_source_myarr.to_yaml
36
+
37
+ def sort_by_list(ordered_list, &block)
38
+ new_ol = ordered_list.inject({}){|new_hash, sort_item| new_hash[sort_item] = ordered_list.index(sort_item); new_hash}
39
+ sorted = self.map{|item| [item, new_ol[block.call(item)] || 0]}.sort_by{|item| item[1]}.map{|item| item[0]}
40
+ end
41
+
42
+ def uniquify(&block)
43
+ items = []
44
+ comparables = self.map{|item| block.call(item)}
45
+ added_list = []
46
+ self.each do |item|
47
+ parsed = block.call(item)
48
+ unless added_list.include?(parsed)
49
+ added_list << parsed
50
+ items << item
51
+ end
52
+ end
53
+ items
54
+ end
55
+
56
+ end
@@ -0,0 +1,5 @@
1
+ module Enumerable
2
+ def group_count
3
+ self.group_by{|u| yield(u)}.inject({}){|hash, group| hash[group[0]]= group[1].length;hash }
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ class Hash
2
+
3
+ def largest_key
4
+ rows =[]
5
+ keys.each_with_index do |f, i|
6
+ rows << [f.to_s.length, i]
7
+ end
8
+ index = rows.sort_by{|x| x[0]}.last[1]
9
+ keys[index]
10
+ end
11
+
12
+ def rowized
13
+ max_key_length = largest_key.length
14
+ keys.map do |key|
15
+ "#{key.to_s.rjust(max_key_length)}: #{self[key]}\n"
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,18 @@
1
+ class Object
2
+ def fputs(string)
3
+ # move cursor to beginning of line
4
+ cr = "\r"
5
+
6
+ # ANSI escape code to clear line from cursor to end of line
7
+ # "\e" is an alternative to "\033"
8
+ # cf. http://en.wikipedia.org/wiki/ANSI_escape_code
9
+
10
+ clear = "\e[0K"
11
+
12
+ # reset lines
13
+ reset = cr + clear
14
+ print "#{reset}#{string}"
15
+ $stdout.flush
16
+
17
+ end
18
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/cool_extensions.rb'}"
9
+ puts "Loading cool_extensions gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,69 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require "activesupport"
3
+
4
+ class Person
5
+ attr_accessor :name, :age, :eye_color
6
+ def initialize(_name, _age, _eye_color)
7
+ @name = _name
8
+ @age = _age
9
+ @eye_color = _eye_color
10
+ end
11
+ end
12
+
13
+ class TestArrayExtensions < Test::Unit::TestCase
14
+
15
+ context "An array with objects" do
16
+
17
+ setup do
18
+ @people = []
19
+ # Setup an array of users with names and ages
20
+ [["Jason", 31, "brown"],
21
+ ["Ross", 28, 'blue'],
22
+ ["Josh", 29, 'green'],
23
+ ["Patrick", 24, 'blue'],
24
+ ["Danny", 28, 'brown']].each do |name, age, eye_color|
25
+ person = eval("@#{name.downcase} = Person.new('#{name}', #{age}, '#{eye_color}')")
26
+ @people << person
27
+ end
28
+ end
29
+
30
+ should "be sortable by a names" do
31
+ sort_list = %w(Ross Patrick Josh Jason Danny) #different from current array
32
+ sorted_people = @people.sort_by_list(sort_list) do |person|
33
+ person.name
34
+ end
35
+
36
+ assert sorted_people.first == @ross
37
+ assert sorted_people[1] == @patrick
38
+ assert sorted_people[-1] == @danny
39
+
40
+
41
+ end
42
+
43
+ should 'be sortable by eye color' do
44
+ sort_list = %w(blue green brown) #different from current array
45
+ sorted_people = @people.sort_by_list(sort_list) do |person|
46
+ person.eye_color
47
+ end
48
+
49
+ assert sorted_people[0..1].include?(@patrick)
50
+ assert sorted_people[0..1].include?(@ross)
51
+ assert sorted_people[2] == @josh
52
+ assert sorted_people[3..4].include?(@jason)
53
+ assert sorted_people[3..4].include?(@danny)
54
+
55
+ end
56
+
57
+ should "have distinct items based upon an attribute" do
58
+ by_name = @people.uniquify{|person| person.name}
59
+ by_age = @people.uniquify{|person| person.age}
60
+ by_eye_color = @people.uniquify{|person| person.eye_color}
61
+
62
+ assert by_name.length == 5
63
+ assert by_age.length == 4
64
+ assert by_eye_color.length == 3
65
+
66
+ end
67
+ end
68
+
69
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestCoolExtensions < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require "test_helper"
2
+
3
+ # This is a visual test to make sure that fputs does stay on one line. You can
4
+ # Run this test from teh command line by navigating to the tests folder
5
+ # and running `ruby jtest.rb`
6
+ 100.times do |i|
7
+ fputs ((i+1)/100.to_f).to_s
8
+ sleep 0.05
9
+ end
@@ -0,0 +1,5 @@
1
+ require "rubygems"
2
+ require 'stringio'
3
+ require 'test/unit'
4
+ require File.dirname(__FILE__) + '/../lib/cool_extensions'
5
+ require 'shoulda'
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cool_extensions
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: ruby
11
+ authors:
12
+ - Jason Amster
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-17 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 2
31
+ version: 2.0.2
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: fastercsv
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: shoulda
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
55
+ version: "0"
56
+ type: :runtime
57
+ version_requirements: *id003
58
+ - !ruby/object:Gem::Dependency
59
+ name: rubyforge
60
+ prerelease: false
61
+ requirement: &id004 !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ segments:
66
+ - 2
67
+ - 0
68
+ - 3
69
+ version: 2.0.3
70
+ type: :development
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: gemcutter
74
+ prerelease: false
75
+ requirement: &id005 !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ - 5
82
+ - 0
83
+ version: 0.5.0
84
+ type: :development
85
+ version_requirements: *id005
86
+ - !ruby/object:Gem::Dependency
87
+ name: hoe
88
+ prerelease: false
89
+ requirement: &id006 !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ segments:
94
+ - 2
95
+ - 5
96
+ - 0
97
+ version: 2.5.0
98
+ type: :development
99
+ version_requirements: *id006
100
+ description: Some interesting extensions to your favorite ruby object types
101
+ email:
102
+ - jayamster@gmail.com
103
+ executables: []
104
+
105
+ extensions: []
106
+
107
+ extra_rdoc_files:
108
+ - History.txt
109
+ - Manifest.txt
110
+ - PostInstall.txt
111
+ files:
112
+ - History.txt
113
+ - Manifest.txt
114
+ - PostInstall.txt
115
+ - README.rdoc
116
+ - Rakefile
117
+ - cool_extensions.gemspec
118
+ - lib/cool_extensions.rb
119
+ - lib/cool_extensions/array.rb
120
+ - lib/cool_extensions/enumerable.rb
121
+ - lib/cool_extensions/hash.rb
122
+ - lib/cool_extensions/object.rb
123
+ - script/console
124
+ - script/destroy
125
+ - script/generate
126
+ - test/test_array_extensions.rb
127
+ - test/test_cool_extensions.rb
128
+ - test/test_fputs.rb
129
+ - test/test_helper.rb
130
+ has_rdoc: true
131
+ homepage: http://github.com/#{github_username}/#{project_name}
132
+ licenses: []
133
+
134
+ post_install_message: PostInstall.txt
135
+ rdoc_options:
136
+ - --main
137
+ - README.rdoc
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ segments:
145
+ - 0
146
+ version: "0"
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ segments:
152
+ - 0
153
+ version: "0"
154
+ requirements: []
155
+
156
+ rubyforge_project: cool_extensions
157
+ rubygems_version: 1.3.6
158
+ signing_key:
159
+ specification_version: 3
160
+ summary: Some interesting extensions to your favorite ruby object types
161
+ test_files:
162
+ - test/test_array_extensions.rb
163
+ - test/test_cool_extensions.rb
164
+ - test/test_fputs.rb
165
+ - test/test_helper.rb