class Language

  include DataMapper::Resource

  # properties

  property :id,   Serial

  property :code, String, :required => true, :unique => true
  property :name, String, :required => true

  # locale string like 'en-US'
  validates_format_of :code, :with => /\A[a-z]{2}-[A-Z]{2}\z/


  def self.[](code)
    return nil if code.nil?
    first :code => code.to_s.gsub('_', '-')
  end

end

class Language

  include DataMapper::Resource

  # properties

  property :id,   Serial

  property :code, String, :required => true, :unique => true, :format => /\A[a-z]{2}-[A-Z]{2}\z/
  property :name, String, :required => true

  def self.[](code)
    return nil if code.nil?
    first :code => code.to_s.gsub('_', '-')
  end

end

Use :format option in property declaration rather than validates_format_of
Use :format option in property declaration rather than validates_format_of
