<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mitch on Medium]]></title>
        <description><![CDATA[Stories by Mitch on Medium]]></description>
        <link>https://medium.com/@mitchocail?source=rss-d46f0b694d07------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*kZyR2N3niRjXtn_V8LaPGQ.jpeg</url>
            <title>Stories by Mitch on Medium</title>
            <link>https://medium.com/@mitchocail?source=rss-d46f0b694d07------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 17:39:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mitchocail/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Simulating a signed in user in Rails using sessions]]></title>
            <link>https://medium.com/@mitchocail/simulating-a-signed-in-user-in-rails-using-sessions-eae39e157c74?source=rss-d46f0b694d07------2</link>
            <guid isPermaLink="false">https://medium.com/p/eae39e157c74</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Mitch]]></dc:creator>
            <pubDate>Mon, 14 Mar 2016 02:41:58 GMT</pubDate>
            <atom:updated>2016-03-14T02:41:58.597Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This is an excerpt from my senior engineering project on how we use sessions for </em><a href="http://mealmate.co/"><em>mealmate.co</em></a></p><p>A session is a small bit of information that a web application is able to store on the server side. For the purposes of mealmate we use them to store information across http requests. A general example of this could be storing a logged in user’s secret key across requests so they are able to access restricted pages.</p><p>As we were developing the application a design feature we wanted to implement was allowing users to use the application whether they are signed in or not. We believed that this would be a great way to set us apart from many of the applications that already exist in the market. Most of the current applications only allow users to view a small subset of their application and require logging in to view the rest. What we accomplished was allowing users to freely use the site up until anything needs to be saved our database, at which point sign in is required.</p><p>A use case for this can be observed when a user wants to create a new meal plan. The user creating the meal plan could be in one of two states, either logged in or not logged in. Users who are logged in have user_stats associated to them, this is where we store information such as height, weight, age, calorie intake…etc. If they are signed in we can grab that information from the database and place it into a session. If the user is not logged in then we will present them a ‘stats’ form which will be used to calculate their daily nutrition. This information will then be stored into a session.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*5eUw8ezrG-hhvKxhffjd9A.png" /></figure><p>From the chart we can see that the meal plan logic does not need to know if the user is signed in or not. It will be passed the session hash and perform the same no matter what the users current state is.</p><p><strong>Drawbacks of sessions and simulating ‘sudo’ sessions</strong></p><p>A Rails design we were not familiar with prior creating this design was that a session is only able to hold 4kb of information at a time. Our sessions were storing user information as well as recipes to be added for a given day of the week. We began seeing overflow errors when users added too many recipes to a meal plan. To combat this we needed to create a ‘sudo’ session, a block of code that acts as a session but is stored and retrieve from the database as opposed to memory. In order to achieve this we created methods to serialize and unserialize a ruby hash. Adding to the session would work in the following way,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/489/1*nvhd3WornSamvG9yIUgduw.png" /></figure><p>This pattern allowed us to create hashes very large in size without the need to worry about overflow errors. It does however, have the drawback of being slower than an in memory hash. However for our needs, we believe that the time tradeoff is well worth it for the extra amount we are able to store.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eae39e157c74" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Observer Pattern in RUBY]]></title>
            <link>https://medium.com/@mitchocail/observer-pattern-in-ruby-e80ac3c1dac7?source=rss-d46f0b694d07------2</link>
            <guid isPermaLink="false">https://medium.com/p/e80ac3c1dac7</guid>
            <dc:creator><![CDATA[Mitch]]></dc:creator>
            <pubDate>Wed, 24 Sep 2014 20:43:55 GMT</pubDate>
            <atom:updated>2014-10-13T23:58:41.054Z</atom:updated>
            <content:encoded><![CDATA[<h4>Overview</h4><p>In this quick article we’re going to go over the Observer Pattern. We’ll make this as quick and as sweet as possible. The purpose is that is you ever find yourself in the need to use the pattern in your own application you’ll have a quick ‘how-to’ guide.</p><h4>When to use the Observer Pattern?</h4><p>The observer pattern is used when you are building a system where the state of one object effects the state of other objects. Take the weather channel for example, it’s their job to report the forcast for the day. They report this information to you, the person observing the broadcast. If the anchor on the weather channel reports that it is going to rain maybe that would mean that you can’t cut the lawn today… it doesn’t really matter. All the anchor is responsible for is reporting the weather, it’s up to you to take that information and plan your day accordingly.</p><p>This is exactly how the observer pattern works. We have one component that’s being ‘watched’ by other components. When the component being watched changes it sends out a message to the other components that basically says “ I’m different now ”, and anyone waiting for this message can change accordingly.</p><p>One of the best thought examples I’ve found of this comes from Russ Olsen in his ‘Design Patterns In Ruby’ book. Think of a spreadsheet application, like Excel, when you change the state of one cell it will effect the numbers in other cells.</p><p>Maybe you created a expenses application and you have a cell to keep track of all the money you’ve been spending. If you were to change the amount in one of the cells it would change your total sum. So, the sum is being a observer to the other cells. When one cell changes the observing cells will need to change accordingly.</p><h4>Using the Observer Pattern</h4><p>For our sample application we’re going to use an example that y Professor used from my Software Development class.</p><p><strong>Let’s say we have a professor who needs to set the date for the midterm. Who will need to know about this date? Well, the Students, they’ll need to study. The Secretary, she will need to book a room. The Dean will need to know, he needs to go over the exam and make sure it’s up to course standards.</strong></p><p>So initially our program, in the simplest form, will now look something like what we have below… try and see if you can spot the bad code?</p><pre>class Professor<br> attr_reader :name, :class_name, :exam_date</pre><pre> def initialize(name, class_name, student, secretary, dean)<br>   <a href="http://twitter.com/name">@name</a> = name<br>   <a href="http://twitter.com/class">@class</a> = class_name<br>   <br>   <a href="http://twitter.com/student">@student</a> = student<br>   <a href="http://twitter.com/secretary">@secretary</a> = secretary<br>   <a href="http://twitter.com/dean">@dean</a> = dean<br> end</pre><pre> def set_midterm(midterm_date)<br>   <a href="http://twitter.com/exam_date">@exam_date</a> = midterm_date<br>   <a href="http://twitter.com/student">@student</a>.update(self)<br>   <a href="http://twitter.com/secretary">@secretary</a>.update(self)<br>   <a href="http://twitter.com/dean">@dean</a>.update(self)<br> end<br>end</pre><pre>class Student<br> def update(prof)<br>   puts “I need to start studying for Prof. #{prof.name} exam on the #{prof.exam_date}”<br> end<br>end </pre><pre>class Secretary<br> def update(prof)<br>   puts “I will find a room for the midterm on the #{prof.exam_date}”<br> end<br>end</pre><pre>class Dean<br> def update(prof)<br>   puts “I will go over the midterm before #{prof.exam_date}”<br> end<br>end</pre><pre>student = Student.new<br>secretary = Secretary.new<br>dean = Dean.new<br>prof = Professor.new(“Jeff Professor”, “Software Engineering”, student, secretary, dean)</pre><pre>prof.set_midterm(“Oct 25th”)</pre><p>*Notice that Professor knows that each class observing it has an update method. He or She wouldn’t care what those methods do, just that they’re there.</p><p><em>output</em></p><pre><strong>Student:</strong> I need to start studying for Prof. Jeff Professor exam on the Oct 25th<br><strong>Secretary:</strong> I will find a room for the midterm on the Oct 25th<br><strong>Dean:</strong> I will go over the midterm before Oct 25t</pre><p>Did you find where the code starts to smell? <strong>It should be fairly easy to see that the Professor class is taking on way too much responsibility in this application.</strong> We are instantiating the student, secretary and dean all in the professor class. Quite frankly the Professor shouldn’t really care what’s watching it. So if we wanted to add another class that’s watching the Professor we would have to change the class again, this is gonna get bad real quick.</p><p>A better approach to this would be to have a list of all the object that are observing the the Professor class. That way whenever we want to add an observer we can just add them to the array. We can achieve this by changing around the Professor class slightly. We’ll create a new array in the initialize method of the class and then create methods to add and delete them from the array.</p><pre>class Professor<br> attr_reader :name, :class_name, :exam_date<br> attr_accessor :observers</pre><pre><br> def initialize(name, class_name)<br>   <a href="http://twitter.com/name">@name</a> = name<br>   <a href="http://twitter.com/class">@class</a> = class_name<br>   <a href="http://twitter.com/observers"><strong>@observers</strong></a><strong> = []</strong><br> end</pre><pre> def set_midterm(midterm_date)<br>   notify_observers<br> end</pre><pre> <strong>def add_observer(observer)<br>   </strong><a href="http://twitter.com/observers"><strong>@observers</strong></a><strong> &lt;&lt; observer<br> end</strong></pre><pre> <strong>def notify_observers<br>   observers.each do |observer|<br>     observer.update(self)<br>   end<br> end</strong><br>end</pre><p>This simple change has already taken a huge load off the professors shoulders, whenever we wanted to add a new observer we would simply call the add_observer method.</p><pre><strong>prof.add_observer(student)<br>prof.add_observer(secretary)<br>prof.add_observer(dean)</strong></pre><p>This code achieves the same end goal of the earlier example, but this one is much cleaner and easier to scale when we need new observers.</p><h4>Going Further</h4><p>we can take the refactoring a step further by factoring out everything that&#39;s specific to the observer methods. One way to accomplish this is through inheritance. We could create a class that’s called Subject specifically for this and then have the Professor class inherit from it. This would accomplish what we want but would not be the most ideal solution because Ruby doesn’t support multiple inheritance. This would use up our single precious inherited base class.</p><p>A better Rubyist way of doing this is to create a module to extract out the behavior we want.</p><p>So here is what our module will look like…</p><pre>module Subject<br>  def initialize<br>    <a href="http://twitter.com/observers">@observers</a> = []<br>  end</pre><pre>  def add_observer(observer)<br>    <a href="http://twitter.com/observers">@observers</a> &lt;&lt; observer<br>  end</pre><pre>  def delete_observer(observer)<br>    <a href="http://twitter.com/observer">@observer</a>.delete(observer)<br>  end</pre><pre>  def notify_observers<br>    <a href="http://twitter.com/observers">@observers</a>.each do |observer|<br>      observer.update(self)<br>    end<br>  end<br>end</pre><p>so now professor looks something like this…</p><pre>class Professor<br> <strong>include Subject</strong><br> attr_reader :name, :class_name, :exam_date<br> attr_accessor :observers</pre><pre><br> def initialize(name, class_name)<br>   <strong>super()</strong><br>   <a href="http://twitter.com/name">@name</a> = name<br>   <a href="http://twitter.com/class">@class</a> = class_name<br> end</pre><pre> def set_midterm(midterm_date)<br>   notify_observers<br> end<br>end</pre><p>Boy! does this class ever look clean. We now have a fully scalable program were our class being observed can communicate it’s changes with anything watching it.</p><h4>Thats all there is to it</h4><p>So there, we can see how our shiny new observer pattern is moving the source of the news (the midterm) to all of it’s consumers.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e80ac3c1dac7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>