bad_ass_extensions 0.1.0

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
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Jason Amster
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,23 @@
1
+ = bad_ass_extensions
2
+
3
+ Bad Ass Extensions are a set of standard library extensions and tools I use on a regular basis.
4
+
5
+ == Contributing to bad_ass_extensions
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == TODO
16
+
17
+ needs some serious testing
18
+
19
+ == Copyright
20
+
21
+ Copyright (c) 2011 Jason Amster. See LICENSE.txt for
22
+ further details.
23
+
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "bad_ass_extensions"
16
+ gem.homepage = "http://github.com/jamster/bad_ass_extensions"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Some bad ass extensions I use all the time}
19
+ gem.description = %Q{Some bad ass extensions I use all the time}
20
+ gem.email = "jayamster@gmail.com"
21
+ gem.authors = ["Jason Amster"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "bad_ass_extensions #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -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__))+'/bad_ass_extensions/*').each do |extension|
5
+ require extension
6
+ end
7
+
8
+ module BadAssExtensions
9
+ VERSION = '0.1.0'
10
+ end
11
+
12
+
13
+
@@ -0,0 +1,86 @@
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
+
43
+ ## Frequency takes the existing array and returns an array of frequencies of
44
+ # the items within the array
45
+ #
46
+ # [1, 1, 3, 3, 4, 4, 4, 5].frequency_list => [[1, 2], [3,2], [4,3], [5,1]]
47
+ #
48
+ # or if you want to do it by object property
49
+ #
50
+ # class Person
51
+ # attr_accessor
52
+ # def initalizse(name)
53
+ # @name=name
54
+ # end
55
+ # end
56
+ #
57
+ # freqs = [Person.new("Jay")
58
+ # Person.new("Jay"),
59
+ # Person.new("Joe"),
60
+ # Person.new("Mike")].frequency_list{|x| x.name} => [[<Person:Jay>, 2], [<Person:Jay>, 1], [<Person:Jay>, 1] ]
61
+ #
62
+ #
63
+ #
64
+ def frequency_list(&block)
65
+ if block
66
+ return self.group_by{|elem| block.call(elem)}.inject([]){|arr, group| arr << [group[0], group[1].length];arr}
67
+ else
68
+ return self.group_by{|elem| elem}.inject([]){|arr, group| arr << [group[0], group[1].length];arr}
69
+ end
70
+ end
71
+
72
+ def uniquify(&block)
73
+ items = []
74
+ comparables = self.map{|item| block.call(item)}
75
+ added_list = []
76
+ self.each do |item|
77
+ parsed = block.call(item)
78
+ unless added_list.include?(parsed)
79
+ added_list << parsed
80
+ items << item
81
+ end
82
+ end
83
+ items
84
+ end
85
+
86
+ end
@@ -0,0 +1,32 @@
1
+ # EXTRACTED FROM THIS THREAD:
2
+ # http://rails.lighthouseapp.com/projects/8994/tickets/879-finding-the-days-weeks-months-years-between-two-dates
3
+ require "active_support" rescue (require 'activesupport')
4
+
5
+ class Date
6
+ def self.days_between(start, finish)
7
+ (finish - start).to_i
8
+ end
9
+
10
+ def self.weeks_between(start, finish)
11
+ days_between(start, finish) / 7
12
+ end
13
+
14
+ def self.months_between(start, finish)
15
+ if start.year != finish.year
16
+ difference = (12 - start.month) + (12 * years_between(start, finish)) + finish.month
17
+ else
18
+ difference = finish.month - start.month
19
+ end
20
+ difference -= 1 if finish.day < start.day
21
+ difference
22
+ end
23
+
24
+ def self.years_between(start, finish)
25
+ difference = finish.year - start.year
26
+ difference -= 1 if difference > 0 && (finish.month < start.month || finish.day < start.day)
27
+ difference
28
+ end
29
+ end
30
+
31
+ #=> Date.years_between(Date.parse('04/26/78', "%m/%d/%Y"), Date.today)
32
+ #=> 30
@@ -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,82 @@
1
+ class Histogram
2
+ def initialize(data, options={})
3
+
4
+ if data.is_a?(Array)
5
+ puts "Array"
6
+ @hash = data.group_by{|x| x}.inject({}){|hash, val| hash[val[0]] = val[1].length;hash}
7
+ elsif data.is_a?(Hash) && data[data.keys.first].is_a?(Array)
8
+ puts "Hash of Array"
9
+ @hash = data.inject({}){|hash, val| hash[val[0]] = val[1].length;hash}
10
+ elsif data.is_a?(Hash) && data[data.keys.first].is_a?(Integer)
11
+ puts "Hash of Integer"
12
+ @hash = data
13
+ elsif data.is_a?(Hash) && data[data.keys.first].is_a?(String) && data[data.keys.first].to_i.is_a?(Integer)
14
+ puts "Hash of Integer String"
15
+ @hash = data.inject({}){|hash, val| hash[val[0]] = val[1].to_i;hash}
16
+ else
17
+ raise "Need to be throwing out an array of item, a hash where each val is an array or a hash where each val is an integer"
18
+ end
19
+ puts @hash.inspect
20
+
21
+ @title = options[:title] || "Histogram by Jay"
22
+ @hist_height = options[:height] || 100
23
+ @total = @hash.inject(0){|sum, val| sum += val[1];sum}
24
+ @sorted_keys = @hash.keys.sort
25
+ @max_key_length = @sorted_keys.map{|x| x.to_s.length}.max
26
+ @max_int_length = @hash.map{|k,v| v.to_s.length}.max
27
+ @hash_modified = @hash.inject({}){|hash, val| hash[val[0]] = ((val[1].to_f/@total)*@hist_height);hash}
28
+ end
29
+
30
+ def pad_string(string, max_length)
31
+ len = string.to_s.length
32
+ pad = max_length - len
33
+ string.to_s + (" " * pad)
34
+ end
35
+
36
+ def sorted_padded_keys(string_array)
37
+ max_length = string_array.map{|x| x.length}.max
38
+ new_arry = string_array.inject([]) do |arr, val|
39
+ len = val.length
40
+ pad = max_length - len
41
+ arr << val + (" " * pad)
42
+ arr
43
+ end
44
+ new_arry.sort
45
+ end
46
+
47
+ def draw(options={})
48
+ draw_title(@title)
49
+ value = options[:value] || nil
50
+ percent = options[:percent] || nil
51
+
52
+ @sorted_keys.each do |key|
53
+ value = pad_string(@hash[key].to_s, @max_int_length) if value
54
+ percent = clean_percent(@hash_modified[key]) if percent
55
+ puts "#{pad_string(key, @max_key_length)}\t#{value}\t#{percent}\t#{'*' * (@hash_modified[key]+1)}"
56
+ end
57
+ end
58
+
59
+ def clean_percent(value)
60
+ int = value.to_i
61
+ int = "%2d" % int # "%02d" if you want to pad with zeros
62
+ prec = (value - int.to_i).to_s[2..5]
63
+ "#{int}.#{prec}"
64
+ end
65
+
66
+ def draw_title(string, options={})
67
+ pad_char = options[:pad_char] || '-'
68
+ puts pad_char * (string.length + 6)
69
+ puts "#{pad_char}#{pad_char} " + string + " #{pad_char}#{pad_char}"
70
+ puts pad_char * (string.length + 6)
71
+ end
72
+ end
73
+
74
+ ## USAGE
75
+
76
+ # array = [1, 1, 3, 3, 3, 4,4, 4, 4,4, 4, 4,4,4, 4,4 ,4, 5,6,6,6, 6,6,6 ,67, 7, 78]
77
+ # Histogram.new(array).draw
78
+ #
79
+ # hashy = {"Trial cancelation confirmation"=>"51", "Monitor system activated"=>"716", "No zendesk message"=>"3", "Zendesk message"=>"3", "Cancelation refund confirmation"=>"5", "Plan change confirmation"=>"1", "Subscription signup notification"=>"161", "Mobile report"=>"7", "Reset password"=>"28", "Cancelation confirmation"=>"11", "New member"=>"161"}
80
+ # Histogram.new(hashy).draw
81
+ # hashy2 = {'aasdf' => 10, 'aaaaaaaaaeeb' => 50, 'c' => 100, 'd' => 20}
82
+ # Histogram.new(hashy2, :title => 'Big Testing Title').draw(:value => true, :percent => true)
@@ -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/test/helper.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'bad_ass_extensions'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestBadAssExtensions < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bad_ass_extensions
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jason Amster
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-21 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ type: :development
24
+ name: shoulda
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ type: :development
38
+ name: bundler
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 23
45
+ segments:
46
+ - 1
47
+ - 0
48
+ - 0
49
+ version: 1.0.0
50
+ requirement: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ prerelease: false
53
+ type: :development
54
+ name: jeweler
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 7
61
+ segments:
62
+ - 1
63
+ - 5
64
+ - 2
65
+ version: 1.5.2
66
+ requirement: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ prerelease: false
69
+ type: :development
70
+ name: rcov
71
+ version_requirements: &id004 !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
+ requirement: *id004
81
+ description: Some bad ass extensions I use all the time
82
+ email: jayamster@gmail.com
83
+ executables: []
84
+
85
+ extensions: []
86
+
87
+ extra_rdoc_files:
88
+ - LICENSE.txt
89
+ - README.rdoc
90
+ files:
91
+ - .document
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.rdoc
95
+ - Rakefile
96
+ - VERSION
97
+ - lib/bad_ass_extensions.rb
98
+ - lib/bad_ass_extensions/array.rb
99
+ - lib/bad_ass_extensions/date.rb
100
+ - lib/bad_ass_extensions/enumerable.rb
101
+ - lib/bad_ass_extensions/hash.rb
102
+ - lib/bad_ass_extensions/histogram.rb
103
+ - lib/bad_ass_extensions/object.rb
104
+ - test/helper.rb
105
+ - test/test_bad_ass_extensions.rb
106
+ has_rdoc: true
107
+ homepage: http://github.com/jamster/bad_ass_extensions
108
+ licenses:
109
+ - MIT
110
+ post_install_message:
111
+ rdoc_options: []
112
+
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ hash: 3
130
+ segments:
131
+ - 0
132
+ version: "0"
133
+ requirements: []
134
+
135
+ rubyforge_project:
136
+ rubygems_version: 1.3.7
137
+ signing_key:
138
+ specification_version: 3
139
+ summary: Some bad ass extensions I use all the time
140
+ test_files:
141
+ - test/helper.rb
142
+ - test/test_bad_ass_extensions.rb