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-9 of 9 total  RSS 

Generate a list of Rails controllers and methods

This code goes over the App/controllers directory and extracts a list
of all controllers and their methods.

use this script from rails console.

I have used this script to generate a migration file with default authorization roles & rights.

controllers = Dir.new("#{RAILS_ROOT}/app/controllers").entries
controllers.each do |controller|
 if controller =~ /_controller/ 
  cont = controller.camelize.gsub(".rb","")
  puts cont
  (eval("#{cont}.new.methods") - 
    ApplicationController.methods - 
    Object.methods -  
    ApplicationController.new.methods).sort.each {|met| 
       puts "\t#{met}"
    }
end

end



to use it to create a Right object I used the following adjustments:

controllers.each do |controller|
    if controller =~ /_controller/ 
    name = controller.camelize.gsub(".rb","")
     (eval("#{name}.new.methods") - 
       Object.methods -  
       ApplicationController.new.methods).sort.each {|met| 
          name_short = name.gsub("Controller","").downcase
          #it is possible to call the create directly from this script
          #I wanted to review and revise the names. 
          puts "#{met}_#{name_short}=Right.create(:name=>\"#{met} #{name_short}\",
                                                  :controller=>\"#{name_short}\",
                                                  :action=>\"#{met}\")"}
  end
  end

Add rails log to console

In order to show rails log in console, add theses lines to your .irbrc

if ENV.include?('RAILS_ENV')&& !Object.const_defined?('RAILS_DEFAULT_LOGGER')
  Object.const_set('RAILS_DEFAULT_LOGGER', Logger.new(SDTOUT))
end

Create new repository with svnadmin

svnadmin create REPOS_PATH

Python - getchar()

import os,sys
import sys
import termios

def getchar():
	'''
	Equivale al comando getchar() di C
	'''

	fd = sys.stdin.fileno()
	
	if os.isatty(fd):
		
		old = termios.tcgetattr(fd)
		new = termios.tcgetattr(fd)
		new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
		new[6] [termios.VMIN] = 1
		new[6] [termios.VTIME] = 0
		
		try:
			termios.tcsetattr(fd, termios.TCSANOW, new)
			termios.tcsendbreak(fd,0)
			ch = os.read(fd,7)

		finally:
			termios.tcsetattr(fd, termios.TCSAFLUSH, old)
	else:
		ch = os.read(fd,7)
	
	return(ch)

Linux - Escape Terminal

// Scrive sottolineato
echo -e "\033[4m\017Prova"


// Scrive in Blink
echo -e "\033[5m\017Prova"

Console keyboard scheme switcher

Switches between us and swedish (or any other)
keyboard-scheme in the console. Default is US.

#!/bin/sh
# Keyboard switcher script
# Written in 2006 by Davor Babic <davorb@gmail.com>
# This software is realeased into public domain

case $1 in
sv)
    echo "Switching to swedish keyboard layout."
    loadkeys /usr/share/keymaps/i386/qwerty/se-latin1.kmap.gz
    ;;
us)
    echo "Switching to US keyboard layout."
    loadkeys /usr/share/keymaps/i386/qwerty/us.kmap.gz
    ;;
default)
    echo "None selected. Loading US..."
    loadkeys /usr/share/keymaps/i386/qwerty/us.kmap.gz
    ;;
esac

Console scheme snippet

c: open/no-wait [scheme: 'console]
forever [
    wait [c]
    print copy c
]

c: open/no-wait [scheme: 'console]
forever [
    wait [c]
    print mold ch: copy c
    insert c #"^M"
]

c: open/binary/no-wait [scheme: 'console]
forever [
    if not none? wait/all [c] [
        print to-char copy to-integer c
    ]
]

readable object output in Rails breakpointer

breakpointer/console shows straight variable output as one long munge. Console can simply run .to_yaml to organize, breakpointer requires:

>> client.puts @object.to_yaml

script/console reload models

$ ./script.console
Loading development environment...
>> [do stuff / modify models]
>> Dispatcher.reset_application!


Picked this up from...

http://habtm.com/articles/2005/10/04/script-console-reload-models

... which would have been fine but the site is completely unusable, opting for cool drag-and-drop over the ability to copy and paste. Bad.
« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS