module JsonbAccessor::ClassBuilder

def define_nested_accessors(klass, fields_map)

def define_nested_accessors(klass, fields_map)
  klass.class_eval do
    fields_map.nested_fields.keys.each do |attribute_name|
      attr_reader attribute_name
      define_method("#{attribute_name}=") do |value|
        instance_class = nested_classes[attribute_name]
        instance = cast_nested_field_value(value, instance_class, __method__)
        instance_variable_set("@#{attribute_name}", instance)
        attributes[attribute_name] = instance.attributes
        update_parent
      end
    end
  end
end