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

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Howto: setup the rails environment in a .rb script

Stolen from: http://www.railsonwave.com/railsonwave/2007/2/14/howto-setup-the-rails-environment-in-a-rb-script
I quote:

It can happen that you need to perform some operations on your rails application from a script, for example you may need to have a cron job that do something on your models. To archieve this goal you need to set up a rails environment in your script; it’s really easy, you’ve just to put these lines on the top of your .rb file.
RAILS_ROOT = RELATIVEPATHTOYOURAPPFOLDER
require RAILS_ROOT + "/config/environment" 
Dependencies.load_file("application.rb")

I really don’t know why but you have to manually load application.rb, if you don’t rails translate the ‘ApplicationController’ constant into ‘application_controller’ and look for a file named ‘application_controller.rb’ that doesn’t exist.

Sandro.

From the comments:
edbond says:
for script in your RAILS_ROOT folder
ENV['RAILS_ENV'] = 'development' require File.expand_path(File.dirname(__FILE__) + "/config/environment")


// insert code here..

Windows Start|Run magic

Stolen from: http://www.hanselman.com/blog/Reflector5ReleasedWorldDominationAssured.aspx

Tuesday, February 20, 2007 1:18:12 PM (Pacific Standard Time, UTC-08:00)
The PATH might make sense if you have a single folder with a bunch of utilities. Otherwise, you can safely leave your DOS tricks behind and just add an entry for reflector.exe to:
HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths

Set the (Default) string value to the full path to the executable. Start|Run glory, the Windows way.
Joshua Flanagan

Windows Path

Stolen From: http://www.hanselman.com/blog/Reflector5ReleasedWorldDominationAssured.aspx
Tuesday, February 20, 2007 4:54:50 PM (Pacific Standard Time, UTC-08:00)

You mention that reflector should be in your path. That gives me an opportunity to exploit a (ruby) gem I created a few weeks ago which makes managing your path a breeze. Instead of going to My Computer | Properties | Blah blah and adding the path entry using that terrible dialog, just follow these steps:
gem install patheditor
path_editor --add c:\path\to\reflectore

Adding items to the Windows Path is so painful, I created this utility to make it a little easier for everyone.

It uses a WSH object to update your USER path setting, and the change will be permanent - current and future command prompts will see the new path. Unfortunately, slickrun does not respect path updates so you'll have to kill it and restart if you launch prompts from there ...
Justin

Also:
Thursday, February 22, 2007 5:25:22 PM (Pacific Standard Time, UTC-08:00)
@Jon

Updating path that way (Set PATH=) only affects your current command prompt. My script uses WSH objects to propagate the change to all running processes (that respect the update), and future prompts will include the new path.

The script will also not add duplicate entries, and it can even clean duplicate/non-existent directories out of your current path.
Justin

Load OS X's environment.plist in your shell

To make environment variables available to Mac OS X GUI applications, those variables must be defined in your ~/.MacOSX/environment.plist. Sometimes you want to have those same environment variables available from your shell as well. If you're using Terminal, that's no problem--the variables from environment.plist are available. But what if you need to SSH into the machine?

Adhering to the DRY principle, this script can be launched from your shell's rc file to load up the environment variables from environment.plist. To load it into my tcsh environment, I use

if ($?SSH_CLIENT) then
    eval `~/bin/parseEnvironmentPlist.rb`
endif


And here's the ruby script:

#!/usr/bin/ruby

#
# A script for parsing ~/.MacOSX/environment.plist and loading the 
# environment variables it defines into a shell environment.
#

# determine which shell the user is running
# currently we support bash and tcsh
if /^\/[-A-Za-z\/]+\/(bash|tcsh)$/ =~ ENV['SHELL']
    shell = $1
else
    # if we can't determine the users shell, or if
    # it's an unsupported shell, bail out here
    exit 1
end

# a regex for matching <key>...</key> lines
# group 1 is the name of the key
key_re = /^\s*<key>([A-Za-z]+[_A-Za-z0-9]*)<\/key>\s*$/

# a regex for matching <string>...</string> value lines
# group 1 is the value of the environment variable
value_re = /^\s*<string>([-_:.\/0-9A-Za-z]+)<\/string>\s*$/

File.open("#{ENV['HOME']}/.MacOSX/environment.plist", "r") do |plist|

    currentKey = "" # the key we're currently processing
    
    # look at each line of the file to find keys
    # followed by values
    plist.each_line do |next_line| 
    
        # if we find a key, hold on to it 
        if key_re =~ next_line
            currentKey = $1
            currentValue = ""
        
        # since key lines alternate with value lines,
        # if we match a value line, we know it's a value
        # for the previously matched key
        elsif value_re =~ next_line
            currentValue = $1
        
            if shell == "bash"
                # output a setenv command to stdout that's 
                # suitable for running through bash's eval
                puts "#{currentKey}=#{currentValue}; export #{currentKey};"
            elsif shell == "tcsh"
                # output a setenv command to stdout that's 
                # suitable for running through tcsh's eval
                puts "setenv #{currentKey} #{currentValue};"
            else
                # we should never get to this point since we 
                # exit much earlier if the shell type can't be 
                # determined. But, just in case, exit here too.
                exit 1
            end
        
            currentKey = currentValue = ""
        end
    
    end

end


I wrote this script back when I was first learning Ruby, so A) that's why there are so many comments and 2) it could probably be improved, but it works for me!
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS