Commit 573e9d08 by ivan Lan

Improve JsonColumn code style and add reader "#{key}_rewrited"

parent b99bf278
...@@ -45,5 +45,9 @@ module Shotengai ...@@ -45,5 +45,9 @@ module Shotengai
def hash_map def hash_map
self.map { |obj| yield obj['key'], obj['val']} self.map { |obj| yield obj['key'], obj['val']}
end end
def merge hash
self.decode.merge(hash)
end
end end
end end
...@@ -36,28 +36,35 @@ module Shotengai ...@@ -36,28 +36,35 @@ module Shotengai
def template_with_value key, value: "#{key}_value", template: "#{key}_template", rewrite: "#{key}_rewrite", 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| val = obj[1].is_a?(Array) ? obj[1].map{ |x| #{trans}[x] } : #{trans}[ obj[1] ]
trans = #{trans} o.merge( obj[0] => val )
val = obj[1].is_a?(Array) ? obj[1].map{ |x| trans[x] } : trans[ obj[1] ] end ,
o.merge( obj[0] => val ) } : ''
end
}
} : '{}'
# QUESTION: 在继承时 self will get (Object doesn't support #inspect) # QUESTION: 在继承时 self will get (Object doesn't support #inspect)
has_rewrite_column = self.has_attribute?(rewrite) rescue superclass.has_attribute?(rewrite) has_rewrite_column = self.has_attribute?(rewrite) rescue superclass.has_attribute?(rewrite)
rewrite_key_val = has_rewrite_column ? "rewrite: #{rewrite}" : ''
rewrite_key_val, define_rewrited_reader =
if has_rewrite_column
[
"rewrite: #{rewrite},",
"def #{key}_rewrited; #{value}.merge(#{rewrite}); end;"
]
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,
#{trans_result}
#{rewrite_key_val} #{rewrite_key_val}
}.merge(#{trans_result}) }
end end
#{define_rewrited_reader}
} }
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