Skip to content

Commit

Permalink
Merge bd6aac4 into f70d026
Browse files Browse the repository at this point in the history
  • Loading branch information
iwat committed Nov 21, 2017
2 parents f70d026 + bd6aac4 commit 578791f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ext/gpgme/gpgme_n.c
Expand Up @@ -468,6 +468,28 @@ rb_s_gpgme_data_set_encoding (VALUE dummy, VALUE vdh, VALUE venc)
return LONG2NUM(err);
}

static VALUE
rb_s_gpgme_data_get_file_name (VALUE dummy, VALUE vdh)
{
gpgme_data_t dh;

UNWRAP_GPGME_DATA(vdh, dh);
const char *result = gpgme_data_get_file_name (dh);
return result ? rb_str_new2 (result) : Qnil;
}

static VALUE
rb_s_gpgme_data_set_file_name (VALUE dummy, VALUE vdh, VALUE vfile_name)
{
gpgme_data_t dh;
gpgme_error_t err;

UNWRAP_GPGME_DATA(vdh, dh);
err = gpgme_data_set_file_name (dh,
NIL_P(vfile_name) ? NULL : StringValueCStr(vfile_name));
return LONG2NUM(err);
}

static VALUE
rb_s_gpgme_new (VALUE dummy, VALUE rctx)
{
Expand Down Expand Up @@ -2335,6 +2357,10 @@ Init_gpgme_n (void)
rb_s_gpgme_data_get_encoding, 1);
rb_define_module_function (mGPGME, "gpgme_data_set_encoding",
rb_s_gpgme_data_set_encoding, 2);
rb_define_module_function (mGPGME, "gpgme_data_get_file_name",
rb_s_gpgme_data_get_file_name, 1);
rb_define_module_function (mGPGME, "gpgme_data_set_file_name",
rb_s_gpgme_data_set_file_name, 2);

/* Creating Contexts */
rb_define_module_function (mGPGME, "gpgme_new",
Expand Down
17 changes: 17 additions & 0 deletions lib/gpgme/data.rb
Expand Up @@ -176,6 +176,23 @@ def encoding=(encoding)
encoding
end

##
# Return the file name of the underlying data.
def file_name
GPGME::gpgme_data_get_file_name(self)
end

##
# Sets the file name for this buffer.
#
# @raise [GPGME::Error::InvalidValue] if the value isn't accepted.
def file_name=(file_name)
err = GPGME::gpgme_data_set_file_name(self, file_name)
exc = GPGME::error_to_exception(err)
raise exc if exc
file_name
end

##
# Return the entire content of the data object as string.
def to_s
Expand Down
15 changes: 15 additions & 0 deletions test/data_test.rb
Expand Up @@ -113,6 +113,21 @@
end
end

describe :file_name do
it "has no name by default" do
data = GPGME::Data.new("wadus")
assert_nil data.file_name
end

it "can set file_name" do
data = GPGME::Data.new("wadus")
[ "foo.bar", nil ].each do |file_name|
data.file_name = file_name
assert_equal file_name, data.file_name
end
end
end

describe :to_s do
it "returns the entire content of data" do
data = GPGME::Data.new("wadus")
Expand Down

0 comments on commit 578791f

Please sign in to comment.