The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Renum goes one-oh with a nice new syntax for an important new feature

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
john hume

Posts: 82
Nickname: duelnmrkrs
Registered: Oct, 2005

John Hume is a developer and consultant with ThoughtWorks.
Renum goes one-oh with a nice new syntax for an important new feature Posted: Jan 25, 2008 12:01 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by john hume.
Original Post: Renum goes one-oh with a nice new syntax for an important new feature
Feed Title: El Humidor
Feed URL: http://feeds.feedburner.com/ElHumidor
Feed Description: John D. Hume on Ruby and development with agile teams.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by john hume
Latest Posts From El Humidor

Advertisement

Last week while watching Project Runway, I realized there was a cool way to provide a feature Renum was sorely lacking.

The basic enum supported by Renum like the below is nice, but not very interesting.

enum :Status, %w[ NOT_STARTED IN_PROGRESS COMPLETE ]

You get the collection of values and the sorting, which might be worthwhile for you, but there's not much to recommend it over strings or symbols. You could also put behavior in the values like this.

enum :Color, [ :RED, :GREEN, :BLUE ] do
  def abbr
    name[0..0]
  end
end

But with just the name to work with, the behavior couldn't be very interesting unless you resorted to defining singleton methods on the values, which is fine but not pretty. (It sort of defeats the purpose of Renum: to provide a terse and readable API for creating object constants.)

What would really make enum behavior interesting is if you could associate some extra data with each value. But since we want the instances created automagically, we can't just let you provide a custom constructor.

Back in the post where I first tried to create a good API for this, I experimented with a const_missing hack to create the values. Unfortunately I couldn't get const_missing to be called on the enum class that was being created: it would be called on whatever module the code was in the scope of—Object at the top level—which would have meant redefining const_missing on the receiver of the enum method (either the module we were in or the "main" object), which I wasn't willing to do.

What it took me 6 months to realize is that (thanks to the differences between constant lookup and method sending) almost the same syntax I couldn't support with const_missing can be supported with method_missing! Because Renum already has the newly created enumerated value class class_eval the associated block, it would receive the method_missing calls, so I wouldn't have to mess with the methods on any pre-existing object. Method calls would also allow providing additional arguments that could be passed along to the values so that you can define more intersting behavior.

That's probably more detail than anyone wanted. The upshot of all that is that Renum 1.0 allows you to do this:

enum :Size do
  Smaller("Really really tiny", 0.6)
  Medium("Sort of in the middle", 1)
  Larger("Quite big", 1.5)

  attr_reader :description

  def init description, factor
    @description, @factor = description, factor
  end
  
  def adjust original
    original * @factor
  end
end

For more details, see the spec.

Read: Renum goes one-oh with a nice new syntax for an important new feature

Topic: Feed Maintenance Previous Topic   Next Topic Topic: Indentation as Block Structure - HAML instead of RHTML

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use