Sha256: b48186fc92a00061b36595f1a5e925434f9869f2b5434ae50a937e9a0671e1f2
Contents?: true
Size: 880 Bytes
Versions: 14
Compression:
Stored size: 880 Bytes
Contents
module ClassyEnum module Predicate # Define attribute methods like two? def self.define_predicate_method(klass, enum) klass.base_class.class_eval do define_method "#{enum}?", lambda { attribute?(enum) } end end protected # Determine if the enum attribute is a particular member. # # ==== Example # # Create an Enum with some elements # class Breed < ClassyEnum::Base # end # # class Breed::GoldenRetriever < Breed; end # class Breed::Snoop < Breed; end # # # Create an ActiveRecord class using the Breed enum # class Dog < ActiveRecord::Base # classy_enum_attr :breed # end # # @dog = Dog.new(:breed => :snoop) # @dog.breed.snoop? # => true # @dog.breed.golden_retriever? # => false def attribute?(attribute) self == attribute end end end
Version data entries
14 entries across 14 versions & 1 rubygems