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

Enum IDs Can't Be Strings #21

Closed
pvanb opened this issue Oct 26, 2012 · 6 comments
Closed

Enum IDs Can't Be Strings #21

pvanb opened this issue Oct 26, 2012 · 6 comments

Comments

@pvanb
Copy link

pvanb commented Oct 26, 2012

Looking at the code referenced in this issue, it doesn't seem possible to create an enum where the id is a string.

You can see this in an example:

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

will result in:

> Sex['m']
 => nil

Lookup by name works as expected:

> Sex['male']
 => "m"
@adzap
Copy link
Owner

adzap commented Oct 27, 2012

Yes, it is a limitation of the plugin. I use a type check to decide if the argument to in the [] is an id and you want to return a name, or a name and you want the id.

I've been thinking of moving away from the [] accessor and going with Enum.id(name) and Enum.name(id). Only issue name is defined on Module. But I can override and check for an argument and pass to super if there is no argument.

@adzap adzap closed this as completed Oct 29, 2012
@kuraga
Copy link
Contributor

kuraga commented Oct 29, 2012

@pvanb @adzap Yes, and if we set id to a String - it solves #17

@adzap
Copy link
Owner

adzap commented Oct 29, 2012

the type is used to determine what you want from the [] accessor. We can't accept string ids without breaking the current behaviour.

@kuraga
Copy link
Contributor

kuraga commented Oct 29, 2012

@adzap I know, we talked about it. I say for protocol :) Maybe you will think about new version's behavior...

@adzap
Copy link
Owner

adzap commented Oct 29, 2012

Perhaps we should explore this more. What is the use case for a string ID? Do you actually use this example in code? Is the enum being used to enumerate attributes on the model?

@kuraga
Copy link
Contributor

kuraga commented Oct 30, 2012

@adzap I used it for solving issue #17. I'll explain there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants