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

About this user

Chu Yeow http://blog.codefront.net/

« Newer Snippets
Older Snippets »
Showing 1-10 of 12 total  RSS 

Removing empty directories (non-recursive)

// Simple class to remove empty directories.

   1  
   2  require 'fileutils'
   3  
   4  class ReorgBot
   5  
   6    attr_accessor :path
   7    attr_accessor :dir
   8  
   9    def initialize(path)
  10      raise ArgumentError, "path '#{path}' does not exist!" unless File.exist?(path)
  11      raise ArgumentError, "path '#{path} is not a directory you dolt!" unless File.directory?(path)
  12      @path = path
  13      @dir = Dir.new(path)
  14    end
  15  
  16    def dirs
  17      @dir.select { |f| File.directory?(File.join(@path, f)) }
  18    end
  19  
  20    def empty_dirs
  21      dirs.select { |d| Dir[File.join(@path, d, '*')].empty? }
  22    end
  23  
  24    def non_empty_dirs
  25      dirs - empty_dirs
  26    end
  27  
  28    def remove_empty_dirs
  29      empty_dirs.each do |d|
  30        FileUtils.rm_rf(File.join(@path, d))
  31      end
  32    end
  33  end
  34  
  35  bot = ReorgBot.new('/tmp/reorg_test')
  36  bot.remove_empty_dirs

Java inflections

   1  
   2  import java.util.ArrayList;
   3  import java.util.List;
   4  import java.util.regex.Matcher;
   5  import java.util.regex.Pattern;
   6  
   7  /**
   8   * Transforms words (from singular to plural, from camelCase to under_score, etc.). I got bored of doing Real Work...
   9   * 
  10   * @author chuyeow
  11   */
  12  public class Inflector {
  13  
  14      // Pfft, can't think of a better name, but this is needed to avoid the price of initializing the pattern on each call.
  15      private static final Pattern UNDERSCORE_PATTERN_1 = Pattern.compile("([A-Z]+)([A-Z][a-z])");
  16      private static final Pattern UNDERSCORE_PATTERN_2 = Pattern.compile("([a-z\\d])([A-Z])");
  17  
  18      private static List<RuleAndReplacement> plurals = new ArrayList<RuleAndReplacement>();
  19      private static List<RuleAndReplacement> singulars = new ArrayList<RuleAndReplacement>();
  20      private static List<String> uncountables = new ArrayList<String>();
  21  
  22      private static Inflector instance; // (Pseudo-)Singleton instance.
  23  
  24      private Inflector() {
  25          // Woo, you can't touch me.
  26          
  27          initialize();
  28      }
  29      
  30      private void initialize() {
  31          plural("$", "s");
  32          plural("s$", "s");
  33          plural("(ax|test)is$", "$1es");
  34          plural("(octop|vir)us$", "$1i");
  35          plural("(alias|status)$", "$1es");
  36          plural("(bu)s$", "$1es");
  37          plural("(buffal|tomat)o$", "$1oes");
  38          plural("([ti])um$", "$1a");
  39          plural("sis$", "ses");
  40          plural("(?:([^f])fe|([lr])f)$", "$1$2ves");
  41          plural("(hive)$", "$1s");
  42          plural("([^aeiouy]|qu)y$", "$1ies");
  43          plural("([^aeiouy]|qu)ies$", "$1y");
  44          plural("(x|ch|ss|sh)$", "$1es");
  45          plural("(matr|vert|ind)ix|ex$", "$1ices");
  46          plural("([m|l])ouse$", "$1ice");
  47          plural("(ox)$", "$1en");
  48          plural("(quiz)$", "$1zes");
  49  
  50          singular("s$", "");
  51          singular("(n)ews$", "$1ews");
  52          singular("([ti])a$", "$1um");
  53          singular("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$", "$1$2sis");
  54          singular("(^analy)ses$", "$1sis");
  55          singular("([^f])ves$", "$1fe");
  56          singular("(hive)s$", "$1");
  57          singular("(tive)s$", "$1");
  58          singular("([lr])ves$", "$1f");
  59          singular("([^aeiouy]|qu)ies$", "$1y");
  60          singular("(s)eries$", "$1eries");
  61          singular("(m)ovies$", "$1ovie");
  62          singular("(x|ch|ss|sh)es$", "$1");
  63          singular("([m|l])ice$", "$1ouse");
  64          singular("(bus)es$", "$1");
  65          singular("(o)es$", "$1");
  66          singular("(shoe)s$", "$1");
  67          singular("(cris|ax|test)es$", "$1is");
  68          singular("([octop|vir])i$", "$1us");
  69          singular("(alias|status)es$", "$1");
  70          singular("^(ox)en", "$1");
  71          singular("(vert|ind)ices$", "$1ex");
  72          singular("(matr)ices$", "$1ix");
  73          singular("(quiz)zes$", "$1");
  74  
  75          irregular("person", "people");
  76          irregular("man", "men");
  77          irregular("child", "children");
  78          irregular("sex", "sexes");
  79          irregular("move", "moves");
  80  
  81          uncountable(new String[] {"equipment", "information", "rice", "money", "species", "series", "fish", "sheep"});
  82      }
  83  
  84      public static Inflector getInstance() {
  85          if (instance == null) {
  86              instance = new Inflector();
  87          }
  88          return instance;
  89      }
  90  
  91      public String underscore(String camelCasedWord) {
  92  
  93          // Regexes in Java are fucking stupid...
  94          String underscoredWord = UNDERSCORE_PATTERN_1.matcher(camelCasedWord).replaceAll("$1_$2");
  95          underscoredWord = UNDERSCORE_PATTERN_2.matcher(underscoredWord).replaceAll("$1_$2");
  96          underscoredWord = underscoredWord.replace('-', '_').toLowerCase();
  97  
  98          return underscoredWord;
  99      }
 100  
 101      public String pluralize(String word) {
 102          if (uncountables.contains(word.toLowerCase())) {
 103              return word;
 104          }
 105          return replaceWithFirstRule(word, plurals);
 106      }
 107  
 108      public String singularize(String word) {
 109          if (uncountables.contains(word.toLowerCase())) {
 110              return word;
 111          }
 112          return replaceWithFirstRule(word, singulars);
 113      }
 114  
 115      private String replaceWithFirstRule(String word, List<RuleAndReplacement> ruleAndReplacements) {
 116  
 117          for (RuleAndReplacement rar : ruleAndReplacements) {
 118              String rule = rar.getRule();
 119              String replacement = rar.getReplacement();
 120  
 121              // Return if we find a match.
 122              Matcher matcher = Pattern.compile(rule, Pattern.CASE_INSENSITIVE).matcher(word);
 123              if (matcher.find()) {
 124                  return matcher.replaceAll(replacement);
 125              }
 126          }
 127          return word;
 128      }
 129  
 130      public String tableize(String className) {
 131          return pluralize(underscore(className));
 132      }
 133      
 134      public String tableize(Class klass) {
 135          // Strip away package name - we only want the 'base' class name.
 136          String className = klass.getName().replace(klass.getPackage().getName()+".", "");
 137          return tableize(className);
 138      }
 139  
 140      public static void plural(String rule, String replacement) {
 141          plurals.add(0, new RuleAndReplacement(rule, replacement));
 142      }
 143  
 144      public static void singular(String rule, String replacement) {
 145          singulars.add(0, new RuleAndReplacement(rule, replacement));
 146      }
 147  
 148      public static void irregular(String singular, String plural) {
 149          plural(singular, plural);
 150          singular(plural, singular);
 151      }
 152  
 153      public static void uncountable(String... words) {
 154          for (String word : words) {
 155              uncountables.add(word);
 156          }
 157      }
 158  }
 159  
 160  
 161  // Ugh, no open structs in Java (not-natively at least).
 162  class RuleAndReplacement {
 163      private String rule;
 164      private String replacement;
 165      public RuleAndReplacement(String rule, String replacement) {
 166          this.rule = rule;
 167          this.replacement = replacement;
 168      }
 169      public String getReplacement() {
 170          return replacement;
 171      }
 172      public void setReplacement(String replacement) {
 173          this.replacement = replacement;
 174      }
 175      public String getRule() {
 176          return rule;
 177      }
 178      public void setRule(String rule) {
 179          this.rule = rule;
 180      }
 181  }

Helper for displaying flash

   1  
   2  // Add this to your application_helper.rb.
   3  
   4    def display_standard_flashes
   5      known_levels = [:error, :warning, :notice] # highest priority to lowest
   6      level = known_levels.find { |level| flash.has_key?(level) }
   7      level ? content_tag('div', flash[level], :class => "flash #{level}") : nil
   8    end

WEBrick servlet with HTTP authentication

// HTTP authentication for a directory listing


   1  
   2  require 'webrick'
   3  include WEBrick
   4  
   5  dir = Dir::pwd
   6  port = 1234
   7  
   8  authenticate = Proc.new do |req, res|
   9    HTTPAuth.basic_auth(req, res, '') do |user, password|
  10      user == 'foo' && password == 'bar'
  11    end
  12  end
  13  
  14  s = HTTPServer.new(:Port => port, :ServerType => Daemon)
  15  s.mount('/', HTTPServlet::FileHandler, dir,
  16    :FancyIndexing => true,
  17    :HandlerCallback => authenticate # Hook up the authentication proc.
  18  )
  19  
  20  trap('INT') { s.shutdown }
  21  s.start

WEBrick servlet skeleton

// Skeleton code for a WEBrick servlet.

   1  
   2  require 'webrick'
   3  include WEBrick
   4  class UberServlet < HTTPServlet::AbstractServlet
   5  
   6    def do_GET(req, res)
   7      id = req.query['id'] # Get GET/POST params like that.
   8      res['content-type'] = 'text/html'
   9      res.status = 200
  10      res.body = some_text
  11    end
  12    alias :do_POST :do_GET
  13  
  14  end
  15  
  16  # "Mount" the servlet.
  17  server = HTTPServer.new(:Port => 1234)
  18  server.mount('/some/path', UberServlet)
  19  
  20  # Handle signals.
  21  %w(INT TERM).each do |signal|
  22    trap(signal) { server.shutdown }
  23  end
  24  
  25  server.start

Struct "magic" - creating objects from CSV file


   1  
   2  require 'csv'
   3  
   4  csv = CSV.open('some_file.csv', 'r')
   5  Post = Struct.new(*(csv.shift.map { |f| f.to_sym })) # Nice! Read in CSV header, turns them into symbols, and creates a new Struct.
   6  posts = csv.inject([]) do |posts, row|
   7    posts << Post[*row]
   8  end
   9  csv.close

Ruby Struct example


   1  
   2  Post = Struct.new(:id, :title, :content, :created_at)
   3  
   4  # Some database access, maybe via DBI.
   5  while row = @stmt.fetch do
   6    posts << Post.new(*row)
   7  end

Add a to_conditions method to ActiveRecord::Base for converting models to finder :conditions hash.

// Mixes in a to_conditions method to ActiveRecord::Base. Converts the attributes of an AR object to a
// ActiveRecord::Base#find :conditions hash. Useful for comparing AR objects, especially when looking for
// duplicates.
// E.g.
//
// if not Post.find(:all, :conditions => my_post.conditions).empty?
// puts "Duplicate found"
// end

   1  
   2  module Bezurk #:nodoc:
   3    module ActiveRecord #:nodoc:
   4      module Extensions
   5        def to_conditions
   6          attributes.inject({}) do |hash, (name, value)|
   7            hash.merge(name.intern => value)
   8          end
   9        end
  10        alias :to_conditions_hash :to_conditions
  11      end
  12    end
  13  end
  14  
  15  ActiveRecord::Base.send(:include, Bezurk::ActiveRecord::Extensions)

Updating Rubygems and gems

// Update Rubygems to the latest version, and getting the latest gems, and then cleaning up.

   1  
   2  sudo gem update --system  # Update to latest Rubygems (if any)
   3  gem outdated  # Get list of outdated gems and update as necessary
   4  sudo gem install XXX --include-dependencies
   5  sudo gem clean  # Clean up outdated gems

Making applications installed from source uninstallable



   1  
   2  # Install checkinstall and auto-apt if they aren't already installed.
   3  sudo apt-get install checkinstall auto-apt
   4  
   5  
   6  auto-apt run ./configure
   7  make
   8  sudo checkinstall # instead of "make install"
« Newer Snippets
Older Snippets »
Showing 1-10 of 12 total  RSS