123456789_123456789_123456789_123456789_123456789_

Class: Psych::Emitter

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Handler
Instance Chain:
self, Handler
Inherits: Psych::Handler
Defined in: ext/psych/psych_emitter.c

Constant Summary

Handler - Inherited

EVENTS, OPTIONS

Class Method Summary

Instance Attribute Summary

Handler - Inherited

#streaming?

Is this handler a streaming handler?

Instance Method Summary

Handler - Inherited

#alias

Called when an alias is found to anchor.

#empty

Called when an empty event happens.

#end_document

Called with the document ends.

#end_mapping

Called when a map ends.

#end_sequence

Called when a sequence ends.

#end_stream

Called when the YAML stream ends.

#event_location

Called before each event with line/column information.

#scalar

Called when a scalar value is found.

#start_document

Called when the document starts with the declared version, tag_directives, if the document is implicit.

#start_mapping

Called when a map starts.

#start_sequence

Called when a sequence is started.

#start_stream

Called with encoding when the YAML stream starts.

Constructor Details

.new(io, options = Psych::Emitter::OPTIONS)

Create a new Emitter that writes to io.

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 74

static VALUE initialize(int argc, VALUE *argv, VALUE self)
{
    yaml_emitter_t * emitter;
    VALUE io, options;
    VALUE line_width;
    VALUE indent;
    VALUE canonical;

    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    if (rb_scan_args(argc, argv, "11", &io, &options) == 2) {
        line_width = rb_funcall(options, id_line_width, 0);
        indent     = rb_funcall(options, id_indentation, 0);
        canonical  = rb_funcall(options, id_canonical, 0);

        yaml_emitter_set_width(emitter, NUM2INT(line_width));
        yaml_emitter_set_indent(emitter, NUM2INT(indent));
        yaml_emitter_set_canonical(emitter, Qtrue == canonical ? 1 : 0);
    }

    rb_ivar_set(self, id_io, io);
    yaml_emitter_set_output(emitter, writer, (void *)self);

    return self;
}

Instance Attribute Details

#canonical (rw)

Get the output style, canonical or not.

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 495

static VALUE canonical(VALUE self)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    return (emitter->canonical == 0) ? Qfalse : Qtrue;
}

#canonical=(true) (rw)

Set the output style to canonical, or not.

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 481

static VALUE set_canonical(VALUE self, VALUE style)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_emitter_set_canonical(emitter, Qtrue == style ? 1 : 0);

    return style;
}

#indentation (rw)

Get the indentation level.

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 522

static VALUE indentation(VALUE self)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    return INT2NUM(emitter->best_indent);
}

#indentation=(level) (rw)

Set the indentation level to level. The level must be less than 10 and greater than 1.

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 508

static VALUE set_indentation(VALUE self, VALUE level)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_emitter_set_indent(emitter, NUM2INT(level));

    return level;
}

#line_width (rw)

Get the preferred line width.

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 534

static VALUE line_width(VALUE self)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    return INT2NUM(emitter->best_width);
}

#line_width=(width) (rw)

Set the preferred line with to width.

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 546

static VALUE set_line_width(VALUE self, VALUE width)
{
    yaml_emitter_t * emitter;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_emitter_set_width(emitter, NUM2INT(width));

    return width;
}

Instance Method Details

#alias(anchor)

Emit an alias with anchor.

See Handler#alias

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 456

static VALUE alias(VALUE self, VALUE anchor)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    if(!NIL_P(anchor)) {
        Check_Type(anchor, T_STRING);
        anchor = rb_str_export_to_enc(anchor, rb_utf8_encoding());
    }

    yaml_alias_event_initialize(
            &event,
            (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValueCStr(anchor))
            );

    emit(emitter, &event);

    return self;
}

#end_document(implicit)

End a document emission with an implicit ending.

See Handler#end_document

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 257

static VALUE end_document(VALUE self, VALUE imp)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_document_end_event_initialize(&event, imp ? 1 : 0);

    emit(emitter, &event);

    return self;
}

#end_mapping

Emit the end of a mapping.

See Handler#end_mapping

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 437

static VALUE end_mapping(VALUE self)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_mapping_end_event_initialize(&event);

    emit(emitter, &event);

    return self;
}

#end_sequence

End sequence emission.

See Handler#end_sequence

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 373

static VALUE end_sequence(VALUE self)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_sequence_end_event_initialize(&event);

    emit(emitter, &event);

    return self;
}

#end_stream

End a stream emission

See Handler#end_stream

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 126

static VALUE end_stream(VALUE self)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_stream_end_event_initialize(&event);

    emit(emitter, &event);

    return self;
}

#scalar(value, anchor, tag, plain, quoted, style)

Emit a scalar with value, anchor, tag, and a plain or quoted string type with style.

See Handler#scalar

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 277

static VALUE scalar(
        VALUE self,
        VALUE value,
        VALUE anchor,
        VALUE tag,
        VALUE plain,
        VALUE quoted,
        VALUE style
        ) {
    yaml_emitter_t * emitter;
    yaml_event_t event;
    rb_encoding *encoding;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    Check_Type(value, T_STRING);

    encoding = rb_utf8_encoding();

    value = rb_str_export_to_enc(value, encoding);

    if(!NIL_P(anchor)) {
        Check_Type(anchor, T_STRING);
        anchor = rb_str_export_to_enc(anchor, encoding);
    }

    if(!NIL_P(tag)) {
        Check_Type(tag, T_STRING);
        tag = rb_str_export_to_enc(tag, encoding);
    }

    yaml_scalar_event_initialize(
            &event,
            (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValueCStr(anchor)),
            (yaml_char_t *)(NIL_P(tag) ? NULL : StringValueCStr(tag)),
            (yaml_char_t*)StringValuePtr(value),
            (int)RSTRING_LEN(value),
            plain ? 1 : 0,
            quoted ? 1 : 0,
            (yaml_scalar_style_t)NUM2INT(style)
            );

    emit(emitter, &event);

    return self;
}

#start_document(version, tags, implicit)

Start a document emission with YAML version, tags, and an implicit start.

See Handler#start_document

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 237

static VALUE start_document(VALUE self, VALUE version, VALUE tags, VALUE imp)
{
    struct start_document_data data = {
        .self = self,
        .version = version,
        .tags = tags,
        .imp = imp,

        .head = NULL,
    };

    return rb_ensure(start_document_try, (VALUE)&data, start_document_ensure, (VALUE)&data);
}

#start_mapping(anchor, tag, implicit, style)

Start emitting a YAML map with anchor, tag, an implicit start and end, and style.

See Handler#start_mapping

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 393

static VALUE start_mapping(
        VALUE self,
        VALUE anchor,
        VALUE tag,
        VALUE implicit,
        VALUE style
        ) {
    yaml_emitter_t * emitter;
    yaml_event_t event;
    rb_encoding *encoding;

    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    encoding = rb_utf8_encoding();

    if(!NIL_P(anchor)) {
        Check_Type(anchor, T_STRING);
        anchor = rb_str_export_to_enc(anchor, encoding);
    }

    if(!NIL_P(tag)) {
        Check_Type(tag, T_STRING);
        tag = rb_str_export_to_enc(tag, encoding);
    }

    yaml_mapping_start_event_initialize(
            &event,
            (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValueCStr(anchor)),
            (yaml_char_t *)(NIL_P(tag) ? NULL : StringValueCStr(tag)),
            implicit ? 1 : 0,
            (yaml_mapping_style_t)NUM2INT(style)
            );

    emit(emitter, &event);

    return self;
}

#start_sequence(anchor, tag, implicit, style)

Start emitting a sequence with anchor, a tag, implicit sequence start and end, along with style.

See Handler#start_sequence

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 330

static VALUE start_sequence(
        VALUE self,
        VALUE anchor,
        VALUE tag,
        VALUE implicit,
        VALUE style
        ) {
    yaml_emitter_t * emitter;
    yaml_event_t event;

    rb_encoding * encoding = rb_utf8_encoding();

    if(!NIL_P(anchor)) {
        Check_Type(anchor, T_STRING);
        anchor = rb_str_export_to_enc(anchor, encoding);
    }

    if(!NIL_P(tag)) {
        Check_Type(tag, T_STRING);
        tag = rb_str_export_to_enc(tag, encoding);
    }

    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);

    yaml_sequence_start_event_initialize(
            &event,
            (yaml_char_t *)(NIL_P(anchor) ? NULL : StringValueCStr(anchor)),
            (yaml_char_t *)(NIL_P(tag) ? NULL : StringValueCStr(tag)),
            implicit ? 1 : 0,
            (yaml_sequence_style_t)NUM2INT(style)
            );

    emit(emitter, &event);

    return self;
}

#start_stream(encoding)

Start a stream emission with encoding

See Handler#start_stream

[ GitHub ]

  
# File 'ext/psych/psych_emitter.c', line 106

static VALUE start_stream(VALUE self, VALUE encoding)
{
    yaml_emitter_t * emitter;
    yaml_event_t event;
    TypedData_Get_Struct(self, yaml_emitter_t, &psych_emitter_type, emitter);
    Check_Type(encoding, T_FIXNUM);

    yaml_stream_start_event_initialize(&event, (yaml_encoding_t)NUM2INT(encoding));

    emit(emitter, &event);

    return self;
}