Sha256: 548ed1d4c2cfeece266db515d26674db5b7572871ae60dab52aa629c829088bc

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

h1. renum

h1. → 'renum'


h2. What

Renum provides a readable but terse enum facility for Ruby.


h2. Installing

<pre syntax="ruby">sudo gem install renum</pre>


h2. Demonstration of usage

Renum allows you to do things like this:

<pre syntax="ruby">enum :Status, [ :NOT_STARTED, :IN_PROGRESS, :COMPLETE ]

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

module MyNamespace
  enum :FooValue, [ :Bar, :Baz, :Bat ]
end</pre>

Giving you something that satisfies this spec:

<pre syntax="ruby">describe "enum" do
  
  it "creates a class for the value type" do
    Status.class.should == Class
  end
  
  it "makes each value an instance of the value type" do
    Status::NOT_STARTED.class.should == Status
  end
  
  it "exposes array of values" do
    Status.values.should == [Status::NOT_STARTED, Status::IN_PROGRESS, Status::COMPLETE]
  end
  
  it "enumerates over values" do
    Status.map {|s| s.name}.should == %w[NOT_STARTED IN_PROGRESS COMPLETE]
  end
  
  it "indexes values" do
    Status[2].should == Status::COMPLETE
    Color[0].should == Color::RED
  end
  
  it "provides index lookup on values" do
    Status::IN_PROGRESS.index.should == 1
    Color::GREEN.index.should == 1
  end
  
  it "allows an associated block to define instance methods" do
    Color::RED.abbr.should == "R"
  end
  
  it "provides a reasonable to_s for values" do
    Status::NOT_STARTED.to_s.should == "Status::NOT_STARTED"
  end
  
  it "makes values comparable" do
    Color::RED.should < Color::GREEN
  end
  
  it "allows enums to be nested in other modules or classes" do
    MyNamespace::FooValue::Bar.class.should == MyNamespace::FooValue
  end
  
end</pre>

h2. License

This code is free to use under the terms of the MIT license. 

h2. Contact

Renum was created by John D. Hume. Comments are welcome. Send an email to duelin dot markers at gmail or "contact me via my blog":http://elhumidor.blogspot.com/.

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
renum-0.0.3 website/index.txt
renum-0.1.0 website/index.txt