<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>CITguy</title>
 <link href="http://citguy.com/atom.xml" rel="self"/>
 <link href="http://citguy.com/"/>
 <updated>2026-04-12T13:44:08-05:00</updated>
 <id>http://citguy.com</id>
 <author>
   <name></name>
   <email></email>
 </author>

 
 <entry>
   <title>How I Start My Gems</title>
   <link href="http://citguy.com/ruby/2014/07/02/how-i-start-my-gems/"/>
   <updated>2014-07-02T17:54:00-05:00</updated>
   <id>http://citguy.com/ruby/2014/07/02/how-i-start-my-gems</id>
   <content type="html">&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I’ve built quite a few rubygems and I’d like to share how I begin my gems nowadays.&lt;/p&gt;

&lt;p&gt;For the purposes of this article, assume that we want to build a gem called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bad_math&lt;/code&gt;.
This article will not cover how to &lt;em&gt;build&lt;/em&gt; a gem, merely how to make it easier on the developer.
If you’ve not built a gem before, I recommend watching &lt;a href=&quot;http://railscasts.com/episodes/245-new-gem-with-bundler&quot;&gt;Railscast #245: New Gem with Bundler&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;prerequisites&quot;&gt;Prerequisites&lt;/h2&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bundler&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;generate-gem-template&quot;&gt;Generate Gem Template&lt;/h2&gt;
&lt;p&gt;Yes, I &lt;em&gt;could&lt;/em&gt; build the gem directory/file structure manually, but that’s too slow, and bundler makes it hella easy.
(I do recommend doing it manually at least once to appreciate what bundler does for you.)&lt;/p&gt;

&lt;p&gt;Running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bundle gem bad_math&lt;/code&gt; will output something similar to the following:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;~/workspace/gems] &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;bundle gem bad_math
     create  bad_math/Gemfile
     create  bad_math/Rakefile
     create  bad_math/LICENSE.txt
     create  bad_math/README.md
     create  bad_math/.gitignore
     create  bad_math/bad_math.gemspec
     create  bad_math/lib/bad_math.rb
     create  bad_math/lib/bad_math/version.rb
Initializing git repo &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; ~/workspace/gems/bad_math&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Look at that! I’ve got a template and all I have to do is fill in the blanks and start coding.&lt;/p&gt;

&lt;h2 id=&quot;modifying-the-gemspec&quot;&gt;Modifying the gemspec&lt;/h2&gt;
&lt;p&gt;For additional reading, you can view the gem &lt;a href=&quot;http://guides.rubygems.org/specification-reference/&quot;&gt;specification reference&lt;/a&gt; to see all of the various
configuration options that are available to you within this file.  For the sake of this article, I’m only concerned with adding
&lt;a href=&quot;http://rubygems.org/gems/yard&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yard&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;http://rubygems.org/gems/redcarpet&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;redcarpet&lt;/code&gt;&lt;/a&gt; to my development dependencies.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;  &lt;span class=&quot;n&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add_development_dependency&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;yard&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;spec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;add_development_dependency&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;redcarpet&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;These gems work together to provide a very straightforward way of documenting my gem as I build it.
I’ve found that &lt;em&gt;if I have the capability to document from the get go, I’m more likely to actually write documentation&lt;/em&gt;, because nobody
likes an undocumented gem.&lt;/p&gt;

&lt;h2 id=&quot;rakefile-addition&quot;&gt;Rakefile Addition&lt;/h2&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;n&quot;&gt;task&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:console&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;irb&apos;&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;irb/completion&apos;&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&apos;bad_math&apos;&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;ARGV&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clear&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;IRB&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;After reading the short &lt;a href=&quot;http://erniemiller.org/2014/02/05/7-lines-every-gems-rakefile-should-have/&quot;&gt;“7 Lines Every Gem’s Rakefile Should Have”&lt;/a&gt;, I’ve
been adding this to my gems ever since.  As Ernie states, nothing beats playing around in the console, and no you don’t need to use IRB.
You can use the console of your choice.  The point is to start it up a console and load your gem so you can play around with it.&lt;/p&gt;

&lt;h2 id=&quot;configurable-from-the-start&quot;&gt;Configurable From the Start&lt;/h2&gt;
&lt;p&gt;I’ve written gems that started off with what I thought would be rock solid functionality, but find out later down the road that it’s more flexible than
I’d originally thought.  This is the reason why the first functionality I add is a Configuration class.  With configuration set up from the start, it’s
easier to stay flexible with functionality.  Believe me, it’s a pain in the ass to add configuration functionality after the fact that your logic has
mostly been written.&lt;/p&gt;

&lt;h3 id=&quot;libbad_mathconfigurationrb&quot;&gt;lib/bad_math/configuration.rb&lt;/h3&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;BadMath&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Configuration&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# These are our configuration options.&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;attr_accessor&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:nonesuch&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;# default the values here&lt;/span&gt;
      &lt;span class=&quot;vi&quot;&gt;@nonesuch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;nil&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;libbad_mathrb&quot;&gt;lib/bad_math.rb&lt;/h3&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;bad_math/version&quot;&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;require&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;bad_math/configuration&quot;&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;BadMath&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Customize configuration for library&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# @yieldparam [Configuration] config&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;configure&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;block_given?&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# @return [Configuration]&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;config&lt;/span&gt;
    &lt;span class=&quot;vc&quot;&gt;@@config&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Configuration&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So now if I need to use a configurable ‘foobar’ option, I will:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Add the option to the Configuration class (if not already) (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;attr_accessor :foobar&lt;/code&gt;)
    &lt;ul&gt;
      &lt;li&gt;I use attr_accessor because I want to be able to modify the value at any point.&lt;/li&gt;
      &lt;li&gt;If I need to keep a value from changing, I’ll use attr_reader instead.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Set a default value (Configuration#initialize: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@foobar = &quot;&quot;&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;Use it by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BadMath.config.foobar&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;PROFIT!!!&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;user-configuration&quot;&gt;User Configuration&lt;/h3&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;no&quot;&gt;BadMath&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;configure&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;nonesuch&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;I&apos;m not here&quot;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;cfg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foobar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;bizbang&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;
&lt;p&gt;Boom! Bundler to flesh out the boring templating, yard/redcarpet for simple documentation, and a simple configuration setup.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>How I Version APIs</title>
   <link href="http://citguy.com/apis/2014/05/30/how-i-version-apis/"/>
   <updated>2014-05-30T18:37:00-05:00</updated>
   <id>http://citguy.com/apis/2014/05/30/how-i-version-apis</id>
   <content type="html">&lt;p&gt;Sick of having API versioned paths in your rails app? Try using Action Pack Variants instead.&lt;/p&gt;

&lt;p&gt;The following is a simple example on how to implement this functionality with a Rails 4.1 application.&lt;/p&gt;

&lt;h2 id=&quot;set-up-your-routes&quot;&gt;Set up your routes&lt;/h2&gt;
&lt;p&gt;Configure your routes as you would for a JSON endpoint.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;# config/routes.rb&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Rails&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;routes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;draw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;format: :json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;get&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:hello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;controller: :sandbox&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;define-your-custom-vendor-mime-type&quot;&gt;Define your custom Vendor MIME type&lt;/h2&gt;
&lt;p&gt;Try to use a definition that has very little chance of interfering with other possible API services.  Here, I use the java naming notation (com.mydomain.api) to associate the MIME type specifically with my domain.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;# config/initializers/mime_types.rb&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Define a custom Vendor MIME type&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Mime&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;register&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;application/com.mydomain.api-v1+json&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:v1json&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;requestvariant&quot;&gt;request.variant&lt;/h2&gt;
&lt;p&gt;It is now a simple matter of setting the request.variant to the proper variant name for the new MIME format (lines 6-8).&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;# app/controllers/sandbox_controller.rb&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SandboxController&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ApplicationController&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hello&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;respond_to&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;json&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# hello.jbuilder&lt;/span&gt;
      &lt;span class=&quot;nb&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;v1json&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;variant&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:v1&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# hello+v1.jbuilder&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;define-your-variant-views&quot;&gt;Define your variant views&lt;/h2&gt;
&lt;p&gt;Since we used :v1 as our variant name, we will need a hello+v1.jbuilder variant to go along with it.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;# app/views/sandbox/hello.jbuilder&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hello&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;from default json&quot;&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# app/views/sandbox/hello+v1.jbuilder&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;hello&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;from v1 json variant&quot;&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;setup-your-requests&quot;&gt;Setup your requests&lt;/h2&gt;
&lt;p&gt;Finally, your requests will need to send the proper Content-Type header to request the correct response variant.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&lt;span class=&quot;c1&quot;&gt;// GET [Content-Type:&quot;application/json&quot;] /sandbox/hello&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;from default json&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// GET [Content-Type:&quot;application/com.mydomain.api-v1+json&quot;] /sandbox/hello&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;from v1 json variant&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;benefits&quot;&gt;Benefits&lt;/h2&gt;
&lt;p&gt;A huge benefit of doing this way would be the lack of needing to maintain multiple nested, versioned routes and their associated controllers and views.  This reduces your code footprint as well as routing complexity.  Simply modify your output based on the versioned MIME requested.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>OMG! magicExcel</title>
   <link href="http://citguy.com/coldfusion/2010/09/22/omg-magicExcel/"/>
   <updated>2010-09-22T07:00:00-05:00</updated>
   <id>http://citguy.com/coldfusion/2010/09/22/omg-magicExcel</id>
   <content type="html">&lt;p&gt;Have you ever had trouble wading through the ColdFusion 9 documentation pertaining to the new CFSpreadsheet Functions? Have you ever gotten frustrated with generating Excel documents with ColdFusion? Are you tired of boring spreadsheets? Don’t you wish there was an easier way to do it all?&lt;/p&gt;

&lt;p&gt;You’re in luck! A new RIAForge project called “magicExcel” makes all this super simple. Using a custom tag, all you have to do is write HTML.&lt;/p&gt;

&lt;p&gt;Wait! What?&lt;/p&gt;

&lt;p&gt;Yes, that’s right! All you need to create sleek spreadsheets is knowledge of HTML tables.&lt;/p&gt;

&lt;p&gt;Don’t believe me, check it out for yourself at &lt;a href=&quot;http://magicexcel.riaforge.org&quot; target=&quot;_blank&quot;&gt;http://magicexcel.riaforge.org&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>How I Create Custom CFML Tags</title>
   <link href="http://citguy.com/coldfusion/2010/09/13/how-i-create-custom-cfml-tags/"/>
   <updated>2010-09-13T07:00:00-05:00</updated>
   <id>http://citguy.com/coldfusion/2010/09/13/how-i-create-custom-cfml-tags</id>
   <content type="html">&lt;p&gt;While I was cleaning up some code in a portal of ours, I realized that I could condense some of the code by combining a variable assignment with a custom tag attribute assignment.&lt;/p&gt;

&lt;p&gt;To make things simple, the tag I am trying to optimize creates a database record with a note assigned to it.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cfm&quot; data-lang=&quot;cfm&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;  &amp;lt;cfset notes=&quot;A ton of html code here.&quot;&amp;gt;
  &amp;lt;cf_my_tag notes=&quot;#notes#&quot;&amp;gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The issue is that this code wouldn’t necessarily add a note in the same manner, every time. There might be times where I have to build the notes in a loop before I run the tag. Then there are times where I want to run the tag and NOT add a note. Also, I might want to define the note directly in the tag contents.&lt;/p&gt;

&lt;p&gt;So here are the requirements for my new custom tag:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The tag must be willing to accept an attribute named “notes”&lt;/li&gt;
  &lt;li&gt;The tag must be willing to run without defining “notes”&lt;/li&gt;
  &lt;li&gt;The tag must be willing to transform its contents into “notes”, if it is not empty.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With the above constraints in mind, here’s what I came up with.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cfm&quot; data-lang=&quot;cfm&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;!--- Content of my_tag.cfm ---&amp;gt;
&amp;lt;cfscript&amp;gt;
  param ATTRIBUTES.notes = &quot;&quot;;
  if (ThisTag.hasEndTag) { // Closable Tag
    if (ThisTag.ExecutionMode EQ &quot;start&quot;) {
      // ThisTag.GeneratedContent is not set yet.
    } else {
      // This is the best place to execute code
      // All possible &apos;ThisTag&apos; variables are defined.
      if (ThisTag.GeneratedContent NEQ &apos;&apos;) {
        // GeneratedContent takes priority over attribute
        ATTRIBUTES.notes = ThisTag.GeneratedContent;
      }
      engage();
    }
  } else {
    // Self-standing Custom Tag
    engage();
  }
&amp;lt;/cfscript&amp;gt;

&amp;lt;cffunction name=&quot;engage&quot;&amp;gt;
  &amp;lt;cfscript&amp;gt;
    // Capture Generated Content
    // (if we need to refer to the original content)
    LOCAL.content = ThisTag.GeneratedContent;

    // Prevent browser display of GeneratedContent
    ThisTag.GeneratedContent = &quot;&quot;;
  &amp;lt;/cfscript&amp;gt;

  &amp;lt;!--- Do Stuff Here ---&amp;gt;
&amp;lt;/cffunction&amp;gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This setup fulfills all three requirements:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;I can refer to the ATTRIBUTES.notes variable to insert my note.&lt;/li&gt;
  &lt;li&gt;I don’t need to define ATTRIBUTES.notes as it is param’ed at the top of the tag.&lt;/li&gt;
  &lt;li&gt;If I have a closing tag with a non-empty string for its GeneratedContent, the ATTRIBUTES.notes variable will be overwritten with the GeneratedContent. This works for self-closing tags as well.&lt;/li&gt;
  &lt;li&gt;It also allows me to place my logic into a single function rather than splitting it up in an if..else block.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So there you have it. A simple solution to create a custom tag with flexible closing tags.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Gotchas: cfsavecontent and cfoutput</title>
   <link href="http://citguy.com/coldfusion/2010/08/29/cfsavecontent-and-cfoutput/"/>
   <updated>2010-08-29T07:00:00-05:00</updated>
   <id>http://citguy.com/coldfusion/2010/08/29/cfsavecontent-and-cfoutput</id>
   <content type="html">&lt;p&gt;I was playing around with cfsavecontent to generate dynamic HTML variable values for use as an argument to a function when I came across an interesting behavior regarding the tag. The issue I ran was that Coldfusion was giving me an error that the variable saved by cfsavecontent was not defined when I was trying to output the value. Take a look at the code below:&lt;/p&gt;

&lt;p&gt;This code will produce a “Variable not Defined” error in ColdFusion at line #5.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cfm&quot; data-lang=&quot;cfm&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;cfoutput&amp;gt;
  &amp;lt;cfsavecontent variable=&quot;foo&quot;&amp;gt;
    Gozirra!
  &amp;lt;/cfsavecontent&amp;gt;
  #MyFunction(foo)#
&amp;lt;/cfoutput&amp;gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The reason for this is because the cfsavecontent isn’t set until the cfoutput block closes.
Here’s the ugly fix.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-cfm&quot; data-lang=&quot;cfm&quot;&gt;&lt;table class=&quot;rouge-table&quot;&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td class=&quot;gutter gl&quot;&gt;&lt;pre class=&quot;lineno&quot;&gt;1
2
3
4
5
6
7
8
&lt;/pre&gt;&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;pre&gt;&amp;lt;cfoutput&amp;gt;
  &amp;lt;cfsavecontent variable=&quot;foo&quot;&amp;gt;
    Gozirra!
  &amp;lt;/cfsavecontent&amp;gt;
&amp;lt;/cfoutput&amp;gt;
&amp;lt;cfoutput&amp;gt;
  #MyFunction(foo)#
&amp;lt;/cfoutput&amp;gt;
&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</content>
 </entry>
 

</feed>
