也许这个问题太天真了,但它确实让我感到困惑。这是我的代码,一个职业类。
class Profession < OhmModel
include ProfessionMethods
attribute :type, Type::Integer
attribute :level, Type::Integer
attribute :add_point, Type::Hash
attribute :attribute_add_point, Type::Hash
reference :player, Player
def initialize(args = {})
super
if args.blank?
self.type = 1
self.level = 1
end
@add_point = SerializedHash.new
@attribute_add_point = SerializedHash.new
end
# 初始化加点
def init_add_point(hash)
hash.each do |key, value|
@add_point[key] = value
end
update_add_point
end
def update_add_point
self.add_point = @add_point
self.save
init_attribute_add_point
end
def init_attribute_add_point
self.add_point.each do |key, value|
key_new = key.to_s.gsub('_percent', '').to_sym
PROFESSIONS[type][key_new][:attribute].split(',').each do |attr|
@attribute_add_point[attr] = value
end
end
update_attribute_add_point
end
def update_attribute_add_point
self.attribute_add_point = @attribute_add_point
self.save
end
def name
PROFESSIONS[type][:name]
end
def probability
hash = {}
PROFESSIONS[type].keys.each do |key|
case key
when /property_id_\d?_percent/
hash[key] = PROFESSIONS[type][key]
end
end
return hash
end
def base_value
hash = {}
hash[:basic] = PROFESSIONS[type][:basic]
return hash
end
def points_count
5*level
end
def get_add_point
hash ={}
add_point_hash = self.add_point
add_point_hash.keys.each do |key|
key_new = key.to_s.gsub('_percent', '').to_sym
info = "#{PROFESSIONS[type][key_new][:name]}: #{PROFESSIONS[type][key_new][:des]}"
hash[info] = add_point_hash[key]
end
return hash
end
def fresh_add_point
probability_hash = probability
base_value_hash = base_value
points_count_value = points_count
calculate_probability(probability_hash, base_value_hash, points_count_value)
end
def redistribute_add_point
hash = {}
probability_hash = {}
probability.keys.each do |key|
probability_hash[key] = 1.0/probability.count
end
base_value_hash = base_value
points_count_value = points_count
add_point_hash = calculate_probability(probability_hash, base_value_hash, points_count_value)
init_add_point(add_point_hash)
add_point_hash.keys.each do |key|
key_new = key.to_s.gsub('_percent', '').to_sym
info = "#{PROFESSIONS[type][key_new][:name]}: #{PROFESSIONS[type][key_new][:des]}"
hash[info] = add_point_hash[key]
end
return hash
end
protected
def after_create
hash = fresh_add_point
init_add_point(hash)
end
end
初始化的时候就可以了。
pro = Profession.create
=> #<Profession:0x9504ffc
@_memo={},
@add_point=
{:property_id_1_percent=>104.0,
:property_id_2_percent=>101.0,
:property_id_3_percent=>100.0,
:property_id_4_percent=>100.0,
:property_id_5_percent=>100.0},
@attribute_add_point=
{"crystal_output"=>104.0,
"tritium_output"=>104.0,
"power_consume"=>101.0,
"speed"=>100.0,
"armor"=>100.0,
"hp_damage"=>100.0,
"armor_damage"=>100.0,
"sheild_damage"=>100.0},
@attributes=
{:type=>1,
:level=>1,
:created_at=>1368756861,
:updated_at=>1368756861,
:id=>"34",
:add_point=>
{:property_id_1_percent=>104.0,
:property_id_2_percent=>101.0,
:property_id_3_percent=>100.0,
:property_id_4_percent=>100.0,
:property_id_5_percent=>100.0},
:attribute_add_point=>
{"crystal_output"=>104.0,
"tritium_output"=>104.0,
"power_consume"=>101.0,
"speed"=>100.0,
"armor"=>100.0,
"hp_damage"=>100.0,
"armor_damage"=>100.0,
"sheild_damage"=>100.0}},
@errors={},
@id="34">
当将哈希类型值重新分配给 pro.add_point 并保存对象时,问题就在这里。
pro.add_point = {:one => 1, :two => 2}
pro.save
Profession[34]
=> #<Profession:0x9a544bc
@_memo={},
@add_point={},
@attribute_add_point={},
@attributes=
{:created_at=>"1368756861",
:level=>"1",
:attribute_add_point=>
"{\"crystal_output\":104.0,\"tritium_output\":104.0,\"power_consume\":101.0,\"speed\":100.0,\"armor\":100.0,\"hp_damage\":100.0,\"armor_damage\":100.0,\"sheild_damage\":100.0}",
:add_point=>"{:one=>1, :two=>2}",
:updated_at=>"1368757147",
:type=>"1"},
@id="34">
“add_point”属性保存为普通字符串,不转换为 JSON 样式。