Giter VIP home page Giter VIP logo

Comments (4)

beerlington avatar beerlington commented on August 28, 2024

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.

borski avatar borski commented on August 28, 2024

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.

beerlington avatar beerlington commented on August 28, 2024

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.

beerlington avatar beerlington commented on August 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.