notes from the ruby-ground
ruby, rails, etc
Wednesday, November 2, 2011
ARel and ActiveRecord
Wednesday, January 26, 2011
Very colorful vim
It seems that xterms and screen are slowly clawing their way into the 21st century... We can now view our code in more than 16 colors. There are a few hoops you'll need to jump through to get it working.
- Make sure tmux is 256 ready, editing your ~/.tmux.conf
set -g default-terminal "screen-256color"
- Make sure screen is 256 ready, editing your ~/.screenrc
# terminfo and termcap for nice 256 color terminal
# allow bold colors - necessary for some reason
attrcolor b ".I"
# tell screen how to set colors. AB = background, AF=foreground
termcapinfo xterm 'Co#256:AB=\E[48;5;%dm:AF=\E[38;5;%dm'
# erase background with current bg color
defbce "on"
- Check your terminal supports 256 colors
- colortest
- Run that in and out of screen
- If you find that the gradient doesn't print what you expect, you may need to recompile screen with the configure option
--enable-colors256
- If that doesn't work, you may need to recompile X...
- Update your .vimrc
-
syntax on
set t_Co=256
-
You can now begin using some super fancy color schemes with vim. While in vim, use :colorscheme
to test drive the many and varied options. Fortunately, colorscheme
supports tab completion and it will search your system (
Very nice (am I crazy, or are a bunch of these fifteen year old emacs color schemes)
Not too shabby
Tuesday, January 18, 2011
Migrating to MySQL
https://github.com/diaspora/diaspora/wiki/Migrating-from-MongoDB-to-MySQL
- Update your source
- git fetch upstream
- git checkout master
- git pull upstream master
- Install mysql packages
- apt-get install mysql-server mysql-client libmysqlclient16-dev
- Make sure you're utf8 all the way, Diaspora is
- sudo vi /etc/mysql/my.cnf
- Update the server section [mysqld]
- collation_server=utf8_bin
character_set_server=utf8
- collation_server=utf8_bin
- Update the client section [mysql]
- default-character-set = utf8
- Update the server section [mysqld]
- sudo vi /etc/mysql/my.cnf
- Restart MySQL
- service mysql restart
- Create your database.yml
- cp config/database.yml.example config/database.yml
- Edit your new yaml file to suit your environment
- Note As of this writing, only the MySQL user root can migrate from mongo to MySQL
- Create the necessary databases and tables
- rake db:drop:all
- Make sure test, development, and production all have valid credentials (for root mysql user)
- You can safely ignore messages like:
Couldn't drop diaspora_production : <Mysql2::Error: Unknown database 'diaspora_production'>
- rake db:create
- rake db:migrate
- rake migrations:migrate_to_mysql
- rake db:drop:all
Voila, you should now have everything you need to help the community continue to test this massive change...
Wednesday, January 12, 2011
A rake task to syntax check erb, haml, rb, sass, and yaml files
Diaspora is pretty fluid right now. This means we are have some green tests, some missing tests, and other tests that check intent (not implementation). In an ideal world, I suppose test cases would cover all of our bases...
Until then, I've added a new task to my fork, check_syntax:all. This breaks down further to the subtasks check_syntax:erb
, check_syntax:haml
, check_syntax:haml_ruby
, check_syntax:sass
, and check_syntax:yaml
. Most of these tasks are pretty straight forward, search the tree for .suffix files and check their syntax. The subtask check_syntax:haml_ruby
uses HAML::Engine.new().precompiled
(similar output to haml --debug
) to generate the ruby haml generates. The resulting ruby is syntax checked... This is very important, because haml --check
does not check ruby syntax.
The following is based on Hungry Machine's post
code is also available from here
namespace :check_syntax do
require 'erb'
require 'open3'
require 'yaml'
desc "Check syntax of various file types"
task :all => [:ruby, :erb, :yaml, :haml, :haml_ruby, :sass]
#desc 'Check syntax of .erb and .rhtml files'
task :erb do
(Dir["**/*.erb"] + Dir["**/*.rhtml"]).each do |file|
next if skip_path?(file)
Open3.popen3('ruby -Ku -c') do |stdin, stdout, stderr|
stdin.puts(ERB.new(File.read(file), nil, '-').src)
stdin.close
if error = ((stderr.readline rescue false))
puts red_string(file) + error[1..-1].sub(/^[^:]*:\d+: /, '')
end
stdout.close rescue false
stderr.close rescue false
end
end
end
#desc 'Check syntax of .haml files'
task :haml do
haml = bin_path "haml"
Dir['**/*.haml'].each do |file|
next if skip_path?(file)
next if file.match("vendor/plugins/.*/generators/.*/templates")
Open3.popen3("#{haml} -c #{file}") do |stdin, stdout, stderr|
if error = ((stderr.readline rescue false))
puts red_string(file) + error
end
stdin.close rescue false
stdout.close rescue false
stderr.close rescue false
end
end
end
#desc 'Check ruby syntax of .haml files'
task :haml_ruby do
Dir['**/*.haml'].each do |file|
next if skip_path?(file)
next if file.match("vendor/plugins/.*/generators/.*/templates")
Open3.popen3("ruby -Ku -c") do |stdin, stdout, stderr|
stdin.puts(Haml::Engine.new(File.read(file)).precompiled)
stdin.close
if error = ((stderr.readline rescue false))
puts red_string(file) + error.sub(/^[^:]*:\d+: /, '')
end
stdout.close rescue false
stderr.close rescue false
end
end
end
#desc 'Check syntax of .rb files'
task :ruby do
Dir['**/*.rb'].each do |file|
next if skip_path?(file)
next if file.match("vendor/plugins/.*/generators/.*/templates")
Open3.popen3("ruby -Ku -c #{file}") do |stdin, stdout, stderr|
if error = ((stderr.readline rescue false))
puts error
end
stdin.close rescue false
stdout.close rescue false
stderr.close rescue false
end
end
end
#desc 'Check syntax of .sass files'
task :sass do
sass = bin_path "haml", "sass"
Dir['**/*.sass'].each do |file|
next if skip_path?(file)
next if file.match("vendor/plugins/.*/generators/.*/templates")
Open3.popen3("#{sass} -c #{file}") do |stdin, stdout, stderr|
if error = ((stderr.readline rescue false))
puts error
end
stdin.close rescue false
stdout.close rescue false
stderr.close rescue false
end
end
end
#desc 'Check syntax of .yml files'
task :yaml do
Dir['**/*.yml'].each do |file|
next if skip_path?(file)
begin
YAML.load_file(file)
rescue => e
puts red_string(file) + "#{(e.message.match(/on line (\d+)/)[1] + ':') rescue nil} #{e.message}"
end
end
end
def bin_path gem_name, bin_name=nil
bin_name=gem_name if bin_name.nil?
[`bundle show #{gem_name}`.chomp!, 'bin', bin_name].join("/")
end
def skip_path?(file)
["tmp", "vendor/gems", "vendor/rails"].each do |dir|
return true if file.match(dir)
end
return false
end
def red_string(str)
"\e[31m" + str + "\e[0m: "
end
end