-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
- Loading branch information
There are no files selected for viewing
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 |
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 |
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 |
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 |
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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
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 |
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 |
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@defunkt – cargo culter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drnic ftw
why define_method and not just def not;.. end?