Sha256: 423e6e7723f574df9775168d965ff1f94e5897ae1c89cc028f872c63dee90705

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

#include "attributes_writer.h"

static bool types_initialized = false;
static VALUE ar_base_type = Qundef;

VALUE init_types(VALUE v) {
  if (types_initialized == true) {
    return Qundef;
  }

  types_initialized = true;

  volatile VALUE ar_type =
      rb_const_get_at(rb_cObject, rb_intern("ActiveRecord"));
  ar_base_type = rb_const_get_at(ar_type, rb_intern("Base"));

  return Qundef;
}

AttributesWriter create_attributes_writer(VALUE subject) {
  // If ActiveRecord::Base can't be found it will throw error
  int isErrored;
  rb_protect(init_types, Qnil, &isErrored);

  if (ar_base_type != Qundef &&
      rb_obj_is_kind_of(subject, ar_base_type) == Qtrue) {
    return (AttributesWriter){
        .object_type = ActiveRecord,
        .write_attributes = active_record_attributes_writer};
  } else {
    return (AttributesWriter){.object_type = Plain,
                              .write_attributes = plain_attributes_writer};
  }

  return create_empty_attributes_writer();
}

void empty_write_attributes(VALUE obj,
                            VALUE attributes,
                            EachAttributeFunc func,
                            VALUE writer) {}

AttributesWriter create_empty_attributes_writer() {
  return (AttributesWriter){.object_type = Unknown,
                            .write_attributes = empty_write_attributes};
}

void init_attributes_writer(VALUE mPanko) {
  init_active_record_attributes_writer(mPanko);
}

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
panko_serializer-0.5.7 ext/panko_serializer/attributes_writer/attributes_writer.c
panko_serializer-0.5.6 ext/panko_serializer/attributes_writer/attributes_writer.c