Skip to content

Commit

Permalink
Add support for ids as symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
hackling authored and parameme committed Jun 4, 2014
1 parent bb674f0 commit 3e09cb9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 10 deletions.
7 changes: 7 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ Define using implicit id values
value :name => 'Female'
end

Define using symbol ids

class Sex < ActiveEnum::Base
value :id => :m, :name => 'Male'
value :id => :f, :name => 'Female'
end

Beware that if you change the order of values defined in an enum which don't have explicit ids, then the ids will change.
This could corrupt your data if the enum values have been stored in a model record, as they will no longer map to
the original enum.
Expand Down
27 changes: 18 additions & 9 deletions lib/active_enum/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ def to_select
# Access id or name value. Pass an id number to retrieve the name or
# a symbol or string to retrieve the matching id.
def get(index)
if index.is_a?(Fixnum)
if index.is_a?(Fixnum) || index.is_a?(Symbol)
row = store.get_by_id(index)
row[1] if row
else
value = row[1] if row
end

if (index.is_a?(String) || index.is_a?(Symbol)) && value.nil?
row = store.get_by_name(index)
row[0] if row
value = row[0] if row
end

value
end
alias_method :[], :get

Expand All @@ -71,12 +75,17 @@ def include?(value)

# Access any meta data defined for a given id or name. Returns a hash.
def meta(index)
row = if index.is_a?(Fixnum)
store.get_by_id(index)
else
store.get_by_name(index)
if index.is_a?(Fixnum) || index.is_a?(Symbol)
row = store.get_by_id(index)
value = row[2] if row
end
row[2] || {} if row

if (index.is_a?(String) || index.is_a?(Symbol)) && value.nil?
row = store.get_by_name(index)
value = row[2] if row
end

value || {}
end

private
Expand Down
54 changes: 53 additions & 1 deletion spec/active_enum/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,28 @@ class NewEnum < ActiveEnum::Base; end
end

describe ".meta" do
it 'should return meta values hash for a given index value' do
it 'should return meta values hash for a given integer index value' do
enum = define_enum do
value :id => 1, :name => 'Name', :description => 'extra'
end
enum.meta(1).should == {:description => 'extra'}
end

it 'should return meta values hash for a given symbol index value' do
enum = define_enum do
value :id => :one, :name => 'Name 1', :description => 'extra'
end
enum.meta(:one).should == {:description => 'extra'}
end

it 'should match on id before name and return meta values hash' do
enum = define_enum do
value :id => :one, :name => 'Name 1', :description => 'extra'
value :id => :name_1, :name => 'Name 2', :description => 'extra2'
end
enum.meta(:name_1).should == {:description => 'extra2'}
end

it 'should return empty hash for index with no meta defined' do
enum = define_enum do
value :id => 1, :name => 'Name'
Expand Down Expand Up @@ -182,6 +197,43 @@ class NewEnum < ActiveEnum::Base; end
enum[:name_1].should == 1
end
end

context 'symbol ids' do
let(:enum) {
define_enum do
value :id => :one, :name => 'Name 1'
value :id => :two, :name => 'Name 2'
end
}

it 'should return name when given an id' do
enum[:one].should == 'Name 1'
end

it 'should return id when given a name' do
enum['Name 1'].should == :one
end

it 'should return id when given a symbol of the name' do
enum[:Name_1].should == :one
enum[:name_1].should == :one
end

context 'ids and names are similar' do
let(:enum) {
define_enum do
value :id => :one, :name => 'two'
value :id => :two, :name => 'three'
end
}

it 'should match on id before name' do
enum[:one].should == 'two'
enum[:two].should == 'three'
enum[:three].should == :two
end
end
end
end

describe ".include?" do
Expand Down

0 comments on commit 3e09cb9

Please sign in to comment.