About this user

« Newer Snippets
Older Snippets »
6 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

Create an archive with tar

// description of your code here

tar czf project.tar.gz project/

Add all new files to subversion (usefull with rails)

Just run this code from inside your repository. This will do an "svn add" on each file marked as "?" when doing "svn status"

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

#!/usr/bin/ruby
list = IO.popen("svn st")
list.each {|i|
if (i =~ /^\?\s*(.*)/) 
    cmd = "svn add '"+$1+"'"
    print cmd + "\n"
    system(cmd)
end
}

Svn obliterate (dump filter)

Taken and slightly adapted from www.robgonda.com/blog

svnadmin dump /path/to/repos > proj.dump
cat proj.dump | svndumpfilter exclude somefolder > cleanproj.dump
STOP SVN services
BACKUP /path/to/repos/conf /path/to/repos/hooks (all custom configuration for this repository)
DELETE /path/to/repos
svnadmin create /path/to/repos
RESTORE /path/to/repos/conf /path/to/repos/hooks
svnadmin load /path/to/repos < cleanproj.dump
RESTART SVN services

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 »
6 total  XML / RSS feed