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

Question methods for enum accessors #327

Closed
rtalexk opened this issue Dec 20, 2024 · 1 comment
Closed

Question methods for enum accessors #327

rtalexk opened this issue Dec 20, 2024 · 1 comment

Comments

@rtalexk
Copy link

rtalexk commented Dec 20, 2024

After using this gem for the last two years my team and I ended up adding a lot of methods to ask whether an object is a given record, ie:

class Country < ActiveHash::Base
  include ActiveHash::Enum
  self.data = [
      {:id => 1, :name => "US", :capital => "Washington, DC"},
      {:id => 2, :name => "Canada", :capital => "Ottawa"},
      {:id => 3, :name => "Mexico", :capital => "Mexico City"}
  ]
  enum_accessor :name

  # a method like this for each record
  def us?
    name == "US"
  end
end

Then, somewhere else:

# Assuming a user has an association to a Country
>> user.country.us?
=> true

This could be added programmatically using meta-programming, but I think it'd be a nice feature for the gem to do it either automatically by the enum_accessor or using a different macro for this purpose.

@rtalexk
Copy link
Author

rtalexk commented Dec 20, 2024

Nvm, I just saw this PR #321

@rtalexk rtalexk closed this as completed Dec 20, 2024
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

1 participant