ncbo / goo

Graph Oriented Objects (GOO) for Ruby. A RDF/SPARQL based ORM.
http://ncbo.github.io/goo/
Other
15 stars 6 forks source link

Add DSL for enums #69

Open palexander opened 11 years ago

palexander commented 11 years ago

Handle Java-style enum objects that allow for a definition of allowed values that get created automatically on first access with a static flag to avoid recreating the values again.

class UserRole
  enum_values :administrator, :librarian, :developer
end

For retrieving, two possible syntax options:

u = UserRole.new(role: "ADMINISTRATOR")
=> Exception "Enums cannot be instantiated, only found)
u = UserRole.find("ADMINISTRATOR")
=> <UserRole: {role: "ADMINISTRATOR"}>

Or get rid of new (by making initialize private) and find and use one method per enum value:

UserRole.default
UserRole.administrator
msalvadores commented 11 years ago

In

UserRole.default

How do we know which enum instance is default ? do we need an option for that like:

class UserRole
  enum_values :administrator, :librarian, :developer, :default => :librarian
end
palexander commented 11 years ago

Yes, UserRole.default is just a hard-coded method with the default value, so if we want that functionality in the DSL then it would make sense to have an option available