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-z]{2}-[A-Z]{2}$/


  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
  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

Use \A and \z in regexp for stricter matching
Use \A and \z in regexp for stricter matching

* While \A and ^ are the same, there is a difference between \z and $

  Using $ it will match the "end of line", which means that this would
  match strings like: "en-US\n". While \z means "end of string" and it
  will not match that, and will only match "en-US".

  I would assert that most of the time when using anchors you would want
  to use \A and \z, only falling back to ^ and $ in specific cases.
