Class: LocalizableModel::AnyLocalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/localizable_model/any_localizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record) ⇒ AnyLocalizer

Returns a new instance of AnyLocalizer.



7
8
9
10
# File 'lib/localizable_model/any_localizer.rb', line 7

def initialize(record)
  @record = record
  @attributes = record.class.localized_attributes.to_set
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/localizable_model/any_localizer.rb', line 17

def method_missing(method_name, *args)
  name = method_name.to_s
  if name.end_with?("?")
    attr = name.delete_suffix("?").to_sym
    return super unless @attributes.include?(attr)

    localized?(attr)
  else
    return super unless @attributes.include?(method_name)

    localized(method_name)
  end
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



5
6
7
# File 'lib/localizable_model/any_localizer.rb', line 5

def record
  @record
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/localizable_model/any_localizer.rb', line 12

def respond_to_missing?(method_name, include_private = false)
  attr = method_name.to_s.delete_suffix("?")
  @attributes.include?(attr.to_sym) || super
end