impl {{ wrapper }} {
/// Create a new bridge wrapping a Ruby object.
/// Validates that the Ruby object responds to all required methods.
pub fn new({{ ctor_params }}) -> {{ ctor_return_type }} {
// SAFETY: constructors are called by generated Magnus registration functions.
let ruby = unsafe { magnus::Ruby::get_unchecked() };
{%- for req_method in required_methods %}
if !rb_obj.respond_to("{{ req_method }}", false).unwrap_or(false) {
let ruby = unsafe { magnus::Ruby::get_unchecked() };
return Err(magnus::Error::new(
ruby.exception_runtime_error(),
format!("Ruby object missing required method: {}", "{{ req_method }}"),
));
}
{%- endfor %}
let runtime_dispatcher = {{ dispatcher_name }}::new(&ruby, rb_obj)?;
Ok(Self {
inner: magnus::value::Opaque::from(rb_obj),
cached_name: name,
runtime_dispatcher,
{%- for opt_method in optional_methods %}
has_{{ opt_method }}: rb_obj.respond_to("{{ opt_method }}", false).unwrap_or(false),
{%- endfor %}
})
}
}