Sha256: d6e2046d732cb8c04a64c0f3585869c492be440b3bf3040838686a31829528ec
Contents?: true
Size: 1.77 KB
Versions: 5
Compression:
Stored size: 1.77 KB
Contents
module ClassyEnum module ClassMethods # Build a new ClassyEnum child instance # # ==== Example # # Create an Enum with some elements # class Priority < ClassyEnum::Base # enum_classes :low, :medium, :high # end # # Priority.build(:low) # => PriorityLow.new def build(option) return option if option.blank? return TypeError.new("Valid #{self} options are #{self.valid_options}") unless self::OPTIONS.include? option.to_sym Object.const_get("#{self}#{option.to_s.camelize}").new end alias :find :build # Returns an array of all instantiated enums # # ==== Example # # Create an Enum with some elements # class Priority < ClassyEnum::Base # enum_classes :low, :medium, :high # end # # Priority.all # => [PriorityLow.new, PriorityMedium.new, PriorityHigh.new] def all self::OPTIONS.map {|e| build(e) } end # Returns a 2D array for Rails select helper options. # Also used internally for Formtastic support # # ==== Example # # Create an Enum with some elements # class Priority < ClassyEnum::Base # enum_classes :low, :really_high # end # # Priority.select_options # => [["Low", "low"], ["Really High", "really_high"]] def select_options all.map {|e| [e.name, e.to_s] } end # Returns a comma separated list of valid enum options. # Also used internally for ActiveRecord model validation error messages # # ==== Example # # Create an Enum with some elements # class Priority < ClassyEnum::Base # enum_classes :low, :medium, :high # end # # Priority.valid_options # => "low, medium, high" def valid_options self::OPTIONS.map(&:to_s).join(', ') end end end
Version data entries
5 entries across 5 versions & 1 rubygems