Class: ActiveEnum::Storage::I18nStore

Inherits:
MemoryStore show all
Defined in:
lib/active_enum/storage/i18n_store.rb

Instance Method Summary collapse

Methods inherited from MemoryStore

#_values, #set, #sort!

Methods inherited from AbstractStore

#initialize, #set

Constructor Details

This class inherits a constructor from ActiveEnum::Storage::AbstractStore

Instance Method Details

#check_duplicate(id, name) ⇒ Object



20
21
22
23
24
# File 'lib/active_enum/storage/i18n_store.rb', line 20

def check_duplicate(id, name)
  if _values.assoc(id) || _values.rassoc(name.to_s)
    raise ActiveEnum::DuplicateValue
  end
end

#get_by_id(id) ⇒ Object



6
7
8
9
# File 'lib/active_enum/storage/i18n_store.rb', line 6

def get_by_id(id)
  row = _values.assoc(id)
  [ id, translate(row[1]), row[2] ].compact if row
end

#get_by_name(name) ⇒ Object



11
12
13
14
# File 'lib/active_enum/storage/i18n_store.rb', line 11

def get_by_name(name)
  row = _values.rassoc(name.to_s)
  [ row[0], translate(row[1]), row[2] ].compact if row
end

#i18n_scopeObject



26
27
28
# File 'lib/active_enum/storage/i18n_store.rb', line 26

def i18n_scope
  @i18n_scope ||= [ :active_enum ] + @enum.name.split("::").map { |nesting| nesting.underscore.to_sym }
end

#translate(key) ⇒ Object



30
31
32
# File 'lib/active_enum/storage/i18n_store.rb', line 30

def translate(key)
  I18n.translate key, :scope => i18n_scope, :default => key
end

#valuesObject



16
17
18
# File 'lib/active_enum/storage/i18n_store.rb', line 16

def values
  _values.map { |(id, name)| get_by_id(id) }
end