Class: NaranyaEcm::Rest::Model::LocalizedAttribute

Inherits:
HashWithIndifferentAccess
  • Object
show all
Defined in:
lib/naranya_ecm/rest/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ LocalizedAttribute

Returns a new instance of LocalizedAttribute.



16
17
18
19
20
# File 'lib/naranya_ecm/rest/model.rb', line 16

def initialize(*args)
  super()
  given_attrs = args.shift
  given_attrs.each { |k,v| self[k] = v } unless given_attrs.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/naranya_ecm/rest/model.rb', line 33

def method_missing(method_name, *args, &block)
  if method_name == self.class.aliased_lookup_name
    # El nombre del método debe ser un alias de lookup.
    # Agregar el alias:
    self.class.send :alias_method, self.class.aliased_lookup_name, :lookup
    # llamar el método de lookup. La siguiente ocación no caerá aqui:
    lookup
  else
    super # You *must* call super if you don't handle the
    # method, otherwise you'll mess up Ruby's method
    # lookup.
  end
end

Class Method Details

.aliased_lookup_nameObject



48
49
50
# File 'lib/naranya_ecm/rest/model.rb', line 48

def aliased_lookup_name
  @lookup_alias ||= self.name.demodulize[0..-("Translations".length+1)].underscore.to_sym
end

Instance Method Details

#lookupObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/naranya_ecm/rest/model.rb', line 22

def lookup
  locale = ::I18n.locale
  if ::I18n.respond_to?(:fallbacks)
    lookup_result = self[::I18n.fallbacks[locale].map(&:to_s).find{ |loc| self.has_key?(loc) }]
  else
    lookup_result = self[locale.to_s]
  end
  lookup_result = self.present? ? self.first[1] : nil unless lookup_result.present?
  lookup_result
end