Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability to write enums as strings. #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/active_enum/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ def #{attribute}(arg=nil)
# Examples:
# user.sex = 1
# user.sex = :male
# user.sex = 'Male'
#
def define_active_enum_write_method(attribute)
class_eval <<-DEF
def #{attribute}=(arg)
if arg.is_a?(Symbol)
if arg.is_a?(Symbol) || arg.is_a?(String)
super self.class.active_enum_for(:#{attribute})[arg]
else
super arg
Expand Down
10 changes: 10 additions & 0 deletions spec/active_enum/extensions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ class Accepted < ActiveEnum::Base
person.sex.should == nil
end

it 'should store id value when valid enum name' do
person.sex = 'Female'
person.sex.should == 2
end

end

context "with value as enum name" do
Expand All @@ -198,6 +203,11 @@ class Accepted < ActiveEnum::Base
person.sex.should == 'Male'
end

it 'should store value when valid enum name' do
person.sex = 'Female'
person.sex.should == 'Female'
end

it 'should return true for boolean match' do
person.sex?(:male).should be_true
end
Expand Down