Sha256: 9f5f8c057081f042a29f6ea0b1ddfcf960ca027fd7d4a42990d539c973533e03

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

Encode
=========

Encode makes it easy to go from codes to meaningful strings.

Example
=======

Suppose you have a task in one of three possible states: pending, active, finished.  When you store tasks, you'd like to have the state represented as P for pending, A for active, and F, for finished.  However, you don't want to worry about the details of turning a state into a specific character.

t = Task.new('feed the cat')

# By default 
t.status = Status::Pending
t.status         # => 'P'
t.status.decode  # => 'Pending'

# You'd probably encapsulate this in a method though wouldn't you?
t.finish!

# How should that be implemented though?
@status = 'f'
@status = 'finished'
@status = Task::Status::Finished

However, you want to store the state as a single character behind the scenes without having to define a bunch of custom getters or setters. 

class Task
	code :status do
	  Status::Pending = "P" 
	  Status::Active = "A"
	  Status::Finished = "F"
	end
end

To Do
=====

[ ] Auto-mixin on init
[ ] Make a proper gem
[ ] Choose a better name
[ ] Use shoulda or rspec (maybe)

Copyright (c) 2010 Jon Morton, released under the MIT license

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
encoder-0.0.1 README