Sha256: a0addec9d90a5cacdbf69fc3a32969f8041612f7c8a9566dff184d125e58fa3b

Contents?: true

Size: 1.34 KB

Versions: 2

Compression:

Stored size: 1.34 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 object) {
  // 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(object, ar_base_type) == Qtrue) {
    return (AttributesWriter){
        .object_type = ActiveRecord,
        .write_attributes = active_record_attributes_writer};
  }
  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.6.0 ext/panko_serializer/attributes_writer/attributes_writer.c
panko_serializer-0.5.10 ext/panko_serializer/attributes_writer/attributes_writer.c