<?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 Christian Rolle on Medium]]></title>
        <description><![CDATA[Stories by Christian Rolle on Medium]]></description>
        <link>https://medium.com/@christianrolle?source=rss-5ec07ebf06a0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UDS6wGirwgDnhwILB8sDXw.png</url>
            <title>Stories by Christian Rolle on Medium</title>
            <link>https://medium.com/@christianrolle?source=rss-5ec07ebf06a0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 01:00:37 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@christianrolle/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[Understanding the Strategy pattern]]></title>
            <link>https://christianrolle.medium.com/understanding-the-strategy-pattern-25bb5817460b?source=rss-5ec07ebf06a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/25bb5817460b</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-patterns]]></category>
            <category><![CDATA[strategy-pattern]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Christian Rolle]]></dc:creator>
            <pubDate>Sat, 18 Feb 2023 07:18:23 GMT</pubDate>
            <atom:updated>2023-02-18T07:18:23.815Z</atom:updated>
            <content:encoded><![CDATA[<p>The overall objective of the Strategy pattern is similar to the Template Method pattern. Both are higher level behavioral patterns. Though the TemplateMethod pattern is heavily based on inheritance.<br>The Strategy pattern otherwise is aiming at defined interfaces.</p><p>Let’s evaluate with a code example. And Ruby is awesome with that.<br>The story goes about sharing an article and marking it as shared. First the problematic approach.</p><pre>class Article<br>  attr_accessor :text<br>  attr_reader :title, :published_at<br>  <br>  def initialize(title:)<br>    @title = title<br>  end<br>  <br>  def share(publisher:)<br>    case publisher<br>    when :linked_in<br>      TwitterAPI.new.release title: title, text: text<br>    when :twitter<br>      MediumAPI.new.release title: title, text: text<br>    end<br>    @published_at = Time.current<br>  end<br>end</pre><p>The problem is obvious. What if there should another publisher? Sharing the article and how to share is tightly coupled. Let’s unravel the knot with the Strategy pattern.<br>The idea behind is to move the sharing-API specifics into a strategy object and pass it to the article by dependency injection.</p><pre>class Article<br>  attr_accessor :text<br>  attr_reader :title, :published_at<br>  <br>  def initialize(title:)<br>    @title = title<br>  end<br>  <br>  def share(publisher:)<br>    publisher.release title: title, text: text<br>    @published_at = Time.current<br>  end<br>end<br><br>class MediumAPI<br>  def release(title:, text:)<br>    # the Medium API integration details<br>  end<br>end<br><br>class LinkedInAPI<br>  def release(title:, text:)<br>    # the LinkedIn API integration details<br>  end<br>end</pre><p>With that it doesn’t matter how many social-media APIs ever will be wired. Just inject another strategy meaning another API integration. However article specifics like setting the publishing time still remains in the articles responsibility. By applying the Strategy a high degree of flexibility can be achieved.</p><pre>article = Article.new title: &#39;Understanding the Strategy pattern&#39;<br>article.text = &#39;The overall objective of the Strategy pattern ...&#39;<br>article.share LinkedInAPI.new<br>article.published_at<br># =&gt; Sat, 18 Feb 2023 07:10:01 UTC +00:00</pre><p>The article even could be shared with MySpace if it was still around.</p><p>What this statement is trying to say by is separating the concerns the world out there may change but has little effect on the own implementation just by applying the proper pattern.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25bb5817460b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dependency Injection pattern]]></title>
            <link>https://christianrolle.medium.com/dependency-injection-pattern-83b35b05d506?source=rss-5ec07ebf06a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/83b35b05d506</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[object-oriented]]></category>
            <dc:creator><![CDATA[Christian Rolle]]></dc:creator>
            <pubDate>Sat, 18 Feb 2023 05:57:12 GMT</pubDate>
            <atom:updated>2023-04-04T20:58:43.532Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UF4ss5PXLK8j8_DcB-I16g.png" /><figcaption>Injecting the dependency</figcaption></figure><p>OO design has to deal with objects interacting with each other. The resulting dependencies between those objects are potential reasons for side effects.<br>Therefore, the dependencies should be reduced as much as possible.</p><h3>Coupled dependencies</h3><p>Let’s imagine a story concerning taxation as an example for a tight coupling between two objects: Taxation and the Person to be taxed.</p><p>We go with Ruby because Ruby is lovely readable.</p><pre>class Taxation<br>  attr_reader :person<br>  <br>  def initialize(income)<br>    @person = Person.new(income: income)<br>  end<br>  <br>  def charge<br>    person.income.to_d * person.tax_rate.to_d / 100<br>    # algorithm for collecting the tax<br>  end<br>end</pre><p>With this naive approach obviously only people are taxable.</p><pre>Taxation.new(5_000).taxation.charge 20<br># person is charged 1.000</pre><p>The dependency to the taxable entity is baked right into the taxation object itself. However there are also other entities like companies or nonprofit organizations having their specific tax rate. They also should be taxable.</p><h3>Dependency Injection</h3><p>Oftentimes the required flexibility could be achieved with Dependency Injection. It basically means to separate dependent data or algorithm and merge them at a central location. In this example the objective is to merge the tax data like income and tax rate with the algorithm how to tax.</p><pre>class Taxation<br>  attr_reader :taxable<br>  <br>  def initialize(taxable:)<br>    @taxable = taxable<br>  end<br>  <br>  def charge<br>    tax = taxable.income.to_d * taxable.tax_rate.to_d / 100.to_d<br>    # algorithm for collecting the tax<br>  end<br>end</pre><p>The dependency (taxable entity) is injected into the taxation object. Charging the tax works with any kind of taxable object providing the expected interfaces. Even aliens could be taxed as long as they have an income and a tax rate.</p><pre>person = Person.new(income: 5_000)<br>Taxation.new(taxable: person).charge<br># person is charged 1.000<br>company = Company.new(income: 100_000)<br>Taxation.new(taxable: company).charge<br># company is charged 20.000</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83b35b05d506" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Aliasing the Ruby & Rails way]]></title>
            <link>https://christianrolle.medium.com/aliasing-the-ruby-rails-way-c75a891a82bc?source=rss-5ec07ebf06a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/c75a891a82bc</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Christian Rolle]]></dc:creator>
            <pubDate>Wed, 15 Feb 2023 19:51:42 GMT</pubDate>
            <atom:updated>2023-02-18T05:02:04.879Z</atom:updated>
            <content:encoded><![CDATA[<p>A method alias can make sense or even can be necessary for various reasons.</p><ol><li>Satisfy TemplateMethod requirements using duck typing</li><li>Increasing a methods meaning/ readability</li><li>Extend a methods behavior (in the sense of aspect oriented programming)</li></ol><p>Since there are 2 ways of aliasing in Rails I’m going to disclose both and when to use which one.<br>Let’s start with the alias_method.</p><h3>Pure Ruby aliasing</h3><p>Consider a PORO like Article.</p><pre>class Article<br>  attr_reader :name<br>  alias_method :topic, :name<br>  <br>  def initialize(name)<br>    @name = name<br>  end<br>end</pre><p>and use it accordingly:</p><pre>article = Article.new(&#39;Ruby&#39;)<br>article.name<br># =&gt; &quot;Ruby&quot;<br>article.topic<br># =&gt; &quot;Ruby&quot;</pre><p>Simple as that.<br>However with Rails models we have to respect how Rails model work.</p><h3>Rails model aliasing</h3><p>First we try to apply the Ruby aliasing to Rails models as well.</p><pre>rails g model Article name:string &amp;&amp; rake db:migrate</pre><p>and the corresponding model with an alias topic on the attribute method name:</p><pre>class Article &lt; ApplicationRecord<br>  alias_method :topic, :name<br>end</pre><p>Unfortunately that does not work.</p><pre>Article.new<br># =&gt; NameError: undefined method `name&#39; for class &#39;Article&#39;</pre><p>The reason is how Rails model work internally. Ruby on Rails creates attribute accessors dynamically and therefore helps itself with meta programming and the <a href="https://ruby-doc.org/core-3.1.1/BasicObject.html#method-i-method_missing">method_missing</a> approach. But at the time of class instantiation the accessors are not created yet.</p><p>However when alias_method tries to make a copy of the original method, it consequentially fails withNameError.</p><p>The solution is <a href="https://apidock.com/rails/v6.1.3.1/Module/alias_attribute">Module#alias_attribute</a>. Ruby on Rails extended Module and enables aliasing in <a href="https://apidock.com/rails/ActiveRecord">ActiveRecord</a>. The principle is simple. A new method is created by meta programming, which internally calls the original method.</p><p>Thus the model has to look like:</p><pre>class Article &lt; ApplicationRecord<br>  alias_attribute :topic, :name<br>end</pre><p>with:</p><pre>article = Article.new(name: &#39;Ruby&#39;)<br>article.name<br># =&gt; &quot;Ruby&quot;<br>article.label<br># =&gt; &quot;Ruby&quot;</pre><p>Furthermore alias_attribute generates the appropriate setter methods.</p><pre>article = Article.new(name: &#39;Ruby&#39;)<br>article.topic = &#39;RubyOnRails&#39;<br>article.name<br># =&gt; &quot;RubyOnRails&quot;<br>article.topic<br># =&gt; &quot;RubyOnRails&quot;</pre><p>Since the alias_attribute is glued toModule, it works for each class even POROs:</p><pre>class Book<br>  attr_accessor :name<br>  alias_attribute :title, :name<br>  <br>  def initialize(name:)<br>    @name = name<br>  end<br>end</pre><p>With that Book provides two more methods label and label=</p><pre>book = Book.new name: &#39;Ruby&#39;<br>book.title<br># =&gt; &quot;Ruby&quot;<br>book.title = &#39;RubyOnRails&#39;<br>book.title<br># =&gt; &quot;RubyOnRails&quot;<br>book.name<br># =&gt; &quot;RubyOnRails&quot;</pre><h4>Resources</h4><ul><li><a href="https://ruby-doc.org/core-3.1.1">Ruby core doc v3.1</a></li><li><a href="https://apidock.com/rails/v6.1.3.1/Module">Rails Module ApiDock v6.1</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c75a891a82bc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple cURL requests]]></title>
            <link>https://christianrolle.medium.com/simple-curl-requests-983f0d163e31?source=rss-5ec07ebf06a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/983f0d163e31</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[basics]]></category>
            <category><![CDATA[request]]></category>
            <category><![CDATA[curl]]></category>
            <dc:creator><![CDATA[Christian Rolle]]></dc:creator>
            <pubDate>Sun, 29 Jan 2023 05:13:22 GMT</pubDate>
            <atom:updated>2023-02-15T20:55:03.447Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lZc35ZpNIexcxKnCcKdzLQ.png" /></figure><p>Whenever it comes to parsing some web content, <a href="https://curl.se/docs/manpage.html">cURL</a> is a sweet tool for that. It is as simple as</p><pre>curl -X GET https://christianrolle.medium.com</pre><p>which in this case returns some HTML.</p><p>However there is more to it. You will see when messing around with cURL the <a href="https://httpbin.org/">HTTPbin is a sweet public API</a>.</p><h3>cURL request header</h3><p>The request header can be set with option -H</p><pre>curl -H &quot;Accept: application/json&quot; &quot;https://httpbin.org/anything?search=Test&quot;</pre><p>And since that API is able to respond to a JSON request its response looks like</p><pre>{<br>  &quot;args&quot;: {<br>    &quot;search&quot;: &quot;Test&quot;<br>  }, <br>  &quot;data&quot;: &quot;&quot;, <br>  &quot;files&quot;: {}, <br>  &quot;form&quot;: {}, <br>  &quot;headers&quot;: {<br>    &quot;Accept&quot;: &quot;application/json&quot;, <br>    &quot;Host&quot;: &quot;httpbin.org&quot;, <br>    &quot;User-Agent&quot;: &quot;curl/7.81.0&quot;, <br>    &quot;X-Amzn-Trace-Id&quot;: &quot;Root=1-63daca6a-3c9f6f944b65a5877c455039&quot;<br>  }, <br>  &quot;json&quot;: null, <br>  &quot;method&quot;: &quot;GET&quot;, <br>  &quot;origin&quot;: &quot;142.250.181.238&quot;, <br>  &quot;url&quot;: &quot;https://httpbin.org/anything?search=Test&quot;<br>}</pre><p>With a custom user agent using option -A</p><pre>curl -A &quot;Special agent Version 0.0.7&quot; &quot;https://httpbin.org/anything&quot;</pre><p>And claiming a referer with option -e</p><pre>curl -e &quot;http://google.com&quot; &quot;https://httpbin.org/anything&quot;</pre><h3>cURL response header</h3><p>Diving into the response header with option -I</p><pre><br>curl -I &quot;https://httpbin.org/anything&quot;</pre><p>provides some insights</p><pre>HTTP/2 200 <br>date: Wed, 01 Feb 2023 20:58:58 GMT<br>content-type: application/json<br>content-length: 344<br>server: gunicorn/19.9.0<br>access-control-allow-origin: *<br>access-control-allow-credentials: true</pre><h3>cURL request parameters</h3><p>Sending additional request parameters requires option -d</p><pre>curl -X POST -d &quot;search=Test&quot; -d &quot;size=10&quot; &quot;https://httpbin.org/anything&quot;</pre><p>Or execute a multipart upload with option -F</p><pre>curl -X POST -F image=@test.png &quot;https://httpbin.org/anything&quot;</pre><h3>cURL cookie handling</h3><p>Downloading a cookie and saving it locally with the cookie-jar option -c</p><pre>curl -c cookie.txt https://medium.com</pre><p>And it seems to be alive for a long time</p><pre>cat cookie.txt<br><br>#HttpOnly_.medium.com TRUE / FALSE 1706820414  __cfruid 8a6d93effb0f5c6bf6a5702ec13c1c98058e7aa2-1675282510</pre><p>We could manipulate the cookie and set it with option -b</p><pre>curl -b cookie.txt &quot;https://medium.com&quot;</pre><h4>Resources</h4><ul><li><a href="https://curl.se/docs/manpage.html">cURL man page</a></li><li><a href="https://httpbin.org/">HTTPbin</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=983f0d163e31" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>