Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Zeke Sikelianos

« Newer Snippets
Older Snippets »
Showing 21-30 of 66 total

Find all rows for a certain day; Count days between two dates

# Find all rows created on a certain day; Rails apparently has a built-in :db string format
self.find(:all, :conditions => ["created_at >= ? AND created_at <= ?", day.beginning_of_day.to_s(:db), day.end_of_day.to_s(:db)])

# Find number of days between two dates
def days_between_dates(first, last)
  (YMD(last) - YMD(first))
end

def YMD(date)
  date.to_date.to_s.gsub("-", "").to_i
end

Sample Controller method for using Nico's Contacts gem

  def import
    begin 
      @sites = {"gmail"  => Contacts::Gmail, "yahoo" => Contacts::Yahoo, "hotmail" => Contacts::Hotmail}
      @contacts = @sites[params[:from]].new(params[:login], params[:password]).contacts
      @users , @no_users = [], []
      @contacts.each do |contact|
          if u = User.find(:first , :conditions => "accounts.email = '#{contact[1]}'" , :include =>[:account])
            @users << u 
          else
            @no_users << {:name => contact[0] , :email => contact[1]}
          end            
      end
      respond_to do |format|
        format.html {render :template => 'shared/_contact_list' , :layout => false}
	format.xml {render :xml => @contacts.to_xml}
      end	  
    end
    rescue  Contacts::AuthenticationError
      respond_to do |format|
        format.html {render :text => "Username and password do not match"}
	     format.xml {head :bad_request , :bad_account_login}
      end
    end 

svn merging

svn log | more
svn merge -r339:HEAD https://wush.net/svn/givezooks/trunk

Actionscript Perpetual easing

MovieClip.prototype.ease = function(prop, target, speed) {
	delta = this['_' + prop] - target;
	if (delta != 0) {
		this['_' + prop] = this['_' + prop] - (delta / speed);
		delta = this['_' + prop] - target;
		if (delta < .25 and delta > -.25) {
			this['_' + prop] = target;
			delta = 0;
		}
	}
};

File extension regex

# returns nil if not found or numerical index if found
"dog.jpg" =~ /.*(jpg|png|gif)$/

validates_format_of :image_url, :with => %r{\.(gif|jpg|png)$}i


Kill the cornball dock in Leopard

defaults write com.apple.dock no-glass -boolean YES
killall Dock

randomizing an array in ruby (the right way)

Snagged from http://www.rubyquiz.com/quiz113.html

# be sure to use sort_by rather than sort
quiz = (1..10).to_a
quiz.sort_by { rand }

find and replace text from the shell

Snagged from http://snippets.dzone.com/posts/show/116

find . -name '*.txt' -print0 |xargs -0 perl -pi -e 's/find/replace/g'

Python Flickr Backup Script (Untested)


# flickrbackup.py - Matt Croydon - http://postneo.com
import flickr # http://jamesclarke.info/projects/flickr/
import BitBucket # http://www.other10percent.com/?p=15
import urllib
me = flickr.people_findByUsername("postneo")
bucket = BitBucket.BitBucket("postneo-flickr")
page = 1
total_photos = found_photos = 0
while 1:
    try:
        photos = flickr.people_getPublicPhotos(me.id, 100, page)
        for photo in photos:
            total_photos = total_photos + 1
            if bucket.has_key("%s-%s" % (photo.title, photo.id)):
                pass # we already have this photo
            else:
                data = urllib.urlretrieve("http://static.flickr.com/%s/%s_%s_o.jpg" % (photo.server, photo.id, photo.secret), "flickr.jpg")
                bits = BitBucket.Bits(filename="flickr.jpg")
                bucket["%s-%s" % (photo.title, photo.id)] = bits
                print "saving %s" % photo.title
                found_photos = found_photos + 1
        page = page + 1
    except AttributeError:
        break # We probably got an empty bucket
print "Found %s photos, saved %s new photos" % (total_photos, found_photos)
« Newer Snippets
Older Snippets »
Showing 21-30 of 66 total