About this user

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

Camel case a string ( removing accents )

# input str = " j'ai écris l'œuvre de ma vie "
# output str :"JAiEcrisLOeuvreDeMaVie"

accents = { ['á','à','â','ä','ã','Ã','Ä','Â','À'] => 'a',
  ['é','è','ê','ë','Ë','É','È','Ê'] => 'e',
  ['í','ì','î','ï','I','Î','Ì'] => 'i',
  ['ó','ò','ô','ö','õ','Õ','Ö','Ô','Ò'] => 'o',
  ['œ'] => 'oe',
  ['ß'] => 'ss',
  ['ú','ù','û','ü','U','Û','Ù'] => 'u'
  }
accents.each do |ac,rep|
  ac.each do |s|
    str.gsub!(s, rep)
  end
end
str.gsub!(/[^a-zA-Z_\- ]/," ")
str = " " + str.split.join(" ")
str.gsub!(/ (.)/) { $1.upcase }

Copy data from database into fixtures

Take from pylonhead.com

  def self.to_fixture
    write_file(File.expand_path("test/fixtures/#{table_name}.yml", RAILS_ROOT),
      self.find(:all).inject("---\n") { |s, record|
        self.columns.inject(s+"#{record.id}:\n") { |s, c|
          s+"  #{{c.name => record.attributes[c.name]}.to_yaml[5..-1]}\n" }
    })
  end

Bulk svn actions (usefull with rails)

Just run this code from inside your repository. This will do a bulk action like "svn add" on each file marked with the given filter (like '?') when doing "svn status"

Beware: this code might break with multi word filenames...

usage :
# ./svn_bulk ? add ---> this shows a preview of the actions
# ./svn_bulk ? add 1 ---> action !
#!/usr/bin/ruby
sign, action, doit = ARGV
sign = '\?' if sign == '?'
list = IO.popen("svn st")
list.each {|i|
if (i =~ /^#{sign}\s*(.*)/) 
cmd = "svn #{action} '"+$1+"'"
print cmd + "\n"
system(cmd) if doit
end
}

Very simple act_as base file

This is a very simple file to show how to add 'act_as_whatever' to ActiveRecord.

To use it, put the code into a file like lib/your_super_name/acts/idiot.rb and voilà.
Thanks to technoweenie for "acts_as_paranoid" which I used with "acts_as_tree" to build this template.

module YourSuperName
  module Acts
    module Idiot
      # this is called when the module is included into the 'base' module
      def self.included(base)
        # add all methods from the module "AddActsAsMethod" to the 'base' module
        base.extend AddActsAsMethod
      end
      module AddActsAsMethod
        def acts_as_idiot
          # BELONGS_TO HAS_MANY GOES HERE

          class_eval <<-END
            include YourSuperName::Acts::Idiot::InstanceMethods
          END
        end
      end
      
      
      module InstanceMethods
        def self.included(aClass)
          aClass.extend ClassMethods
        end

        # PUT YOUR INSTANCE METHODS HERE
        def idiot(msg)
          "#{self}: I am an idiotic object. And I say '#{msg}'."
        end
        
        module ClassMethods
          # PUT YOUR CLASS METHODS HERE
          def idiot(msg)
          "#{self}: I am an idiotic class. And I say '#{msg}'."
          end
        end
      end
    end
  end
end

ActiveRecord::Base.send :include, YourSuperName::Acts::Idiot
« Newer Snippets
Older Snippets »
4 total  XML / RSS feed