Module: Ixtlan::Models::Locale

Defined in:
lib/ixtlan/models/locale.rb

Constant Summary collapse

DEFAULT =
"DEFAULT"
ALL =
"ALL"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(model) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ixtlan/models/locale.rb', line 11

def self.included(model)
    model.send(:include, DataMapper::Resource)

    model.property :id, ::DataMapper::Types::Serial

    model.property :code, String, :required => true , :format => /^[a-z][a-z](_[A-Z][A-Z])?$|^#{DEFAULT}$|^#{ALL}$/, :length => 7, :unique_index => true

    model.timestamps :created_at

    model.modified_by Models::USER, :created_by

    model.class_eval <<-EOS, __FILE__, __LINE__
  def self.default
    first(:code => DEFAULT)
  end

  def self.every
    first(:code => ALL)
  end

  def self.first_or_get!(id_or_code)
    first(:code => id_or_code) || get!(id_or_code)
  end

  def self.first_or_get(id_or_code)
    first(:code => id_or_code) || get(id_or_code)
  end
EOS
end

Instance Method Details

#==(other) ⇒ Object



58
59
60
# File 'lib/ixtlan/models/locale.rb', line 58

def ==(other)
  attribute_get(:code).eql?(other.attribute_get(:code))
end

#eql?Object



57
# File 'lib/ixtlan/models/locale.rb', line 57

alias :eql? :==

#hashObject



53
54
55
# File 'lib/ixtlan/models/locale.rb', line 53

def hash
  attribute_get(:code).hash
end

#parentObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ixtlan/models/locale.rb', line 41

def parent
  c = attribute_get(:code)
  case c.size
  when 2
    self.model.default
  when 5
    self.model.get!(code[0,2])
  else
    nil
  end
end