Commit 2c1add8d by ivan Lan

Improve :template_with_value about rewrite

parent 3369bc3d
...@@ -34,7 +34,7 @@ module Shotengai ...@@ -34,7 +34,7 @@ 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", trans: nil, trans_to: :en def template_with_value key, value: "#{key}_value", template: "#{key}_template", rewrite: "#{key}_rewrite", trans: nil, trans_to: :en
trans_result = trans.is_a?(Hash) ? %Q{ trans_result = trans.is_a?(Hash) ? %Q{
{ {
#{trans_to}_value: val&.reduce({}) do |o, obj| #{trans_to}_value: val&.reduce({}) do |o, obj|
...@@ -45,18 +45,23 @@ module Shotengai ...@@ -45,18 +45,23 @@ module Shotengai
} }
} : '{}' } : '{}'
# QUESTION: 在继承时 self will get (Object doesn't support #inspect)
has_rewrite_column = self.has_attribute?(rewrite) rescue superclass.has_attribute?(rewrite)
rewrite_key_val = has_rewrite_column ? "rewrite: #{rewrite}" : ''
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}).merge(try(:#{key}_rewrite) || {}) #{rewrite_key_val}
}.merge(#{trans_result})
end end
} }
end end
def template_with_value_getters *keys, value_in_template: false, delegate_template_to: nil, trans: nil, trans_to: :en def template_with_value_getters *keys, value_in_template: false, delegate_template_to: nil, rewrite: 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
......
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