From 9fbf4d7f0a4ad1e2405a227ea534fd1f1dd8c2e7 Mon Sep 17 00:00:00 2001 From: Jian Weihang Date: Tue, 12 Sep 2017 00:02:31 +0800 Subject: [PATCH] feat: add Data#[] --- ext/exif/data.c | 6 ++++++ test/test_exif.rb | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/ext/exif/data.c b/ext/exif/data.c index a0a9b65..3e816a8 100644 --- a/ext/exif/data.c +++ b/ext/exif/data.c @@ -16,6 +16,7 @@ static const char* attr_readers[] = {"ifds", "aperture_value", "artist", "batter static VALUE new(VALUE self, VALUE input); static VALUE dump(VALUE self); +static VALUE brackets(VALUE self, VALUE ifd_symbol); static void each_content(ExifContent *ec, void *user_data); static void each_entry(ExifEntry *, void *user_data); static VALUE exif_entry_to_rb_value(ExifEntry *); @@ -29,6 +30,7 @@ void init_data(){ rb_define_alias(rb_cExifData, "to_h", "ifds"); rb_define_singleton_method(rb_cExifData, "new", new, 1); rb_define_method(rb_cExifData, "dump", dump, 0); + rb_define_method(rb_cExifData, "[]", brackets, 1); } VALUE new(VALUE self, VALUE input){ @@ -66,6 +68,10 @@ static VALUE dump(VALUE self){ return Qnil; } +static VALUE brackets(VALUE self, VALUE ifd_symbol){ + return rb_hash_aref(rb_iv_get(self, "@ifds"), ifd_symbol); +} + static void each_content(ExifContent *ec, void *self_ptr){ VALUE *self; ExifIfd ifd; diff --git a/test/test_exif.rb b/test/test_exif.rb index 7473107..3473af3 100644 --- a/test/test_exif.rb +++ b/test/test_exif.rb @@ -5,6 +5,14 @@ class TestExif < Minitest::Test module Shared + def test_brackets + assert_equal data.ifds[:ifd0], data[:ifd0] + assert_equal data.ifds[:ifd1], data[:ifd1] + assert_equal data.ifds[:exif], data[:exif] + assert_equal data.ifds[:gps], data[:gps] + assert_equal data.ifds[:interoperability], data[:interoperability] + end + def test_to_h assert_equal data.ifds, data.to_h end