Commit 573e9d08 by ivan Lan

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

parent b99bf278
......@@ -45,5 +45,9 @@ module Shotengai
def hash_map
self.map { |obj| yield obj['key'], obj['val']}
end
def merge hash
self.decode.merge(hash)
end
end
end
......@@ -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
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
}
} : '{}'
#{trans_to}_value: val&.reduce({}) do |o, obj|
val = obj[1].is_a?(Array) ? obj[1].map{ |x| #{trans}[x] } : #{trans}[ obj[1] ]
o.merge( obj[0] => val )
end ,
} : ''
# 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}" : ''
rewrite_key_val, define_rewrited_reader =
if has_rewrite_column
[
"rewrite: #{rewrite},",
"def #{key}_rewrited; #{value}.merge(#{rewrite}); end;"
]
end
class_eval %Q{
def #{key}
val = #{value}.is_a?(Harray) ? #{value}.decode : #{value}
{
template: Shotengai::Harray.encode(#{template}).keys,
value: val,
#{trans_result}
#{rewrite_key_val}
}.merge(#{trans_result})
}
end
#{define_rewrited_reader}
}
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