Comments (5)
The problem with using anonymous classes (ie Class.new) is that there isn't any way to know the name of the class before it is created, and therefor the inherited
hook doesn't have the information it needs to properly create the enum member. Have you considered just using eval to do it?
[0.13, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30].each do |num|
num_str = num.to_s.sub(/\./, '_')
eval "ContaminantDepth::I#{num_str} < ContaminantDepth; end"
end
from classy_enum.
Hmm, interesting. That makes sense, thanks.
I tried to avoid eval, since using a string interpolation for this sort of thing inherently seems ugly.
I'm pretty new to Ruby's meta-programming features; are you aware of any way I could do the equivalent to that eval, but using the cleaner block syntax, as I had done before?
from classy_enum.
I'm not sure of a way to do it off hand, I would try Stack Overflow and see if anyone has a suggestion.
from classy_enum.
So just FYI, the solution I went with was defining the class in an eval, and then using class_eval to nicely define the methods in a block.
Like so:
[0.13, 0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30].each do |num|
num_str = num.to_s.sub(/\./, '_')
class_name = "In#{num_str}"
# Must define the class non-anonymously, so superclass' 'inherited' hook gets class name
eval "class ContaminantDepthIn::#{class_name} < ContaminantDepthIn; end"
ContaminantDepthIn.const_get(class_name).class_eval do
define_method :value do
num
end
end
end
Feels a little dirty, but it's better than just one long eval.
from classy_enum.
Awesome, glad you were able to come up with something you're happy with!
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.