PHP PHP年份选项

function getYearOptions( $selected, $offset, $count )
{
    $options = '';

    $start_year = date( 'Y' ) + $offset;

    $finish_year = $start_year + $count;

    for( $x = $start_year; $x <= $finish_year; $x++ )
    {
        if( strlen( $x ) == 1 ) $x = '0' . $x;

        $options .= '<option value="' . $x . '"';

        if( $x == $selected )
        {
            $options .= ' selected="selected"';
        }

        $options .= '>' . $x . '</option>';
    }

    return $options;
}

Bash FreeBSD ipfw内核配置

machine         i386
cpu             I686_CPU
ident           MYKERNEL

options         SCHED_4BSD              # 4BSD scheduler
options         PREEMPTION              # Enable kernel thread preemption
options         INET                    # InterNETworking
options         FFS                     # Berkeley Fast Filesystem
options         SOFTUPDATES             # Enable FFS soft updates support
options         PROCFS                  # Process filesystem (requires PSEUDOFS)
options         PSEUDOFS                # Pseudo-filesystem framework
options         COMPAT_43               # Compatible with BSD 4.3 [KEEP THIS!]
options         COMPAT_FREEBSD4         # Compatible with FreeBSD4
options         COMPAT_FREEBSD5         # Compatible with FreeBSD5
options         KTRACE                  # ktrace(1) support
options         SYSVSHM                 # SYSV-style shared memory
options         SYSVMSG                 # SYSV-style message queues
options         SYSVSEM                 # SYSV-style semaphores
options         _KPOSIX_PRIORITY_SCHEDULING # POSIX P1003_1B real-time extensions
options         KBD_INSTALL_CDEV        # install a CDEV entry in /dev
options         AHC_REG_PRETTY_PRINT    # Print register bitfields in debug
                                        # output.  Adds ~128k to driver.
options         AHD_REG_PRETTY_PRINT    # Print register bitfields in debug
                                        # output.  Adds ~215k to driver.
options         ADAPTIVE_GIANT          # Giant mutex is adaptive.

device          apic                    # I/O APIC
device          eisa
device          pci
device          fdc
device          ata
device          atadisk         # ATA disk drives

options         ATA_STATIC_ID   # Static device numbering

device          atkbdc          # AT keyboard controller
device          atkbd           # AT keyboard
device          psm             # PS/2 mouse
device          vga             # VGA video card driver
device          splash          # Splash screen and screen saver support
device          sc
device          sio             # 8250, 16[45]50 based serial ports
device          miibus          # MII bus support
device          dc              # DEC/Intel 21143 and various workalikes
device          tl              # Texas Instruments ThunderLAN
device          loop            # Network loopback
device          random          # Entropy device
device          ether           # Ethernet support
device          pty             # Pseudo-ttys (telnet etc)
device          bpf             # Berkeley packet filter
device          tun             # Packet tunnel.

options         IPFIREWALL
options         IPFIREWALL_VERBOSE
options         IPFIREWALL_VERBOSE_LIMIT=5
options         IPFIREWALL_DEFAULT_TO_ACCEPT
options         IPDIVERT
options         DUMMYNET
options         HZ=1000

Bash Bash FreeBSD端口升级

#!/bin/sh

cd /usr/ports
make fetchindex
/usr/local/sbin/portupgrade -anrR

Bash Bash FreeBSD更新稳定

#!/bin/sh

if SERVER=`/usr/local/bin/fastest_cvsup -q -c all`; then
  /usr/local/bin/cvsup -g -P - -L 2 -h $SERVER /root/stable-supfile
fi

Ruby Rails活动记录单个错误补丁

# config/environment.rb
require "#{RAILS_ROOT}/app/overrides/all"

# app/overrides/all.rb
Dir[ File.dirname( __FILE__ ) + "/**/*.rb" ].each { |file| require( file ) }

# app/overrides/active_record.rb
module ActiveRecord
  class Errors
    # ONLY RETURN THE 1ST ERROR FOR EACH ATTRIBUTE
    def first_messages
      list = []
      first_messages = []
      @errors.each_key do |attr|
        @errors[ attr ].each do |msg|
          next if msg.nil?
          if attr == 'base'
            unless list.include? attr
              first_messages << msg
              list << attr
            end
          else
            unless list.include? attr
              first_messages << @base.class.human_attribute_name( attr ) + ' ' + msg
              list << attr
            end
          end
        end
      end
      return first_messages
    end
  end
end

Ruby Rails CSS上传器/验证器

#!/usr/bin/env ruby

curl = `which curl 2>/dev/null`.chomp
validator = 'http://jigsaw.w3.org/css-validator/validator'

# All warnings, CSS2, all mediums
options = 'warning=2&profile=css2&usermedium=all'

base = File.expand_path("#{File.dirname(__FILE__)}/../../public/stylesheets")

# Got curl?
raise "Curl not found" if curl.empty?

# Get path to stylesheets
if ARGV.size > 0
  base = ARGV.shift
end

# All css files or just one?
glob = base =~ /css$/ ? base : "#{base}/*.css"

# Do files
Dir.glob(glob) do |file|

  next unless File.exists?( file )

  errors, warnings = [ ], [ ]

  # Send the css file to the validator
  results = `#{curl} -s -F "file=@#{file}" -F "#{options}" #{validator}`

  # Validator couldn't find the file
  #
  # OR the file didn't have _any_ valid css content before
  # the errors <- little gotcha
  #
  results.grep(/No style sheet found/) do |line|
    STDERR << "#{$&}\n"
    exit
  end

  # Add new lines to <li></li> tags so grep can find them easier
  results.gsub!(/\n/,'').gsub!(/<li>/,"\n<li>").gsub!(/<\/li>/,"</li>\n")

  results.grep(/<li>.*<\/li>/) do |line|

    # collect errors
    line.grep(/<span class='error'>/) do |error|
      errors << error.gsub!(/(<p>|<\/p>)/,"\n").gsub!(/<(.|\n)*?>/, '')
    end

  end

  # collect warnings
  results.grep(/<span class='warning'>/) do |line|
    warnings << line.gsub!(/<(.|\n)*?>/, '')
  end

  # Dump information to STDERR
  STDERR << "CSS File #{file}:\n\n"

  { 'Errors' => errors, 'Warnings' => warnings }.each do |k,v|

    if v.empty?
      STDERR << "No #{k.downcase} found\n---------------\n\n"
    else
      STDERR << "#{k} found:\n-------------\n\n"
      v.each {|line| STDERR << line}
    end

  end

end

Ruby Rails alt行颜色

# Used to alternate row colors in a table
def alt( s = '', s2 = '2' )
  @alt_state = true if @alt_state.nil?
  @alt_state = !@alt_state
  @alt_state ? s2 : s
end

Ruby Rails第n个工作日

require "date"
require "date2"

class DateCalc

  ######################################################
  # Returns the date for a specified day on a numbered
  # week as a date object. Such as finding the 3rd Wed 
  # in March, 2006.
  # 
  #   example: nth_weekday(2005,1,2,0)
  #   (2nd Sunday in January 2005) -> Jan 8th, 2005
  #
  # Returns: Date object  
  ######################################################
  def nth_weekday(year,month,week,day)
    test_date = Date.new(year,month,1)
    last_day = Date.new(test_date.year, test_date.month, -1).day
    first_weekday = test_date.wday
    offset = first_weekday - (day % 7)
    weeks = (last_day) / 7
    test_date + (week * 7) - offset
  end
  
end

Ruby Rails随机代词密码

# creates random pronouncable passwords
def self.random_password
  c = %w( b c d f g h j k l m n p qu r s t v w x z ) +
      %w( ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr )
  v = %w( a e i o u y )
  f, r = true, ''
  6.times do
    r << ( f ? c[ rand * c.size ] : v[ rand * v.size ] )
    f = !f
  end
  2.times do
    r << ( rand( 9 ) + 1 ).to_s
  end
  r
end

Ruby Rails分手排长队

def break_up_long_line(str, max)
  counter = 0
  new_text = ''
  for i in 0...str.length
    if str[i,1] =~ /\n/
      counter = 0
    else
      counter = counter + 1
    end
    new_text << str[i,1]
    if counter >= max && str[i,1] =~ /\s/
      new_text << "\n"
      counter = 0
    end
  end
  new_text
end