Config = { # 対å¿ããã¡ãã£ã¢ã¿ã¤ãã®æ¡å¼µå :typemap => %w(xhtml html atom json) } class Resource Config[:typemap].each do |i| # Resource#to_xhtml, Resource#to_html, ... ãå®ç¾©ãã define_method("to_#{i}") do # Resource#to_xml ã¯ã³ã¢ã§å¯¾å¿ããã¦ãã xslt_processor.load(:source => self.to_xml, :template => open("flavour.#{i}.xslt")) xslt_processor.transform end end def xslt_processor @xslt_processor ||= XSLT::Processor.new # ã¦ãã¨ã¼ãªã©ãã end end def get resource = Resource.new media_type = headers.negotiate(:accept) # é©å㪠to_hoge ã¡ã½ãã㸠resource.__send__("to_#{media_type}") end
ãã¥ã¼ã£ã½ããã©ã¢ãã«ã£ã½ããããªæ°ããããã¨ã MVC ããã£ã¡ãç解ãã¦ããªããã¤ãè¨ãã
Resource#xslt_processor ã¿ãããªã¢ã¯ã»ãµã®æ¸ãæ¹ããããããã ãã©ãã©ããªãã ãããããã ||= ã¨ãã代å
¥ã®ä»æ¹ã¯ã¡ã¸ã£ã¼ã ã¨ããããã©ãã¢ã¯ã»ãµã§ã¯ã©ããªãã ããã£ã¦ããã
HTTP ã®ãªã¯ã¨ã¹ã (ã¨ãªã¯ã¨ã¹ã) ãããããã¯ã©ã¹ãä½ã£ã¦ãã³ã³ããã¼ã©ã«ããããã®ã¤ã³ã¹ã¿ã³ã¹ãæãããããã«ãã¦ãããã ãã©ããªã¯ã¨ã¹ãã®ã¤ã³ã¹ã¿ã³ã¹ã«è§¦ãå¿
è¦ãã¾ã£ãããªãã¨ã (åã« Hello World ããã¨ãã¨ã) ã¯ã¤ã³ã¹ã¿ã³ã¹ãä½ããªãããã«ããã¨ããã
gdgd ã¨èª¬æã½ããã¨ãããã©ããããããããããããµãã¦ããããªããã°ãã°ãããã
ã§ã ||= ãã©ããããããªã®ããç¥ã£ãä¸ã§åæã«æãã¤ããã®ã§æ°ã«å
¥ã£ã¦ãããããã£ã¦ããã¾ãã¼ã¿ãããªã