<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Chronicles of a Ruby Developer</title>
    <description></description>
    <link>http://daphsta.github.io/</link>
    <atom:link href="http://daphsta.github.io/feed.xml" rel="self" type="application/rss+xml" />
    <pubDate>Thu, 29 Jan 2015 11:40:49 +0000</pubDate>
    <lastBuildDate>Thu, 29 Jan 2015 11:40:49 +0000</lastBuildDate>
    <generator>Jekyll v2.4.0</generator>
    
      <item>
        <title>Using Interactors to organise business logic</title>
        <description>&lt;h4&gt;What is an Interactor&lt;/h4&gt;

&lt;p&gt;My team has started using the &lt;a href=&quot;https://github.com/collectiveidea/interactor&quot;&gt;Interactor&lt;/a&gt; gem to encapsulate complex business logic hence, freeing up the controllers. The Interactor gem presents us with a very nice way of representing one thing that the application does and allowing us to adhere to the Single Responsibility Principle. Also, if there is a series of action that are required by the business logic, we are able to organise a sequence of interactors using the &lt;em&gt;Organizer&lt;/em&gt; method.&lt;/p&gt;

&lt;p&gt;I believe that it would be easier to demonstrate the functionalities of the &lt;em&gt;Interactor&lt;/em&gt; gem through code, of course.
For a working app, pull from this &lt;a href=&quot;https://github.com/daphsta/interactor_example_app&quot;&gt;github repo&lt;/a&gt; to view the working code. The app is built using the &lt;a href=&quot;https://github.com/rails-api/rails-api&quot;&gt;rails-api&lt;/a&gt; gem which is a lightweight rails app minus all the unnecessary view modules.&lt;/p&gt;

&lt;p&gt;In this app, I would want to create a &lt;em&gt;customer&lt;/em&gt; record and associate it to my &lt;em&gt;user_id&lt;/em&gt; which is my &lt;em&gt;sales_rep&lt;/em&gt; id.
I would create an interactor that would &lt;em&gt;FindSalesRep&lt;/em&gt; and the user_id would be extracted from the params in the JSON document. &lt;/p&gt;

&lt;h4&gt;Constructing an Interactor&lt;/h4&gt;

&lt;p&gt;A &lt;code&gt;context&lt;/code&gt; contains everything that an &lt;em&gt;Interactor&lt;/em&gt; needs to work with. The params from the controller sets the &lt;code&gt;context&lt;/code&gt; when an &lt;em&gt;Interactor&lt;/em&gt; is called. 
Creating an interactor is easy. Just create a class that includes &lt;em&gt;Interactor&lt;/em&gt; and define an instance method &lt;code&gt;call&lt;/code&gt;, whereby the &lt;code&gt;context&lt;/code&gt; can be accessed within it. &lt;/p&gt;
&lt;div 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;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FindSalesRep&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Interactor&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;SalesRep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales_rep_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;rescue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Working with a Context&lt;/h4&gt;

&lt;p&gt;As an &lt;em&gt;Interactor&lt;/em&gt; is run, you may add information to the context.&lt;/p&gt;
&lt;div 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;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales_rep_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;em&gt;sales&lt;em&gt;rep&lt;/em&gt;id&lt;/em&gt; can be accessed by the controller that runs the &lt;em&gt;Interactor&lt;/em&gt;.
&lt;code&gt;ruby
  result = FindSalesRep.call(params)
  puts &amp;quot;Sales rep id #{result.sales_rep_id}&amp;quot;
&lt;/code&gt;
To set a failing &lt;code&gt;context&lt;/code&gt; when an operation has failed, a &lt;code&gt;context.fail!&lt;/code&gt; can be flagged. This can be accessed by the controller to render a flash message.&lt;/p&gt;
&lt;div 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;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;FindSalesRep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;success?&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;flash&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;now&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:error&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h4&gt;Organizing multiple Interactors&lt;/h4&gt;

&lt;p&gt;I find the organizer function, which is a variation of the basic &lt;em&gt;Interactor&lt;/em&gt;, very helpful in explicitly defining the multiple responsibilities require by the controller when processing an action. The organizer is designed to run multiple &lt;em&gt;Interactors&lt;/em&gt; in one &lt;em&gt;Interactor&lt;/em&gt; and contents in the &lt;code&gt;context&lt;/code&gt; is passed to every &lt;em&gt;Interactor&lt;/em&gt; that the organizer organizes. Each &lt;em&gt;Interactor&lt;/em&gt; may change the &lt;code&gt;context&lt;/code&gt; by adding on more information into it to be passed on to the next &lt;em&gt;Interactor&lt;/em&gt;. If one &lt;em&gt;Interactor&lt;/em&gt; fails its &lt;code&gt;context&lt;/code&gt;, the organizer would not call the subsequent &lt;em&gt;Interactors&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The code will provide more clarity on this concept.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;client_controller.rb&lt;/code&gt;&lt;/p&gt;
&lt;div 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;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ClientsController&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;create&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;AssociateSalesRepToClient&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;success?&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;client&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;res&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;status&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;404&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;associate_sales_rep_to_client.rb&lt;/code&gt;&lt;/p&gt;
&lt;div 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;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;AssociateSalesRepToClient&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Interactor&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Organizer&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;organize&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;FindSalesRep&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;CreateClient&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;find_sales_rep.rb&lt;/code&gt;&lt;/p&gt;
&lt;div 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;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FindSalesRep&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Interactor&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;SalesRep&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales_rep_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;rescue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;create_client.rb&lt;/code&gt;&lt;/p&gt;
&lt;div 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;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;CreateClient&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Interactor&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;begin&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;sales_rep_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sales_rep_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;abn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;abn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;client&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;result&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;rescue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;context&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fail!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the example above, the organizer &lt;code&gt;AssociateSalesRepToClient&lt;/code&gt; defines the process of creating a Client. The task of creating a Client record requires a sales rep id to be retrieved and saved in a Client record. This example may be contrived, but it would be sufficient to demonstrate the concept of organizers.&lt;/p&gt;

&lt;p&gt;The create will run &lt;code&gt;AssociateSalesRepToClient&lt;/code&gt; &lt;em&gt;Interactor&lt;/em&gt; whereby &lt;code&gt;user_id&lt;/code&gt; is a key in the params. The &lt;code&gt;FindSalesRep&lt;/code&gt; &lt;em&gt;Interactor&lt;/em&gt; will run and that would set &lt;code&gt;sales_rep_id&lt;/code&gt; into the context to be passed onto &lt;code&gt;CreateClient&lt;/code&gt; &lt;em&gt;Interactor&lt;/em&gt;. The Client would then be created and save if all runs well. If &lt;code&gt;FindSalesRep&lt;/code&gt; runs into an error(user id not found), &lt;code&gt;CreateClient&lt;/code&gt; &lt;em&gt;Interactor&lt;/em&gt; will not be run and the context will be failed.
The controller will receive a false &lt;code&gt;context.success?&lt;/code&gt; which would proceed to render the error message.&lt;/p&gt;

&lt;h4&gt;Conclusion&lt;/h4&gt;

&lt;p&gt;&lt;em&gt;Interactors&lt;/em&gt; are a nice way to reuse operations and explicitly specifying a sequence of actions in order to execute business logic. It also provides a way to bubble back errors when they occur in any of the operation. If in any case the error needs to rollback an operation, a rollback method can be specified in the &lt;em&gt;Interactor&lt;/em&gt;. This serves as an option (compared to service objects and use cases) to trim down the controllers by moving business logic out of it.&lt;/p&gt;
</description>
        <pubDate>Sun, 25 Jan 2015 14:34:25 +0000</pubDate>
        <link>http://daphsta.github.io/ruby%20rails%20learn/2015/01/25/interactors-to-organise-business-logic.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/ruby%20rails%20learn/2015/01/25/interactors-to-organise-business-logic.html</guid>
        
        <category>rails</category>
        
        <category>ruby</category>
        
        <category>pattern</category>
        
        
        <category>ruby rails learn</category>
        
      </item>
    
      <item>
        <title>Inject,each_with_object,reduce - fun Ruby enumerables</title>
        <description>&lt;p&gt;I have learnt to love Enumerables and it is imperative to understand how it works because nobody would want to write 
nested &lt;code&gt;for&lt;/code&gt; loops all the time.
Solving problems in &lt;a href=&quot;http://www.exercism.io&quot;&gt;Exercism&lt;/a&gt; has taught me the joy of using enumerables and how it can effeciently solve problems in a succint manner.&lt;/p&gt;

&lt;p&gt;Take for example this problem from the &lt;strong&gt;Difference of Squares&lt;/strong&gt; exercise from Exercism.&lt;/p&gt;

&lt;p&gt;The sum of the squares of the first ten natural numbers is,&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;1**2 + 2**2 + ... + 10**2 = 385
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The square of the sum of the first ten natural numbers is,&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;(1 + 2 + ... + 10)**2 = 55**2 = 3025
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To calculate the sum of squares, I initially chose to use the &lt;code&gt;each_with_object&lt;/code&gt; and &lt;code&gt;inject&lt;/code&gt; methods.&lt;/p&gt;
&lt;div 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;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sum_of_squares&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each_with_object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sq&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inject&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:+&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h5&gt;each&lt;em&gt;with&lt;/em&gt;object(obj) { |(*args), memo_obj| ... } definition&lt;/h5&gt;

&lt;blockquote&gt;
&lt;p&gt;Iterates the given block for each element with an arbitrary object given, and returns the initially given object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first iteration goes through the given collection, &lt;code&gt;(1..num)&lt;/code&gt; and returns an array, which is the object given to &lt;code&gt;each_with_object([])&lt;/code&gt;. You can pass in an empty hash object, &lt;code&gt;each_with_object({})&lt;/code&gt; to return a hash result.
In the block, &lt;code&gt;sq&lt;/code&gt; becomes the accumulator value(memo value) that will accumulate an array of squares by iterating through every element, &lt;code&gt;n&lt;/code&gt;.
And because an array is returned, I could method chain the result with an &lt;code&gt;inject&lt;/code&gt; method to sum every element in the array.&lt;/p&gt;

&lt;p&gt;The #inject method is really lovely in the sense that it allows you to apply an operation on a collection by specifying a block or symbol.&lt;/p&gt;

&lt;h5&gt;inject(initial, sym) → obj definition&lt;/h5&gt;

&lt;blockquote&gt;
&lt;p&gt;Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.&lt;/p&gt;

&lt;p&gt;If you specify a block, then for each element in enum the block is passed an accumulator value (memo) and the element. If you specify a symbol instead, then each element in the collection will be passed to the named method of memo. In either case, the result becomes the new value for memo. At the end of the iteration, the final value of memo is the return value for the method.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;According to the definition on &lt;a href=&quot;http://ruby-doc.org/core-2.2.0/Enumerable.html#method-i-each_with_object&quot;&gt;Ruby Doc&lt;/a&gt;, &lt;code&gt;inject&lt;/code&gt; expects an initial value and a symbol whereby each element in the collection will be passed to the name of the method, in this case a &lt;code&gt;:+&lt;/code&gt; which would sum every element in the collection.&lt;/p&gt;

&lt;p&gt;In the above example I would have 
&lt;code&gt;ruby
[1,4,9,16,25,36,49,64,81,100].inject(0,:+)
&lt;/code&gt;
This is just a looong way to arrive at the desired result and I think I can do better. With just a bit of refactoring of the first iteration of my solution and it should look good. So, I have decided to use the enumerator &lt;code&gt;reduce&lt;/code&gt;, which essentially is an alias for &lt;code&gt;inject&lt;/code&gt;. I think &lt;code&gt;reduce&lt;/code&gt; gives a better meaning to this example as it is collapsing all the elements into one.&lt;/p&gt;
&lt;div 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;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sum_of_squares&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As per the definition for &lt;code&gt;inject&lt;/code&gt;, I am able to specify a block in &lt;code&gt;reduce&lt;/code&gt;, and an accumulator value, in this case &lt;code&gt;sum&lt;/code&gt;.
Because there isn&amp;#39;t an initial value specified in this block, &lt;code&gt;sum&lt;/code&gt; is going to assume the value of the first element as the initial value(in this case &lt;code&gt;1&lt;/code&gt;) and &lt;code&gt;n&lt;/code&gt; will be set to the 2nd element.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;num&lt;/code&gt; was set to 2, we would have the code with values as such:&lt;/p&gt;
&lt;div 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;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sum_of_squares&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reduce&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sum&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;**&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;sum&lt;/code&gt; initial value in this case would be 1 and &lt;code&gt;n&lt;/code&gt; would be set to 2, hence the returning the value 5.&lt;/p&gt;

&lt;h4&gt;Conclusion&lt;/h4&gt;

&lt;p&gt;If you are coming from a language like C++, you would find these methods delightful to use (hence creating happy Rubyists). I would use it to build hash or an array by applying a method onto every element in the collection. &lt;code&gt;each_with_object&lt;/code&gt; may be an overkill for the problem above but it is definitely useful when transforming values that we have queried from the database. &lt;/p&gt;
</description>
        <pubDate>Fri, 23 Jan 2015 14:34:25 +0000</pubDate>
        <link>http://daphsta.github.io/ruby/2015/01/23/ruby-enumerables.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/ruby/2015/01/23/ruby-enumerables.html</guid>
        
        
        <category>ruby</category>
        
      </item>
    
      <item>
        <title>Raindrops exercise in Exercism</title>
        <description>&lt;p&gt;So this is how I spent my Friday night before bedtime, solving a problem on &lt;a href=&quot;http://www.http://exercism.io/daphsta&quot;&gt;Exercism&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;This is the problem&lt;/h4&gt;

&lt;h6&gt;Raindrops&lt;/h6&gt;

&lt;p&gt;Write a program that converts a number to a string, the contents of which depends on the number&amp;#39;s prime factors.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the number contains 3 as a prime factor, output &amp;#39;Pling&amp;#39;.&lt;/li&gt;
&lt;li&gt;If the number contains 5 as a prime factor, output &amp;#39;Plang&amp;#39;.&lt;/li&gt;
&lt;li&gt;If the number contains 7 as a prime factor, output &amp;#39;Plong&amp;#39;.&lt;/li&gt;
&lt;li&gt;If the number does not contain 3, 5, or 7 as a prime factor,
just pass the number&amp;#39;s digits straight through.&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;Examples&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;28&amp;#39;s prime-factorization is 2, 2, 7.

&lt;ul&gt;
&lt;li&gt;In raindrop-speak, this would be a simple &amp;quot;Plong&amp;quot;.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;1755 prime-factorization is 3, 3, 3, 5, 13.

&lt;ul&gt;
&lt;li&gt;In raindrop-speak, this would be a &amp;quot;PlingPlang&amp;quot;.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;The prime factors of 34 are 2 and 17.

&lt;ul&gt;
&lt;li&gt;Raindrop-speak doesn&amp;#39;t know what to make of that,
so it just goes with the straightforward &amp;quot;34&amp;quot;.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;My solution (not the best but will go through another iteration)&lt;/h4&gt;
&lt;div 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;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Raindrops&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;RAINDROPS&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s1&quot;&gt;&amp;#39;3&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Pling&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s1&quot;&gt;&amp;#39;5&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Plang&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s1&quot;&gt;&amp;#39;7&amp;#39;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&amp;#39;Plong&amp;#39;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&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;convert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prime_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prime_num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prime_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_s&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;find_prime_factors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prime_num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;drops&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;map&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;RAINDROPS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;drops&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prime_num&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_s&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;drops&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&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;find_prime_factors&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&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;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
          &lt;span class=&quot;n&quot;&gt;primes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is_prime?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&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;n&quot;&gt;primes&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&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;is_prime?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Math&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ceil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;each&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;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;num&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Yes, I do realise there is a Prime class that I could use, but I didn&amp;#39;t want to.
If you haven&amp;#39;t checkout &lt;a href=&quot;http://www.exercism.io&quot;&gt;Exercism&lt;/a&gt; already, you&amp;#39;re still not too late to the party.&lt;/p&gt;
</description>
        <pubDate>Fri, 16 Jan 2015 14:34:25 +0000</pubDate>
        <link>http://daphsta.github.io/ruby/2015/01/16/raindrops-excercism.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/ruby/2015/01/16/raindrops-excercism.html</guid>
        
        
        <category>ruby</category>
        
      </item>
    
      <item>
        <title>Why you should give technical talks</title>
        <description>&lt;p&gt;A Newbie month at my local Ruby meetup had spurred me to give a talk for the first time. It was definitely nerve-racking considering
I&amp;#39;ve only been coding in Ruby for a little over a year now. After submitting the topic of my &lt;a href=&quot;https://github.com/BrisRuby/meetups/tree/master/2014-11-24&quot;&gt;talk&lt;/a&gt;,
there was no turning back and I have to give my best shot.&lt;/p&gt;

&lt;p&gt;Coincidentally, I came across a blogpost regarding &lt;a href=&quot;https://www.reinteractive.net/posts/225-thoughts-on-doing-technical-talks&quot;&gt;giving technical talks&lt;/a&gt; by &lt;a href=&quot;https://www.reinteractive.net/&quot;&gt;reInteractive&lt;/a&gt;
which I found really encouraging and gave me a further boost of confidence. This has been really helpful to me and I would like to add my thoughts
and experiences on why every developer should attempt to give technical talks and how to minimize the fear and trepidation.&lt;/p&gt;

&lt;h5&gt;How does giving talks benefit me&lt;/h5&gt;

&lt;p&gt;We love challenges and public speaking is a big challenge for the majority of the society. By overcoming a challenge, you build character and a certain amount of immunity towards fear, in this case, fear of public speaking. The ability to speak in front of a small crowd gives you the confidence when communicating with team mates and clients on a day to day basis. Being a good communicator is a very valuable skill as a developer and should be developed.
We have also probably heard about this time and again that, speaking forces you to research on a subject matter thoroughly and as a result, you would understand it intimately.&lt;/p&gt;

&lt;h5&gt;Coming up with a topic to speak on makes you think&lt;/h5&gt;

&lt;p&gt;As developers, I know we do a lot of thinking and probably do it all day and even in our sleep (certain problems do get solved in our sleep).
However, coming up with a topic to speak makes you think in an entirely different perspective. It takes a bit of work to identify an area of interest,
a problem that you have endeavour to solve, or just general experience, to compose a talk out of it. To help ease the process, writing your ideas
in &lt;a href=&quot;http://www.trello.com&quot;&gt;Trello&lt;/a&gt; at the end of your working day is a start. You can then narrow down on what might interest you (and your potential audience) to warrant
further research. Of course, it will be more adventurous to give a talk on a subject you don&amp;#39;t really know about but would like to understand it more. For starters, it would be best to talk about something you are working on or have worked with, for example the usage of certain gems.&lt;/p&gt;

&lt;h5&gt;But my topic is too trivial and everyone knows about the subject matter&lt;/h5&gt;

&lt;p&gt;If you think that your topic might be suited for entry level, try talking during a Beginner/Newbie/RailsGirls night at the meetup. Understand that beginner nights are meant for speakers who are beginners as well. It doesn&amp;#39;t matter if you think that everyone might have a great deal of understanding on what you will talk about because there will always be a handful of people who would find your topic very informational. &lt;/p&gt;

&lt;p&gt;You will be surprised to find that there are a lot of people who really appreciate entry level talks rather than something that they might not have heard of before. Just keep in mind that it is always refreshing to have at least one entry level talk among the heavy weight topics. As a community who encourages and nurtures beginners, we should show more empathy towards the audience.&lt;/p&gt;

&lt;h5&gt;What if I can&amp;#39;t answer every question by the audience&lt;/h5&gt;

&lt;p&gt;Ask for help. You&amp;#39;re not going to be punished for not knowing an answer to a question. In fact, you will remember the answer for life and that is very valuable. Of course you should always try your best to present your opinion but you could get feedback on that from the audience as well. After all, we meet up to learn from each other and not judge on intelligence level.&lt;/p&gt;

&lt;h5&gt;You don&amp;#39;t need x amount of years of experience to be able to give talks&lt;/h5&gt;

&lt;p&gt;You can always talk about your experience as well and by articulating your learning experience in a talk, you will be able to fine tune your learning strategy as well as inspire others.
Lightning talk is a great way to talk about something relatively small but technical. A topic as small as &lt;em&gt;how to use form_for&lt;/em&gt; is great for a 5 minute talk. &lt;/p&gt;

&lt;h5&gt;I get nervous doing public speaking&lt;/h5&gt;

&lt;p&gt;The proverbial phrase, &lt;em&gt;practice makes perfect&lt;/em&gt; holds very true in gaining confidence when speaking. I have found that, people who nod in agreement of what you have to say, gives a boost of confidence which calms the nerves. Try looking for that one person in that audience who nods and try to focus your attention at that person. If you are absolutely anal about everything going as planned, gather some friends to be in the audience and stress that it is imperative that they nod when you are speaking. &lt;/p&gt;

&lt;p&gt;As you can deduce from all the aforementioned points, it is mostly about a change in perception that would allay any fears in giving talks. Many a time, our fears inhibit us from doing anything. Once all these fears are alleviated, we are able to conquer anything that seemed challenging in the beginning. I hope that this would inspire you to have a crack at giving a talk at your local meetup group. &lt;/p&gt;
</description>
        <pubDate>Mon, 12 Jan 2015 14:34:25 +0000</pubDate>
        <link>http://daphsta.github.io/ruby%20learn%20self-development/2015/01/12/why-you-should-give-technical-talks.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/ruby%20learn%20self-development/2015/01/12/why-you-should-give-technical-talks.html</guid>
        
        
        <category>ruby learn self-development</category>
        
      </item>
    
      <item>
        <title>New Year!</title>
        <description>&lt;p&gt;Happy 2015! First task on my 2015 list is to get a blog with &lt;a href=&quot;http://jekyllrb.com&quot;&gt;jekyll&lt;/a&gt;. &lt;/p&gt;
</description>
        <pubDate>Fri, 02 Jan 2015 14:34:25 +0000</pubDate>
        <link>http://daphsta.github.io/misc/2015/01/02/new-year.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/misc/2015/01/02/new-year.html</guid>
        
        
        <category>misc</category>
        
      </item>
    
      <item>
        <title>CodeSchool 4th and 5th week</title>
        <description>&lt;h3&gt;Week 4- BrainTree integration&lt;/h3&gt;

&lt;p&gt;The whole week was focused on the payment workflow and integration with BrainTree. It was a complex process getting our app to integrate and ensuring all the proper fields are passed on to their servers. I&amp;#39;m not saying that it is complicated to integrate with BrainTree, in fact once you have all the right information and keys set up, it is a straightforward process if the app accepts payments and passes it onto BrainTree&amp;#39;s servers.&lt;/p&gt;

&lt;p&gt;We have chosen to use the Transparent Redirect transaction whereby all payments will be submitted directly to BrainTree&amp;#39;s servers and no credit card information will be stored on the app server. Every merchant who signs up will be given a set of API keys to be included in the app.rb. Developers can sign up for a sandbox account to obtain the keys &lt;/p&gt;

&lt;p&gt;To create transactions, a HTML form_for is set up to retrieve the Customer&amp;#39;s Details and Credit Card Details. The form includes a hidden transaction data which contains the transaction type and URL to be directed to.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;tr_data = Braintree::TransparentRedirect.transaction_data(
  :redirect_url =&amp;gt; &amp;quot;http://example.com/url_to_redirect_to&amp;quot;,
  :transaction =&amp;gt; {
    :type =&amp;gt; &amp;quot;sale&amp;quot;,
    :amount =&amp;gt; &amp;quot;10.00&amp;quot;
  }
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;All we needed to do in our controller is to verify if the attendee has received an invitation and to create the transaction details to be passed on to their servers. The app will then await for a URL confirmation from BrainTree&amp;#39;s servers and notify the user if they payment has been successful or failed. Obtaining the URL confirmation is as simple as setting the API below:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;result = Braintree::TransparentRedirect.confirm(request.query_string)                                                                   
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Week 5 - API design and Handlebars templates&lt;/h3&gt;

&lt;p&gt;I was really looking forward towards learning about writing our own APIs and how the APIs would be consumed in the front end of the app. Why did we need APIs in our app? The index page contains a dashboard with a live feed of the status of attendees. Visitors will be able to see the number of people who are Awaiting for Invitation, Received Invitation, Paid, Received Reminder, Confirmed and Declined. Visitors will be able to register to get an invitation and then go through the payment process for confirmation. &lt;/p&gt;

&lt;p&gt;Abiding by API conventions, we use namespace to keep track of our versioning. The Api::V1::BaseController responds to a JSON string and authenticates the HTTP request with a token. The Api::V1::AttendeesController &amp;lt; Api::V1::BaseController responds with a serialized string of the statistics of attendees to be displayed on the dashboard. To serialized, we used Active-Model-Serializer again to construct a JSON string. &lt;/p&gt;

&lt;p&gt;To consume the API, our front end is written in CoffeeScript to render the statistics to be displayed on the dashboard. I must say that it takes a bit of getting used to writing JavaScript without the {},; and var. Just like Ruby, all CoffeeScript syntaxes are terse and it compiles into JavaScript. One of the advantages of using CoffeeScript is that, variables are not global by default hence it would prevent confusion of having variables with the same names from different JS files. I think I&amp;#39;m sticking to CoffeeScript and would not touch JavaScript unless necessary. &lt;/p&gt;

&lt;p&gt;The data from the API is passed into a function that will invoke the HandlebarsTemplates.&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-text&quot; data-lang=&quot;text&quot;&gt;HandlebarsTemplates[&amp;#39;attendee_statistics&amp;#39;](data.attendee_statistics);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This will run the Handlebars file stored in the asset pipeline and it will be compiled, compressed and cached like all the JavaScript files in the app.&lt;/p&gt;
</description>
        <pubDate>Fri, 21 Jun 2013 07:36:58 +0000</pubDate>
        <link>http://daphsta.github.io/2013/06/21/codeschool-4th-and-5th-week.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/2013/06/21/codeschool-4th-and-5th-week.html</guid>
        
        <category>API</category>
        
        <category>BrainTree</category>
        
        <category>codeschool</category>
        
        <category>coffeescript</category>
        
        <category>handlebars</category>
        
        
      </item>
    
      <item>
        <title>Rails Girls Summer of Code</title>
        <description>&lt;p&gt;I&amp;#39;ve also been occupying my time in organizing a team to participate in the Rails Girls Summer of Code . I just chanced upon this in my Twitter feed and thought I would check it out to see what the buzz is all about. As I was reading through it, my eyes grew the widest it has ever did. I knew I could totally do this, all I needed was a female pair, a coach to support us, and a mentor to guide us in the right direction of the project.&lt;/p&gt;

&lt;p&gt;Looking for a pair wouldn&amp;#39;t be too hard because there are so many enthusiastic women from Rails Girls who would immediately jump into an opportunity like that. The actual uphill task was convincing an individual/company to coach us for the entire duration of this program which is 3 months and dedicating a couple of hours a day to coach us. Students are paid a stipend of $1,500 by Rails Girls organization and coaches would be doing this pro bono. &lt;/p&gt;

&lt;p&gt;I knew I couldn&amp;#39;t just sit and wait for Rails Girls to find us a coach in our city and that would mean going the extra mile to hunt down people in the Rails community who would be kind enough to dedicate themselves for a good cause. After randomly tweeting people, we are finally in the midst of talking to someone who could potentially dedicate his time to coach us! The next step is to put in our application as a team and cross our fingers in hopes of being selected as a successful applicant for this program. &lt;/p&gt;

&lt;p&gt;To give you an idea, I have embedded the Rails Girls video. Gosh, I hope we get it!&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.vimeo.com/67004430%20w=320&amp;amp;h=180&quot;&gt;Rails Girls Summer of Code&lt;/a&gt;&lt;/p&gt;
</description>
        <pubDate>Mon, 03 Jun 2013 08:16:11 +0000</pubDate>
        <link>http://daphsta.github.io/2013/06/03/rails-girls-summer-of-code.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/2013/06/03/rails-girls-summer-of-code.html</guid>
        
        <category>Rails Girls</category>
        
        <category>Summer of Code</category>
        
        
      </item>
    
      <item>
        <title>CodeSchool 3rd week - Tokens</title>
        <description>&lt;p&gt;That was all we did in week 3, creating email tokens and testing it. It doesn&amp;#39;t sound like a lot for 6 hours of lesson but we sure did a lot of discovering when dealing with BCrypt in creating tokens and the output of encoding the hash using urlsafe_encode64. &lt;/p&gt;

&lt;p&gt;At the moment, every attendee will have a 3 unique tokens generated for the actions confirm, pay and decline.  These tokens are generated using the BCrypt::Password create function with a hash.&lt;/p&gt;

&lt;p&gt;BCrypt is the most recommended hash function compared to MD5,SHA1,SHA256 etc. From my understanding, BCrypt uses a variant of the Blowfish encryption algorithm that introduces a work factor which determines how expensive the hash function will be. BCrypt is really slow as the work factor increases compared to other hash functions. This would only mean it would take much longer to crack passwords with massive amounts of resources.&lt;/p&gt;

&lt;p&gt;With every email sent out, there will be a link for the attendee to click on to notify of his/her decision be it confirm, pay or decline in one step. Dependent on their decision, the attendee object will be moved into a different state and forwarded to the appropriate link. &lt;/p&gt;

&lt;p&gt;This is where BrainTree payment gateway comes into play in the app. All credit card transactions and information storage is handled by BrainTree. The integration to their APIs will be in the next few lessons. &lt;/p&gt;

&lt;p&gt;Of course we took a lot of time writing the RSpec tests on the tokens and the links and refactoring our code to ensure there are no unnecessary repetition. At the moment we have not done anything to the view hence I can&amp;#39;t provide any screenshots of our progress. However, this is the project link https://github.com/net-engine/tedx-brisbane in case you want to have a look around.&lt;/p&gt;
</description>
        <pubDate>Mon, 03 Jun 2013 08:10:47 +0000</pubDate>
        <link>http://daphsta.github.io/2013/06/03/codeschool-3rd-week-tokens.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/2013/06/03/codeschool-3rd-week-tokens.html</guid>
        
        <category>codeschool</category>
        
        
      </item>
    
      <item>
        <title>CodeSchool 2nd week - Creating background workers to be passed on to an EmailDelivery service</title>
        <description>&lt;p&gt;So, we have finally arrived at the heart of the app which is sending invitations and confirmations through email using Mandrill. Mandrill is an email service with SMTP setup and API integration. There is even a Mandrill course on Codecademy (yipee!) if going through the documentation is not your thing at the moment.&lt;/p&gt;

&lt;p&gt;We actually spent a lot of time constructing the email background worker model using Sidekiq,  and an email deliverer model using HTTParty gem to serialize the content of the email to be sent to Madrill&amp;#39;s API. The body of the email is serialized using the ActiveModel::Serializer class which basically allows the user to customize the JSON output; beats passing hashes around.&lt;/p&gt;

&lt;p&gt;With a big part of the backend constructed, we needed a front end for the administrator to be able to control the invitations and with that, we were introduced to the ActiveAdmin framework which eliminates the need to build an interface and the basic admin processes.&lt;/p&gt;

&lt;p&gt;ActiveAdmin interface making it easy for developers to customize&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s the most interesting part of the course by far, Capybara testing on the interface! What Capybara does is, it allows the developer to test the application by simulating how a user would interact with it. Capybara comes with a bunch of DSLs that emulates the language a user would use ie: page.should have_content(&amp;quot;New User&amp;quot;), Attendee.count.should == 1. Language that is so similar to the English language that I struggled a bit understanding how it should work at a low level (this stems from my C++ background). I soon got a hang of it and stopped converting it to C++ constantly in order to understand the DSLs used.&lt;/p&gt;

&lt;p&gt;Finally, our app has a front end and in the next few lessons, we will be integrating all the parts together. And now ,back to my revision and preparation for the upcoming lesson.&lt;/p&gt;

&lt;p&gt;p/s: My opinions are based on a beginner&amp;#39;s understanding of RoR. Please do correct me if I&amp;#39;ve made any mistake in terms of terminology and concept.&lt;/p&gt;
</description>
        <pubDate>Sun, 26 May 2013 12:14:31 +0000</pubDate>
        <link>http://daphsta.github.io/2013/05/26/codeschool-2nd-week-creating-background-workers-to-be-passed-on-to-an-emaildelivery-service.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/2013/05/26/codeschool-2nd-week-creating-background-workers-to-be-passed-on-to-an-emaildelivery-service.html</guid>
        
        <category>Capybara</category>
        
        <category>codeschool</category>
        
        <category>Mandrill</category>
        
        <category>RSpec</category>
        
        
      </item>
    
      <item>
        <title>First week of code school</title>
        <description>&lt;p&gt;And so it began, my experience in building a Rails app with the guidance of a professional. The class is definitely not for the ultimate beginner as attendees are expected to have completed the Michael Hartl&amp;#39;s Ruby on Rails tutorial. Don&amp;#39;t give up on this tutorial if you think the first chapter , which is setting up the environment and familiarizing with GitHub, is too tedious and you just can&amp;#39;t wait to start coding. You just need to push through the setting up stage (which is unfortunately a lengthy process) before seeing the fruits of your labor. Also, I would recommend diving straight into this tutorial rather than going through TryRuby because it is easy to lose the plot halfway through TryRuby. I got lost through the stories in poems in TryRuby. Michael Hartl has an entire chapter on Ruby itself and that would be sufficient to understand what is going on in Rails.&lt;/p&gt;

&lt;p&gt;Lots of RSpec on the first day of the 3 hour lesson using FactoryGirl of course and setting up state machines for the app. I like the idea of using state machines to define the behavior of a class rather than toggling boolean variables which will end up really messy as it gets complex. &lt;/p&gt;

&lt;p&gt;Our next task was to design the flow of how an email is supposed to be delivered using thread workers and integrating with a 3rd party email delivery API. It was helpful to decide how the controller should call the methods before actually writing the RSpec tests and the functions in the model. We were introduced to RSpec method stubbing which I&amp;#39;m still trying to get used to it.&lt;/p&gt;

&lt;p&gt;All in all , it was a good first week of learning and I&amp;#39;m definitely better than I was a month ago.   &lt;/p&gt;
</description>
        <pubDate>Sun, 19 May 2013 05:36:40 +0000</pubDate>
        <link>http://daphsta.github.io/ruby%20rails%20learn/2013/05/19/first-week-of-code-school.html</link>
        <guid isPermaLink="true">http://daphsta.github.io/ruby%20rails%20learn/2013/05/19/first-week-of-code-school.html</guid>
        
        <category>codeschool</category>
        
        <category>RSpec</category>
        
        <category>ruby on rails</category>
        
        <category>state machines</category>
        
        <category>tutorial</category>
        
        
        <category>ruby rails learn</category>
        
      </item>
    
  </channel>
</rss>
