commonthread-rails 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{commonthread-rails}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["CommonThread"]
12
- s.date = %q{2010-01-23}
12
+ s.date = %q{2010-01-25}
13
13
  s.description = %q{commonthread-rails is a collection of things that make rails development better for us. It includes date formats, monkey patches to String, Array and NilClass to make things nicer. An Encrypter using blowfish. Also, some rails filters}
14
14
  s.email = %q{hello@commonthread.com}
15
15
  s.extra_rdoc_files = [
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/commonthread/lipsum.rb",
32
32
  "lib/commonthread/monkey_patches.rb",
33
33
  "test/helper.rb",
34
- "test/test_commonthread-rails.rb"
34
+ "test/test_monkey_patches.rb"
35
35
  ]
36
36
  s.homepage = %q{http://github.com/commonthread/commonthread-rails}
37
37
  s.rdoc_options = ["--charset=UTF-8"]
@@ -40,7 +40,7 @@ Gem::Specification.new do |s|
40
40
  s.summary = %q{commonthread-rails is a collection of things that make rails development better for us}
41
41
  s.test_files = [
42
42
  "test/helper.rb",
43
- "test/test_commonthread-rails.rb"
43
+ "test/test_monkey_patches.rb"
44
44
  ]
45
45
 
46
46
  if s.respond_to? :specification_version then
@@ -1,4 +1,7 @@
1
1
  require 'commonthread/date_formats'
2
+
3
+ require 'cgi' unless defined?(CGI)
4
+ require 'digest' unless defined?(Digest)
2
5
  require 'commonthread/monkey_patches'
3
6
 
4
7
  gem 'crypt'
@@ -22,7 +22,7 @@ class String
22
22
  end
23
23
 
24
24
  def to_possesive
25
- self.last.downcase == 's' ? "#{self}'" : "#{self}'s"
25
+ self[-1].chr.downcase == 's' ? "#{self}'" : "#{self}'s"
26
26
  end
27
27
 
28
28
  def xml_strip
@@ -35,6 +35,35 @@ class String
35
35
 
36
36
  def to_pretty_url
37
37
  self.strip.downcase.gsub(/\s+/, '_').gsub(/[^\w_-]/, '')
38
+ end
39
+
40
+ def self.rand(length = 8)
41
+ alphabet = ('a' .. 'z').to_a + ('0' .. '9').to_a
42
+ rand_string = ''
43
+ length.times do
44
+ rand_string << alphabet.rand
45
+ end
46
+ rand_string
47
+ end
48
+
49
+ def email?
50
+ if self =~ /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
51
+ return true
52
+ else
53
+ return false
54
+ end
55
+ end
56
+
57
+ def phone?
58
+ return false if self.nil?
59
+ self.gsub(/[^0123456789]/, '').length > 9
60
+ end
61
+
62
+ def date?
63
+ Date.parse(self)
64
+ true
65
+ rescue
66
+ false
38
67
  end
39
68
  end
40
69
 
@@ -76,8 +105,12 @@ class NilClass
76
105
  end
77
106
 
78
107
  def to_s(format = nil)
79
- ''
108
+ ""
80
109
  end
110
+
111
+ def to_xs
112
+ ""
113
+ end
81
114
 
82
115
  def empty?
83
116
  true
@@ -118,5 +151,21 @@ if defined?(ActiveRecord)
118
151
  prefix ||= 'new' if self.new_record?
119
152
  [ prefix, self.class.name, self.id ].compact.join('_').downcase
120
153
  end
154
+
155
+ if defined?(Resque)
156
+ def self.queue
157
+ @queue || 'default'
158
+ end
159
+
160
+ # This will be called by a worker when a job needs to be processed
161
+ def self.perform(id, method, *args)
162
+ find(id).send(method, *args)
163
+ end
164
+
165
+ # We can pass this any Repository instance method that we want to run later.
166
+ def async(method, *args)
167
+ Resque.enqueue(self.class, self.id, method, *args)
168
+ end
169
+ end
121
170
  end
122
171
  end
@@ -0,0 +1,122 @@
1
+ require 'helper'
2
+
3
+ class TestMonkeyPatches < Test::Unit::TestCase
4
+ context "String patches" do
5
+ should "convert a string with a regex and template" do
6
+ assert_equal "?1234567890=abcdefg".convert('^.(.........)', '$1'), "123456789"
7
+ assert_equal "?1234567890=abcdefg".convert('^.(\d{9})', '$1'), "123456789"
8
+ assert_equal "?1234567890=abcdefg".convert('\?(\d+)=', '$1'), "1234567890"
9
+ end
10
+
11
+ should "escape/unescape strings for use in URLs" do
12
+ assert_equal "foo/bar,boo:car".escape, "foo%2Fbar%2Cboo%3Acar"
13
+ assert_equal "foo%2Fbar%2Cboo%3Acar".unescape, "foo/bar,boo:car"
14
+ end
15
+
16
+ should "hash strings with sha1 and md5" do
17
+ assert_equal "commonthread".to_sha, "dc8e89d0a8d2364c4c3d2231f63df6a792667de2"
18
+ assert_equal "commonthread".to_md5, "9a5c2ec77af09d5673a475566d4ccf14"
19
+ end
20
+
21
+ should "make possesive strings" do
22
+ assert_equal "chris".to_possesive, "chris'"
23
+ assert_equal "jason".to_possesive, "jason's"
24
+ end
25
+
26
+ should "strip xml" do
27
+ assert_equal "<tag>inner</tag>".xml_strip, "inner"
28
+ assert_equal "<tag>inner</tag> <other>text</inner>".xml_strip, "inner text"
29
+ end
30
+
31
+ should "wrap text" do
32
+ assert_equal "pipe me".wrap('|'), "|pipe me|"
33
+ assert_equal "bracket me".wrap('<','>'), "<bracket me>"
34
+ end
35
+
36
+ should "make pretty url fragments" do
37
+ assert_equal "commonthread rails".to_pretty_url, "commonthread_rails"
38
+ assert_equal "commonthread rails".to_pretty_url, "commonthread_rails"
39
+ assert_equal "commonthread_rails".to_pretty_url, "commonthread_rails"
40
+ assert_equal "commonthread-rails".to_pretty_url, "commonthread-rails"
41
+ assert_equal "commonthread-rails v0.1.1".to_pretty_url, "commonthread-rails_v011"
42
+ end
43
+ end
44
+
45
+ context "Array patches" do
46
+ setup do
47
+ @numbers = (1..99).to_a
48
+ @scores = [50, 75, 100]
49
+ @letters = ('A'..'Z').to_a
50
+ end
51
+
52
+ should "find random item" do
53
+ assert @numbers.rand.is_a?(Integer)
54
+ assert @letters.rand.is_a?(String)
55
+
56
+ random_number = @numbers.rand
57
+ assert @numbers.find{|n| n == random_number}
58
+
59
+ random_letter = @letters.rand
60
+ assert @letters.find{|l| l == random_letter}
61
+ end
62
+
63
+ should "find maximum value of block" do
64
+ assert_equal @numbers.maximum{|n| n}, 99
65
+ assert_equal @letters.maximum{|l| l}, 'Z'
66
+ end
67
+
68
+ should "find minimum value of block" do
69
+ assert_equal @numbers.minimum{|n| n}, 1
70
+ assert_equal @letters.minimum{|l| l}, 'A'
71
+ end
72
+
73
+ should "find average value of block" do
74
+ assert_equal @scores.average{|s| s}, 75
75
+ end
76
+ end
77
+
78
+ context "NilClass patches" do
79
+ should "not fail on each" do
80
+ assert_equal nil.each, nil
81
+ end
82
+
83
+ should "not fail on to_s" do
84
+ assert_equal nil.to_s, ""
85
+ assert_equal nil.to_s(:standard), ""
86
+ end
87
+
88
+ should "not fail on empty?" do
89
+ assert nil.empty?
90
+ end
91
+
92
+ should "not fail on blank?" do
93
+ assert nil.blank?
94
+ end
95
+ end
96
+
97
+ context "Object Patches" do
98
+ setup do
99
+ class Foo
100
+ def self.class_bar
101
+ "bar"
102
+ end
103
+
104
+ def instance_bar
105
+ "bar"
106
+ end
107
+ end
108
+
109
+ @foo = Foo.new
110
+ end
111
+
112
+ should "have my_method properties" do
113
+ assert_equal Foo.class_bar, "bar"
114
+ assert_equal @foo.instance_bar, "bar"
115
+
116
+ assert_equal Foo.my_methods, ["class_bar"]
117
+ assert_not_equal Foo.methods, ["class_bar"]
118
+ assert_equal @foo.my_methods, ["instance_bar"]
119
+ assert_not_equal @foo.methods, ["instance_bar"]
120
+ end
121
+ end
122
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commonthread-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - CommonThread
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-23 00:00:00 -06:00
12
+ date: 2010-01-25 00:00:00 -06:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -56,7 +56,7 @@ files:
56
56
  - lib/commonthread/lipsum.rb
57
57
  - lib/commonthread/monkey_patches.rb
58
58
  - test/helper.rb
59
- - test/test_commonthread-rails.rb
59
+ - test/test_monkey_patches.rb
60
60
  has_rdoc: true
61
61
  homepage: http://github.com/commonthread/commonthread-rails
62
62
  licenses: []
@@ -87,4 +87,4 @@ specification_version: 3
87
87
  summary: commonthread-rails is a collection of things that make rails development better for us
88
88
  test_files:
89
89
  - test/helper.rb
90
- - test/test_commonthread-rails.rb
90
+ - test/test_monkey_patches.rb
@@ -1,7 +0,0 @@
1
- require 'helper'
2
-
3
- class TestCommonthreadRails < 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