attribute_localizer 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/attribute_localizer.rb +22 -22
- metadata +3 -3
data/lib/attribute_localizer.rb
CHANGED
@@ -16,7 +16,7 @@ module AttributeLocalizer
|
|
16
16
|
|
17
17
|
src = attributes.map do |attribute|
|
18
18
|
str = "def #{attribute}\n"
|
19
|
-
str << "@#{attribute} || I18n.t(\"\#{self.class.send :localization_namespace}.\#{localization_hash_key}.#{attribute}\", :locale => locale
|
19
|
+
str << "@#{attribute} || I18n.t(\"\#{self.class.send :localization_namespace}.\#{localization_hash_key}.#{attribute}\", :locale => locale)\n"
|
20
20
|
str << "end"
|
21
21
|
str
|
22
22
|
end.join("\n")
|
@@ -25,30 +25,15 @@ module AttributeLocalizer
|
|
25
25
|
|
26
26
|
src = <<-end_src
|
27
27
|
|
28
|
-
def as_json(options = {})
|
29
|
-
instance_values
|
30
|
-
super
|
31
|
-
end
|
32
|
-
|
33
|
-
def to_json(options = {})
|
34
|
-
instance_values
|
35
|
-
super
|
36
|
-
end
|
37
|
-
|
38
28
|
def locale
|
39
29
|
@locale || I18n.locale
|
40
30
|
end
|
41
31
|
|
42
|
-
def instance_values
|
43
|
-
values = super
|
44
|
-
values['attributes'].merge!(#{attributes.map { |attribute| "'#{attribute}' => #{attribute}" }.join(', ') })
|
45
|
-
values
|
46
|
-
end
|
47
|
-
|
48
32
|
end_src
|
49
33
|
|
50
34
|
module_eval src, __FILE__, __LINE__
|
51
35
|
|
36
|
+
|
52
37
|
private
|
53
38
|
|
54
39
|
klass.send :define_method, 'i18n_file' do |locale|
|
@@ -58,17 +43,26 @@ module AttributeLocalizer
|
|
58
43
|
klass.send :private, 'i18n_file'
|
59
44
|
|
60
45
|
src = <<-end_src
|
46
|
+
|
61
47
|
def load_locale_data(locale)
|
62
48
|
localized_hash = YAML::load_file(i18n_file(locale))
|
63
49
|
localized_hash[locale][localization_namespace]
|
64
50
|
rescue
|
65
51
|
{}
|
66
52
|
end
|
53
|
+
|
54
|
+
def instantiate(record)
|
55
|
+
object = super
|
56
|
+
object.instance_variable_get('@attributes').merge!(#{attributes.map { |attribute| "'#{attribute}' => nil" }.join(', ') })
|
57
|
+
object
|
58
|
+
end
|
59
|
+
|
67
60
|
end_src
|
68
61
|
|
69
62
|
klass.class_eval src, __FILE__, __LINE__
|
70
63
|
|
71
64
|
klass.send :private, 'load_locale_data'
|
65
|
+
klass.send :private, 'instantiate'
|
72
66
|
|
73
67
|
klass.send :define_method, 'localization_namespace' do
|
74
68
|
to_s.underscore.pluralize
|
@@ -87,21 +81,27 @@ module AttributeLocalizer
|
|
87
81
|
src = <<-end_src
|
88
82
|
|
89
83
|
private
|
84
|
+
|
85
|
+
def attributes_from_column_definition
|
86
|
+
super.merge(#{attributes.map { |attribute| "'#{attribute}' => nil" }.join(', ') })
|
87
|
+
end
|
88
|
+
|
90
89
|
def localization_hash_key
|
91
90
|
"_\#{id}".to_sym
|
92
91
|
end
|
93
92
|
|
93
|
+
def localized_yaml
|
94
|
+
{
|
95
|
+
#{attributes.map { |attribute| ":#{attribute} => #{attribute}"}.join(',')}
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
94
99
|
def serialize_localizations
|
95
100
|
localized_hash = self.class.send :load_locale_data, locale
|
96
101
|
localized_hash[localization_hash_key] = localized_yaml
|
97
102
|
self.class.send :write_locale_data, locale, localized_hash
|
98
103
|
end
|
99
104
|
|
100
|
-
def localized_yaml
|
101
|
-
{
|
102
|
-
#{attributes.map { |attribute| ":#{attribute} => #{attribute}"}.join(',')}
|
103
|
-
}
|
104
|
-
end
|
105
105
|
end_src
|
106
106
|
|
107
107
|
module_eval src, __FILE__, __LINE__
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Michael Deering
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-22 00:00:00 -06:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|