Never been to TextSnippets 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 (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

SSH dynamic forward (Linux)

This command will create a dynamic forward from an SSH client to an SSH server. Basically what this does is allow you to use any local port (8080 in this example) as a proxy for any TCP application.

Feedback, suggestions and comments are all welcome!

# In the following example, we create a dynamic
# forward from port 8080 (client) to port 22 (host.net).

ssh -D 8080 username@host.net

# Now, we'll check out netstat to see what we
# have done.

netstat

# Active Internet connections (w/o servers)
# ...
# tcp 0 0 host.net:ssh client.com:60565 ESTABLISHED
# ...
#
# Awesome! Now we've got the connection. I'll add
# another post telling how to use this port as a
# socks proxy for any TCP application :)

Dynamic time based finder for ActiveRecord

I think it's pretty nice. Get all records within a railsified period of time (7.days.ago, 12.months.ago). I can do cool things like Workout.within_12_days or Workout.within_12_hours. Further conditions can be given as a hash, and will be transposed onto the end of the :conditions.

Workout.within_n_units(:user_id => 12) #-> Workout.find(:all, :conditions => 'created_at > n.units.ago and user_id = 12')


class Workout < ActiveRecord::Base
  class << self
    def within_n_temporal_units(number, units, further_conds={})
      conds = "created_at > '#{eval("#{number}.#{units}.ago").to_s(:db)}'"
      further_conds.each {|key, val| conds << " and #{key} = #{val}"}
      Workout.find(:all, :conditions => conds)
    end
    
    def method_missing(name, *args)
      return within_n_temporal_units($~[1], $~[2], args[0] || {}) if name.to_s =~ /within_(\d+)_(\w+)/
      raise NoMethodError, "undefined method `#{name}' for #{self}:Class"
    end
  end
end
« Newer Snippets
Older Snippets »
2 total  XML / RSS feed