public
Description: Ruby and Rails core extensions used by GitHub.
Clone URL: git://github.com/github/hubahuba.git
Search Repo:
first commit
defunkt (author)
Sat May 10 21:51:47 -0700 2008
commit  52a27da4d5aea71401bda44094fb6b4113b90b2c
tree    38cd27eb17194773d06fa9e01a5611aeff01731a
0
...
 
 
 
...
1
2
3
0
@@ -0,0 +1,3 @@
0
+This project is an extraction from GitHub.
0
+
0
+For this and other extractions, see http://github.com/github
...
 
 
 
...
1
2
3
0
@@ -0,0 +1,3 @@
0
+Dir[File.dirname(__FILE__) + "/lib/**/*.rb"].each do |file|
0
+ require file
0
+end
...
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
0
@@ -0,0 +1,10 @@
0
+class << ActiveRecord::Base
0
+ def each(limit = 1000)
0
+ rows = find(:all, :conditions => ["id > ?", 0], :limit => limit)
0
+ until rows.blank?
0
+ rows.each { |record| yield record }
0
+ rows = find(:all, :conditions => ["id > ?", rows.last.id], :limit => limit)
0
+ end
0
+ self
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
0
@@ -0,0 +1,9 @@
0
+module Enumerable
0
+ def map_with_index
0
+ result = []
0
+ self.each_with_index do |elt, idx|
0
+ result << yield(elt, idx)
0
+ end
0
+ result
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -0,0 +1,48 @@
0
+class Object
0
+ # Metaid == a few simple metaclass helper
0
+ # (See http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html.)
0
+ # The hidden singleton lurks behind everyone
0
+ def metaclass() class << self; self end end
0
+ def meta_eval(&blk) metaclass.instance_eval(&blk) end
0
+
0
+ # Adds methods to a metaclass
0
+ def meta_def(name, &blk)
0
+ meta_eval { define_method(name, &blk) }
0
+ end
0
+
0
+ # Defines an instance method within a class
0
+ def class_def(name, &blk)
0
+ class_eval { define_method(name, &blk) }
0
+ end
0
+
0
+ ##
0
+ # if ''.not.blank?
0
+ # http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html
2
+ define_method :not do
0
+ Not.new(self)
0
+ end
0
+
0
+ class Not
0
+ private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ }
0
+
0
+ def initialize(subject)
0
+ @subject = subject
0
+ end
0
+
0
+ def method_missing(sym, *args, &blk)
0
+ !@subject.send(sym,*args,&blk)
0
+ end
0
+ end
0
+
0
+ ##
0
+ # @person ? @person.name : nil
0
+ # vs
0
+ # @person.try(:name)
0
+ def try(method)
0
+ send method if respond_to? method
0
+ end
0
+
0
+ def class_attr_accessor(*attrs)
0
+ metaclass.send(:attr_accessor, *attrs)
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
0
@@ -0,0 +1,21 @@
0
+module Rails
0
+ def self.environment
0
+ ENV['RAILS_ENV'].to_s.downcase
0
+ end
0
+
0
+ def self.development?
0
+ environment == 'development'
0
+ end
0
+
0
+ def self.production?
0
+ environment == 'production'
0
+ end
0
+
0
+ def self.test?
0
+ environment == 'test'
0
+ end
0
+
0
+ def self.none?
0
+ environment.empty?
0
+ end
0
+end
...
 
 
 
 
 
...
1
2
3
4
5
0
@@ -0,0 +1,5 @@
0
+class String
0
+ def to_md5
0
+ Digest::MD5.hexdigest(self)
0
+ end
0
+end

Comments

  • evan Sun May 11 06:43:08 -0700 2008

    I thought class << was the devil.

  • defunkt Tue May 13 15:10:18 -0700 2008

    It is. Are you talking about metaclass?

  • therealadam Tue May 13 15:17:26 -0700 2008

    I bet he’s talking about your AR extension. Its what I came here to heckle you for as well.

  • defunkt Tue May 13 15:20:23 -0700 2008

    Oh, you guys should really read the blog post I wrote :) In the AR extension, the scope never changes—it’s always the metaclass. Using self << class within a class definition sucks precisely because it makes the scope ambiguous.

  • drnic Tue May 13 17:22:11 -0700 2008 at lib/object.rb L0

    why define_method and not just def not;.. end?

  • defunkt Tue May 13 17:33:08 -0700 2008 at lib/object.rb L0

    You’d have to ask Jay.

  • drnic Tue May 13 21:29:09 -0700 2008

    @defunkt – cargo culter.

  • rsanheim Wed May 14 05:26:36 -0700 2008

    drnic ftw