When using ActiveHash with enum types, we often find ourselves writing repetitive predicate methods for each enum value. For example:
class PublicStatus < ActiveHash::Base
include ActiveHash::Enum
include ActiveHash::Associations
self.data = [
{ id: 1, name: "Publish", type: "published" },
{ id: 2, name: "Draft", type: "drafted" },
{ id: 3, name: "Archive", type: "archived" }
]
enum_accessor :type
def published?
id == PublicStatus::PUBLISHED.id
end
def drafted?
id == PublicStatus::DRAFTED.id
end
def archived?
id == PublicStatus::ARCHIVED.id
end
end
These methods are commonly used but require manual implementation for each enum value. It would be beneficial if ActiveHash could automatically generate these status check methods based on the defined enum values.
Proposed Solution
Enhance ActiveHash to automatically generate status check methods for enum values. This could be implemented as follows:
When enum_accessor :type is called, automatically generate methods for each enum value.
The generated methods would follow the pattern {enum_value}? and return a boolean indicating whether the current object matches that enum value.
Example Usage
With this enhancement, the above code could be simplified to:
class PublicStatus < ActiveHash::Base
include ActiveHash::Enum
include ActiveHash::Associations
self.data = [
{ id: 1, name: "Publish", type: "published" },
{ id: 2, name: "Draft", type: "drafted" },
{ id: 3, name: "Archive", type: "archived" }
]
enum_accessor :type
# No need to manually define published?, drafted?, archived? methods
end
status = PublicStatus.find(1)
status.published? => true
status.drafted? => false
status.archived? => false
Description
When using ActiveHash with enum types, we often find ourselves writing repetitive predicate methods for each enum value. For example:
These methods are commonly used but require manual implementation for each enum value. It would be beneficial if ActiveHash could automatically generate these status check methods based on the defined enum values.
Proposed Solution
Enhance ActiveHash to automatically generate status check methods for enum values. This could be implemented as follows:
When enum_accessor :type is called, automatically generate methods for each enum value. The generated methods would follow the pattern {enum_value}? and return a boolean indicating whether the current object matches that enum value.
Example Usage
With this enhancement, the above code could be simplified to: