Sha256: 460091367a2def039cd258ff3890848addfabb59aa8cea7d3a32a7f38aa7c1fe

Contents?: true

Size: 885 Bytes

Versions: 2

Compression:

Stored size: 885 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.to_s) }
      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)
      to_s == attribute
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
classy_enum-3.0.1 lib/classy_enum/predicate.rb
classy_enum-3.0.0 lib/classy_enum/predicate.rb