nunojob:~ dscape/08$ echo The Black Sheep

Posts tagged ‘diy’

famfamfam flag iconset yaml representation on ruby

First download the flags from famfamfam into a folder called flags (pngs only,
no subfolders!).

Now in the bash:

cd flags
wget http://www.iso.org/iso/iso3166_en_code_lists.txt
ls > file.txt
irb

Now your on the interactive ruby shell (irb).

# If you want to use this as a script just copy 
# the bash lines and put them as
# system 'cd flags'
# system 'wget ...' and so on

# We start treating the output from the ls
# open the ls output
f = File.new 'file.txt'                            
# place the file in lines
lines = f.readlines                                
# map those who have 2 digits codes
lines = lines.select { |line| line.size == 7 }    
# get the 2 digits 
lines = lines.map { |line| line[0..1] }            
f.close

# Then the iso file
# open the iso file
f = File.new 'iso3166_en_code_lists.txt' 
# get rid of the notes          
f.readline                                         
# place the file in iso
iso = f.readlines                                  
# create a new hash
hashed_iso = {}                                    
# select non empty lines
iso.select { |a| !a.rstrip.empty? }.map do |b|
  # remove the whitespaces and split in ';'     
  aux = b.rstrip.split ';'     
  # place info in the hash                    
  hashed_iso[aux[1].downcase] = aux[0].capitalize  
end
f.close

# Now we cross information giving more 
# importance to what's in the iso.
iso_famfamfam = hashed_iso.select {
  # select those who have flags in famfamfam
  |k,v| lines.member? k                            
}.sort_by {
  # sort them by the name the user will see
  |pair| pair[1]                                   
}

# Now we create the contents to store in the yaml file
# create the yaml first line
yaml_lines = "hash: \n"                           
iso_famfamfam.each do |pair|
  # for each pair, create the yaml
  # representation and put in yaml_lines
  yaml_lines < < '  ' + pair[0] + ': ' 
                         + pair[1] + "\n"    
end 

yaml_lines &lt;&lt; "array: ---\n"

iso_famfamfam.each do |pair|
  yaml_lines << '- - ' + pair[0] + "\n  - " 
                            + pair[1] + "\n" 
end 

# put it in a file
f = File.new 'flags.yml', 'w'
f.write yaml_lines
f.close

#sample for loading the yaml into ruby
f = File.new 'flags.yml'
fy = YAML.load f

Now you have your yaml representation of the flags. Do what you please. Personally I’m going to use the file to load it to Ruby when my Ruby on Rails app starts and use it as part of the registration system in the

Integração entre RSpec & TextMate

Como já disse anteriormente ando a trabalhar no CouchDB-Ruby driver. Como a coisa tem o seu nível de exigência decidi usar rspec, algo que já andava para fazer a algum tempo, de forma a melhorar a testagem do software que produzo e reduzir o tempo de implementação. Já agora aproveito para sugerir o ZenTest a quem quiser que o rspec corra em background.

Continuando: rspec é uma forma descritiva – tipo cenários em Use Case – para descrever os nossos testes. Em Ruby claro, dai o R :P

O resultado final fica assim. É catita e prático, já que evita mais uma ida a consola para escrever ‘spec filename.rb’! E é bastante parecida com a linguagem natural. Por exemplo – um dos meus primeiros testes é:

it "should connect to server and return a Server object" do
  CouchDB.connect(HOST, PORT).should 
    be_kind_of(CouchDB::Server)
end

Não fiz commit de nada porque a implementaçao anterior do CouchDB-Ruby com JSON não está completa. Portanto não vale a pena ir espreitar que não está nada lá! Em breve, espero eu, podem ver os testes completos online.

De qualquer forma se estiverem interessados em experimentar o rspec basta:

  • Instalar o rspec
    sudo gem install rspec
  • Descobrir o path onde está o ruby e o rspec. Estes comandos são capazes de ajudar:
    which ruby
    gem environment
  • Com os meus paths basta correr:
    export TM_RUBY=/usr/bin/ruby
    export TM_RSPEC_HOME=/Library/Ruby/Gems/1.8/gems/rspec-1.1.3
    cd ~/Library/Application\ Support/TextMate/Bundles/
    svn co svn://rubyforge.org/var/svn/rspec/trunk/RSpec.tmbundle
    
  • Como o (meu) terminal não gosta de exports. Caso não funcione façam:
    echo TM_RUBY=/usr/bin/ruby >> ~/.profile
    echo TM_RSPEC_HOME=/Library/Ruby/Gems/1.8/gems/rspec-1.1.3 &gt;&gt; ~/.profile
    
  • Actualizar os Bundles do TextMate
    Bundles > Bundle Editor > Reload Bundles

E pronto! Caso surjam dúvidas podem sempre consultar a documentação oficial do bundle rspec para TextMate. Que comece a diversão! Bem vá diversão é exagerar mas com rspec é pelo menos uma experiência mais agradável que o habitual quando se fala de testes.

Follow

Get every new post delivered to your Inbox.