Commit 1ecc6c20 by ivan Lan

Add trans to JsonColumn :template_with_value_getters

parent 08e5d668
...@@ -34,26 +34,34 @@ module Shotengai ...@@ -34,26 +34,34 @@ module Shotengai
harray_setter *columns, decode: decode harray_setter *columns, decode: decode
end end
def template_with_value key, value: "#{key}_value", template: "#{key}_template" def template_with_value key, value: "#{key}_value", template: "#{key}_template", trans: nil, trans_to: :en
trans_result = trans.is_a?(Hash) ? %Q{
#{trans_to}_value: val.reduce({}) do |o, obj|
trans = #{trans}
val = obj[1].is_a?(Array) ? obj[1].map{ |x| trans[x] } : trans[ obj[1] ]
o.merge( obj[0] => val )
end
} : {}
class_eval %Q{ class_eval %Q{
def #{key} def #{key}
val = #{value}.is_a?(Harray) ? #{value}.decode : #{value} val = #{value}.is_a?(Harray) ? #{value}.decode : #{value}
{ {
template: Shotengai::Harray.encode(#{template}).keys, template: Shotengai::Harray.encode(#{template}).keys,
value: val, value: val,
} }.merge(#{trans_result})
end end
} }
end end
def template_with_value_getters *keys, value_in_template: false, delegate_template_to: nil def template_with_value_getters *keys, value_in_template: false, delegate_template_to: nil, trans: nil, trans_to: :en
if delegate_template_to if delegate_template_to
self.delegate(*keys.map { |key| "#{key}_template" }, to: delegate_template_to) self.delegate(*keys.map { |key| "#{key}_template" }, to: delegate_template_to)
end end
keys.each do |key| keys.each do |key|
value = value_in_template ? "Shotengai::Harray.decode(#{key}_template)" : "#{key}_value" value = value_in_template ? "Shotengai::Harray.decode(#{key}_template)" : "#{key}_value"
self.template_with_value key, value: value self.template_with_value key, value: value, trans: trans, trans_to: trans_to
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment