Commits

certainty  committed 89cd9e6

added post about dynamic attr

  • Participants
  • Parent commits 2234fad

Comments (0)

Files changed (2)

File _posts/2012-10-25-dynamic_attributes.md

+---
+layout: post
+title: Dynamic Attributes
+tagline: syntactic affairs
+tags: [ruby,dynamic_attr,syntactic_affairs]
+---
+
+{% include setup %}
+
+
+This short article describes dynamic attributes for ruby. Lately I've had todo with code that 
+showed a common pattern which I named <strong>dynamic attributes</strong>.
+
+Did you ever come accross a situation where you had an instance-variable and wanted 
+a way to temporarily change the value of that variable during the execution of a block and have then the original 
+value restored?
+
+That is what dynamic attributes are for.
+
+But code tells more than thousand words:
+
+{% highlight ruby %}
+class MyLogger
+  dynamic_attr :context
+  
+  def log(msg)
+    puts "#{context} #{msg}"
+  end
+end
+
+logger = MyLogger.new
+logger.log "Hello World"
+
+logger.with_context("Ruby") do
+  logger.log("is a very flexible language")
+end
+
+logger.with_context("Boot") do
+  logger.log("starting up")
+  
+  logger.with_context("Network") do
+    logger.log("starting up")
+  end
+  
+  logger.log("finishd")
+end
+{% endhighlight %}
+
+
+I guess there is not much that I need to say about it. It's just a piece of boilerplate that should
+really not have to be implemented over and over again.
+
+For each <strong>dynamic_attr</strong> you get:
+
+* attribute
+* attribute=
+* with_*attribute*
+
+
+Without further ado, here is the implementation that makes the above code possible:
+
+{%highlight ruby%}
+class Module
+  def dynamic_attr(*names)
+    attr_accessor *names
+    names.each do |name|
+      define_method("with_#{name}") do |new_value,&blk|
+        begin
+          old_value = __send__(name)
+          __send__("#{name}=",new_value)
+          blk[self]
+        ensure
+          __send__("#{name}=",old_value) if old_value
+        end
+      end
+    end
+  end
+end
+{%endhighlight%}
  
 Hi! 
 
-I'm David Krentzlin, a developer from Germany. In my dayjob I use mainly Ruby to implement all kinds of projects ranging from Web-Applications, Services, Daemons to various tools that enable our administrators to get their work done.
+I'm David Krentzlin, a programmer from Germany.
+
+For my dayjob I use mostly ruby to implement all kinds of applications from command-line applications, daemons, services to web-applications.
 
 In my spare time I'm constantly searching for ways to expand my knowledge about various parts of computer science.
 
-I focus on functional programing languages and as I'm a schemer at heart, I very much enjoy hacking in and on <em><a href='http://call-cc.org'>chicken scheme</a></em>. Lately I found my way to haskell which is a very interesting adventure so far.
+I focus on functional programing languages and as I'm a schemer at heart, I very much enjoy hacking in and on <em><a href='http://call-cc.org'>chicken scheme</a></em>. 
+
+Lately I found my way to haskell which is a very interesting adventure so far.
 
 <strong>I hope you enjoy reading ....</strong>
 
 
 #### IRC
 
-You can find me on freednode where I go by the nick <em>certainty</em>. I&#8217;m mostly present in the following channels:
+You can find me on freednode where I go by the nick <strong>certainty</strong>. I'm mostly present in the following channels:
 <ul>
 <li>#chicken</li>
 <li>#scheme</li>