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

Version Path
classy_enum-3.5.0 lib/classy_enum/predicate.rb
classy_enum-3.5.0.beta2 lib/classy_enum/predicate.rb
classy_enum-3.5.0.beta lib/classy_enum/predicate.rb
classy_enum-3.4.0 lib/classy_enum/predicate.rb
classy_enum-3.4.0.beta1 lib/classy_enum/predicate.rb
classy_enum-3.3.2 lib/classy_enum/predicate.rb
classy_enum-3.3.1 lib/classy_enum/predicate.rb
classy_enum-3.3.0 lib/classy_enum/predicate.rb
classy_enum-3.2.1 lib/classy_enum/predicate.rb
classy_enum-3.2.0 lib/classy_enum/predicate.rb
classy_enum-3.1.3 lib/classy_enum/predicate.rb
classy_enum-3.1.2 lib/classy_enum/predicate.rb
classy_enum-3.1.1 lib/classy_enum/predicate.rb
classy_enum-3.1.0 lib/classy_enum/predicate.rb