1 module action_dispatch.format; 2 3 class Format { 4 protected { 5 string _format; 6 } 7 8 this(string format) { 9 _format = format; 10 } 11 12 @property string format() { 13 return _format; 14 } 15 16 void html(void delegate() yield) { 17 yield_for_format("html", yield); 18 } 19 20 void json(void delegate() yield) { 21 yield_for_format("json", yield); 22 } 23 24 void xml(void delegate() yield) { 25 yield_for_format("xml", yield); 26 } 27 28 protected { 29 void yield_for_format(string format, void delegate() yield) { 30 if (_format == format) { 31 yield(); 32 } 33 } 34 } 35 } 36 37 unittest { 38 import dunit.toolkit; 39 40 struct Foo { int a; } 41 42 Foo html_foo = { a: 0 }; 43 html_foo.a.assertEqual(0); 44 auto html_f = new Format("html"); 45 html_f.format.assertEqual("html"); 46 html_f.html(delegate void() { html_foo.a = 2; }); 47 html_foo.a.assertEqual(2); 48 html_f.xml(delegate void() { html_foo.a = 3; }); 49 html_foo.a.assertEqual(2); 50 51 Foo json_foo = { a: 1 }; 52 auto json_f = new Format("json"); 53 json_f.format.assertEqual("json"); 54 json_f.json(delegate void() { json_foo.a = 3; }); 55 json_foo.a.assertEqual(3); 56 json_f.html(delegate void() { json_foo.a = 0; }); 57 json_foo.a.assertEqual(3); 58 59 Foo xml_foo = { a: 5 }; 60 auto xml_f = new Format("xml"); 61 xml_f.format.assertEqual("xml"); 62 xml_f.xml(delegate void() { xml_foo.a = 4; }); 63 xml_foo.a.assertEqual(4); 64 xml_f.json(delegate void() { xml_foo.a = 6; }); 65 xml_foo.a.assertEqual(4); 66 }