Module: ClassyEnumAttributes
- Defined in:
- lib/classy_enum/classy_enum_attributes.rb
Instance Method Summary collapse
Instance Method Details
#classy_enum_attr(klass, method = nil) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/classy_enum/classy_enum_attributes.rb', line 3 def classy_enum_attr(klass, method=nil) method ||= klass klass = klass.to_s.camelize.constantize # Add ActiveRecord validation to ensure it won't be saved unless it's an option self.send(:validates_inclusion_of, method, :in => klass.all) self.instance_eval do # Define getter method define_method method do klass.new(super()) end # Define setter method define_method "#{method}=" do |value| super(value.to_s) end end end |