Comments (4)
find
was an alias for build
in pre 3.0, but when I was going through and cleaning things up for 3.0, I realized that build
was doing significantly more than it did when I first introduced it. It was no longer just finding the enum type from within an hash, it was also setting some additional properties on it that came from the ActiveRecord classy_enum_attr
call, such as the owner reference and how to serialize it. The TypeError message it returns is also not consistent with either Ruby's find which returns nil, or ActiveRecord's find
which raises a ActiveRecord::RecordNotFound
. Consistency was my biggest goal for the 3.0 refactor, and with this being one of those inconsistencies, I decided to remove it.
Having said that, I'm definitely not opposed to reintroducing find
in another form for a 3.1 release. If implemented properly, I think it would make a lot of sense. I would probably write it as something like the following where all it's doing is just instantiating a new enum or returning nil if it cannot be found:
def find(value)
return unless all.map(&:to_s).include? value.to_s
"#{base_class}::#{value.to_s.camelize}".constantize.new
end
from classy_enum.
Ah, okay. That makes a lot of sense. I'm less peeved now. :)
I (and my team) would love it if you brought back the simpler find
in the future.
from classy_enum.
So it looks like the future is more like a couple days :) It turns out I had to add I18n translation support for a work project today, and I'll be releasing a new version (3.1) either this weekend or early next week. Part of that upgrade is going to include find
and possibly everything that comes with the Enumerable module. I started a branch but want to sit on it for a little while before I commit to it for 3.1. Here's a preview of the functionality, but I may change it slightly before the release.
from classy_enum.
Just released v3.1 which brings back find, and includes everything else that comes with enumerable. You can pass it a string or symbol and it returns an instance if it finds it, otherwise it returns nil. It also behaves like Ruby's find where you can pass it a block and it returns the first item that resolves to true.
Priority.find(:high) # => Priority::High.new
Priority.find('high') # => Priority::High.new
Priority.find {|e| e.to_sym == :high } # => Priority::High.new
from classy_enum.
Related Issues (20)
- How to update many records at once? HOT 2
- Rails 4.1 enums HOT 1
- Eager loading with inludes() returns String, not Enum HOT 6
- Wierd failure when using with jruby HOT 6
- Enum by default are empty in DB? HOT 4
- Drop support for Ruby 1.8.7 HOT 1
- Drop support for Rails 3.x HOT 1
- Clean up specs
- Clean up how code is injected into Active Record HOT 1
- Remove Arel hacks for Active Record scopes HOT 1
- Problem with default value HOT 7
- Update the readme to include a step about autoload_paths HOT 6
- Uninitialized Constant when Loading Classy Enum in 4.0.0 HOT 3
- Undocumented breaking change (Cannot visit <Enum>) HOT 3
- Question: Using classy enum to query with AR? HOT 6
- Classy_enum with actual enum fields? HOT 1
- Enum inside other Enum HOT 5
- Add support for validation :on and :if options HOT 5
- Rails 5.x update HOT 2
- ClassyEnum build method performance
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from classy_enum.