Class: Locomotive::Steam::Decorators::I18nDecorator
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- Locomotive::Steam::Decorators::I18nDecorator
show all
- Defined in:
- lib/locomotive/steam/decorators/i18n_decorator.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(object, locale = nil, default_locale = nil) ⇒ I18nDecorator
Returns a new instance of I18nDecorator.
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 12
def initialize(object, locale = nil, default_locale = nil)
self.__localized_attributes__ = object.localized_attributes
self.__frozen_locale__ = false
self.__locale__ = locale
self.__default_locale__ = default_locale
super(object)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 84
def method_missing(name, *args, &block)
if __is_localized_attribute__(name)
__get_localized_value__(name)
elsif name.to_s.end_with?('=') && __is_localized_attribute__(name.to_s.chop)
__set_localized_value__(name.to_s.chop, args.first)
else
__getobj__.send(name, *args, &block)
end
end
|
Instance Attribute Details
#__default_locale__ ⇒ Object
Returns the value of attribute default_locale.
10
11
12
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 10
def __default_locale__
@__default_locale__
end
|
#__frozen_locale__ ⇒ Object
Returns the value of attribute frozen_locale.
8
9
10
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 8
def __frozen_locale__
@__frozen_locale__
end
|
#__locale__ ⇒ Object
Returns the value of attribute __locale__.
9
10
11
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 9
def __locale__
@__locale__
end
|
#__localized_attributes__ ⇒ Object
Returns the value of attribute localized_attributes.
7
8
9
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 7
def __localized_attributes__
@__localized_attributes__
end
|
Class Method Details
.decorate(object_or_list, locale = nil, default_locale = nil) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 25
def decorate(object_or_list, locale = nil, default_locale = nil)
decorated = [[object_or_list]].flatten.map do |object|
new(object, locale, default_locale)
end
object_or_list.respond_to?(:localized_attributes) ? decorated.first : decorated
end
|
Instance Method Details
#__freeze_locale__ ⇒ Object
62
63
64
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 62
def __freeze_locale__
@__frozen_locale__ = true
end
|
#__get_localized_value__(name) ⇒ Object
74
75
76
77
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 74
def __get_localized_value__(name)
field = __getobj__.public_send(name.to_sym)
field ? field[__locale__] || field[__default_locale__] : nil
end
|
#__is_localized_attribute__(name) ⇒ Object
70
71
72
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 70
def __is_localized_attribute__(name)
__localized_attributes__.include?(name.to_sym)
end
|
#__set_localized_value__(name, value) ⇒ Object
79
80
81
82
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 79
def __set_localized_value__(name, value)
field = __getobj__.public_send(name.to_sym)
field[__locale__] = value
end
|
#__unfreeze_locale__ ⇒ Object
66
67
68
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 66
def __unfreeze_locale__
@__frozen_locale__ = false
end
|
#__with_default_locale__(&block) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 54
def __with_default_locale__(&block)
if self.__default_locale__
__with_locale__(self.__default_locale__, &block)
else
yield
end
end
|
#__with_locale__(locale, &block) ⇒ Object
45
46
47
48
49
50
51
52
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 45
def __with_locale__(locale, &block)
old_locale, self.__locale__ = __locale__, locale.to_sym
self.__freeze_locale__
yield.tap do
self.__unfreeze_locale__
self.__locale__ = old_locale
end
end
|
#as_json(options = nil) ⇒ Object
103
104
105
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 103
def as_json(options = nil)
to_hash.as_json(options)
end
|
#inspect ⇒ Object
112
113
114
115
116
117
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 112
def inspect
"[Decorated #{__getobj__.class.name}][I18n] attributes exist? " +
__getobj__.respond_to?(:attributes).inspect +
', localized attributes: ' + @__localized_attributes__.inspect +
', locale: ' + @__locale__.inspect
end
|
#to_hash ⇒ Object
95
96
97
98
99
100
101
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 95
def to_hash
__getobj__.to_hash.tap do |hash|
@__localized_attributes__.keys.each do |name|
hash[name.to_s] = __get_localized_value__(name)
end
end
end
|
#to_json ⇒ Object
107
108
109
|
# File 'lib/locomotive/steam/decorators/i18n_decorator.rb', line 107
def to_json
as_json.to_json
end
|