Class: PufferPages::Backends::Mixins::Localable::Locales

Inherits:
ActiveSupport::HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/puffer_pages/backends/models/mixins/localable.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Locales

Returns a new instance of Locales.



21
22
23
24
# File 'lib/puffer_pages/backends/models/mixins/localable.rb', line 21

def initialize source
  update (source.presence || {}).stringify_keys
  super()
end

Class Method Details

.dump(value) ⇒ Object



50
51
52
53
# File 'lib/puffer_pages/backends/models/mixins/localable.rb', line 50

def dump value
  value = value.is_a?(Locales) ? value : Locales.new(value)
  value ? YAML.dump(value.to_hash) : nil
end

.load(value) ⇒ Object



55
56
57
58
59
# File 'lib/puffer_pages/backends/models/mixins/localable.rb', line 55

def load value
  return unless value
  value = YAML.load(value) if value.is_a?(String)
  value.is_a?(Locales) ? value : Locales.new(value)
end

Instance Method Details

#errorObject



30
31
32
33
34
35
36
# File 'lib/puffer_pages/backends/models/mixins/localable.rb', line 30

def error
  translations
rescue ::SyntaxError => e
  e.message
else
  false
end

#translationsObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/puffer_pages/backends/models/mixins/localable.rb', line 38

def translations
  @translations ||= Hash[map do |(locale, yaml)|
    load_arguments = [yaml]
    load_arguments.push "<#{locale}>" if YAML.method(:load).arity == -2
    result = YAML.load yaml
    result = result.presence || {}
    raise ::SyntaxError.new("(<#{locale}>): Locale should be a hash") unless result.is_a?(Hash)
    [locale.to_sym, result.deep_symbolize_keys]
  end]
end

#valid?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/puffer_pages/backends/models/mixins/localable.rb', line 26

def valid?
  !error
end