Skip to content

Commit

Permalink
preferred line width may be set
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Jul 7, 2010
1 parent d614c02 commit 6f12aff
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
28 changes: 28 additions & 0 deletions ext/psych/emitter.c
Expand Up @@ -460,6 +460,32 @@ static VALUE indentation(VALUE self)
return INT2NUM(emitter->best_indent);
}

/* call-seq: emitter.line_width
*
* Get the preferred line width.
*/
static VALUE line_width(VALUE self)
{
yaml_emitter_t * emitter;
Data_Get_Struct(self, yaml_emitter_t, emitter);

return INT2NUM(emitter->best_width);
}

/* call-seq: emitter.line_width = width
*
* Set the preferred line with to +width+.
*/
static VALUE set_line_width(VALUE self, VALUE width)
{
yaml_emitter_t * emitter;
Data_Get_Struct(self, yaml_emitter_t, emitter);

yaml_emitter_set_width(emitter, NUM2INT(width));

return width;
}

void Init_psych_emitter()
{
VALUE psych = rb_define_module("Psych");
Expand All @@ -483,6 +509,8 @@ void Init_psych_emitter()
rb_define_method(cPsychEmitter, "canonical=", set_canonical, 1);
rb_define_method(cPsychEmitter, "indentation", indentation, 0);
rb_define_method(cPsychEmitter, "indentation=", set_indentation, 1);
rb_define_method(cPsychEmitter, "line_width", line_width, 0);
rb_define_method(cPsychEmitter, "line_width=", set_line_width, 1);

id_write = rb_intern("write");
}
Expand Down
2 changes: 1 addition & 1 deletion lib/psych/visitors/yaml_tree.rb
Expand Up @@ -54,7 +54,7 @@ def push object
version = @options[:version].split('.').map { |x| x.to_i }
else
version = [1,1]
end if @options[:version]
end if @options.key? :version

@emitter.start_document version, [], false
accept object
Expand Down
6 changes: 6 additions & 0 deletions test/psych/test_emitter.rb
Expand Up @@ -10,6 +10,12 @@ def setup
@emitter = Psych::Emitter.new @out
end

def test_line_width
assert_equal 0, @emitter.line_width
assert_equal 10, @emitter.line_width = 10
assert_equal 10, @emitter.line_width
end

def test_set_canonical
@emitter.canonical = true
assert_equal true, @emitter.canonical
Expand Down

0 comments on commit 6f12aff

Please sign in to comment.