HomeRamblings  ⁄  ProgrammingRuby Language

Initial release of Elemental

Published: February 03, 2009 (almost 9 years ago)
Updated: over 2 years ago

I am happy to announce the initial release of Elemental 0.1.1. Elemental provides enumerated collection of elements that allow you to associate ruby symbols to arbitrary “display” values, thus allowing your code to think symbolically and unambiguously while giving you the means to easily display what end-users need to see. Additionally, symbols are associated with ordinal values, allowing easy storage/retrieval to persistent stores (databases, files, marshalling, etc) by saving the  Element#value as appropriate (String by default, Fixnum if you “persist_ordinally”). The primary aim of Elemental is to collect and abstract literals away from your code logic. There’s an old programmer’s wisdom that you should not encode your logic using literal values, especially those the end-user is exposed to. Complete details provided in README.txt http://github.com/mwlang/elemental/tree/master http://rubyforge.org/projects/elemental/ From Gem:

sudo gem install elemental

From Source:

gem install bones, rake, test-unit
git clone git://github.com/mwlang/elemental.git
cd elemental
rake gem:package
cd pkg
sudo gem install elemental

Example of Using: Here’s an example of how Elemental might be used with a Rails application. In your ~/config/initializers/elementals.rb file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
require 'elemental'

class PublicStatus
  extend Elemental

  member :unpublished,      :display => "Not Published", :default => true
  member :editor_approval,  :display => "Editorial Approval Needed"
  member :published,        :display => "Published"
  member :archived,         :display => "Archived"
end

class CommentType
  extend Elemental
  persist_ordinally

  member :all,        :display => "Anyone can post"
  member :moderated,  :display => "Moderated", :default => true
  member :closed,     :display => "Closed to new posts"
end

Create a “posts” table with this migration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class CreatePosts < ActiveRecord::Migration
  def self.up
    create_table :posts do |t|
      t.string :title
      t.text :body
      t.string :public_status
      t.integer :comment_type

      t.timestamps
    end
  end

  def self.down
    drop_table :posts
  end
end

In your ~/app/views/posts/edit.html.erb:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<h1>Editing post</h1>

<% form_for(@post) do |f| %>
  <%= f.error_messages %>

  <%= render :partial => 'form' %>

  <p>
    <%= f.submit "Update" %>
  </p>
<% end %>

<%= link_to 'Show', @post %> |
<%= link_to 'Back', posts_path %>

Similarly for ~/app/views/posts/new.html.erb

1
2
3
4
5
6
7
8
9
10
11
12
13
<h1>New post</h1>

<% form_for(@post) do |f| %>
  <%= f.error_messages %>

  <%= render :partial => 'form' %>

  <p>
    <%= f.submit "Create" %>
  </p>
<% end %>

<%= link_to 'Back', posts_path %>

An form partial that utilizes the Elemental classes above to populate a pair of drop down lists:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<p>
  <label>Title</label><br />
  <%= text_field :post, :title, :value => @post.title %>
</p>
<p>
  <label>Body</label><br />
  <%= text_area :post, :body, :value => @post.body %>
</p>

<p><label>Public Status</label><br />
  <%= select "post", "public_status",
    PublicStatus.sort.map{|a| [a.display, a.value]},
      { :include_blank => false, :selected => PublicStatus.defaults.first.value }
  %></select>
</p>

<p><label>Comment Type</label><br />
  <%= select "post", "comment_type",
    CommentType.sort.map{|a| [a.display, a.value]},
      { :include_blank => false, :selected => CommentType.defaults.first.value }
  %></select>
</p>

An example ~/app/views/posts/index.html.erb file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<h1>Listing posts</h1>

<table border=1 cellpadding=5px>
  <tr>
    <th>Post</th>
    <th>Public status</th>
    <th>Comment type</th>
    <th colspan=3>Actions</th>
  </tr>

<% for post in @posts %>
  <tr>
    <td><%= link_to post.title, post, :title => post.body %></td>
    <td><%=h PublicStatus[post.public_status].display %></td>
    <td><%=h CommentType[post.comment_type].display %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New post', new_post_path %>
comments powered by Disqus