Skip to content
This repository has been archived by the owner on Nov 1, 2017. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
defunkt committed May 11, 2008
0 parents commit 52a27da
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This project is an extraction from GitHub.

For this and other extractions, see http://github.com/github
3 changes: 3 additions & 0 deletions init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Dir[File.dirname(__FILE__) + "/lib/**/*.rb"].each do |file|
require file
end
10 changes: 10 additions & 0 deletions lib/active_record.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions lib/enumerable.rb
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions lib/object.rb
Original file line number Diff line number Diff line change
@@ -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.

Copy link
@drnic

drnic May 14, 2008

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

This comment has been minimized.

Copy link
@defunkt

defunkt May 14, 2008

Author Contributor

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
21 changes: 21 additions & 0 deletions lib/rails.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lib/string.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class String
def to_md5
Digest::MD5.hexdigest(self)
end
end

6 comments on commit 52a27da

@evan
Copy link

@evan evan commented on 52a27da May 11, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought class << was the devil.

@defunkt
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is. Are you talking about metaclass?

@therealadam
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@defunkt
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

@drnic drnic commented on 52a27da May 14, 2008

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@defunkt – cargo culter.

@rsanheim
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drnic ftw

Please sign in to comment.