This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

first commit

  • Loading branch information...
defunkt committed May 11, 2008
0 parents commit 52a27da4d5aea71401bda44094fb6b4113b90b2c
Showing with 99 additions and 0 deletions.
  1. +3 −0 README
  2. +3 −0 init.rb
  3. +10 −0 lib/active_record.rb
  4. +9 −0 lib/enumerable.rb
  5. +48 −0 lib/object.rb
  6. +21 −0 lib/rails.rb
  7. +5 −0 lib/string.rb
3 README
@@ -0,0 +1,3 @@
+This project is an extraction from GitHub.
+
+For this and other extractions, see http://github.com/github
@@ -0,0 +1,3 @@
+Dir[File.dirname(__FILE__) + "/lib/**/*.rb"].each do |file|
+ require file
+end
@@ -0,0 +1,10 @@
+class << ActiveRecord::Base
+ def each(limit = 1000)
+ rows = find(:all, :conditions => ["id > ?", 0], :limit => limit)
+ until rows.blank?
+ rows.each { |record| yield record }
+ rows = find(:all, :conditions => ["id > ?", rows.last.id], :limit => limit)
+ end
+ self
+ end
+end
@@ -0,0 +1,9 @@
+module Enumerable
+ def map_with_index
+ result = []
+ self.each_with_index do |elt, idx|
+ result << yield(elt, idx)
+ end
+ result
+ end
+end
@@ -0,0 +1,48 @@
+class Object
+ # Metaid == a few simple metaclass helper
+ # (See http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html.)
+ # The hidden singleton lurks behind everyone
+ def metaclass() class << self; self end end
+ def meta_eval(&blk) metaclass.instance_eval(&blk) end
+
+ # Adds methods to a metaclass
+ def meta_def(name, &blk)
+ meta_eval { define_method(name, &blk) }
+ end
+
+ # Defines an instance method within a class
+ def class_def(name, &blk)
+ class_eval { define_method(name, &blk) }
+ end
+
+ ##
+ # if ''.not.blank?
+ # http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html
+ define_method :not do

This comment has been minimized.

Show comment Hide comment
@drnic

drnic May 14, 2008

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

@drnic

drnic May 14, 2008

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

This comment has been minimized.

Show comment Hide comment
@defunkt

defunkt May 14, 2008

Member

You’d have to ask Jay.

@defunkt

defunkt May 14, 2008

Member

You’d have to ask Jay.

+ Not.new(self)
+ end
+
+ class Not
+ private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ }
+
+ def initialize(subject)
+ @subject = subject
+ end
+
+ def method_missing(sym, *args, &blk)
+ !@subject.send(sym,*args,&blk)
+ end
+ end
+
+ ##
+ # @person ? @person.name : nil
+ # vs
+ # @person.try(:name)
+ def try(method)
+ send method if respond_to? method
+ end
+
+ def class_attr_accessor(*attrs)
+ metaclass.send(:attr_accessor, *attrs)
+ end
+end
@@ -0,0 +1,21 @@
+module Rails
+ def self.environment
+ ENV['RAILS_ENV'].to_s.downcase
+ end
+
+ def self.development?
+ environment == 'development'
+ end
+
+ def self.production?
+ environment == 'production'
+ end
+
+ def self.test?
+ environment == 'test'
+ end
+
+ def self.none?
+ environment.empty?
+ end
+end
@@ -0,0 +1,5 @@
+class String
+ def to_md5
+ Digest::MD5.hexdigest(self)
+ end
+end

6 comments on commit 52a27da

@evan

This comment has been minimized.

Show comment Hide comment
@evan

evan May 11, 2008

I thought class << was the devil.

evan replied May 11, 2008

I thought class << was the devil.

@defunkt

This comment has been minimized.

Show comment Hide comment
@defunkt

defunkt May 13, 2008

Member

It is. Are you talking about metaclass?

Member

defunkt replied May 13, 2008

It is. Are you talking about metaclass?

@therealadam

This comment has been minimized.

Show comment Hide comment
@therealadam

therealadam May 13, 2008

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

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

@defunkt

This comment has been minimized.

Show comment Hide comment
@defunkt

defunkt May 13, 2008

Member

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.

Member

defunkt replied May 13, 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

This comment has been minimized.

Show comment Hide comment
@drnic

drnic May 14, 2008

@defunkt – cargo culter.

@defunkt – cargo culter.

@rsanheim

This comment has been minimized.

Show comment Hide comment
@rsanheim

rsanheim May 14, 2008

drnic ftw

drnic ftw

Please sign in to comment.