Sha256: 3d268e4ec7d5aff06d3faa98d39ee638f9e4f437801e65d72196289515eadbe8

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8

module EnumField
  # Add enum methods to class
  # Usage:
  # class Role
  #   include EnumField::DefineEnum
  #   ...
  # end
  #
  module DefineEnum
    def self.included(base)
      base.send :extend, ClassMethods
      base.send :include, InstanceMethods
    end

    module ClassMethods
      def define_enum(options = {}, &block)
        @enum_builder ||= EnumField::Builder.new(self, options)
        @enum_builder.instance_exec(&block)

        EnumField::Builder::METHODS.each do |method|
          define_singleton_method method do |*args, &method_block|
            @enum_builder.send(method, *args, &method_block)
          end
        end

        @enum_builder.members.keys.each do |method|
          define_singleton_method method do
            @enum_builder.members[method]
          end

          define_method "#{method}?" do
            @name == method
          end
        end

        define_singleton_method '[]' do |value|
          @enum_builder[value]
        end

        @enum_builder.members.freeze
      end
    end

    module InstanceMethods
      def id
        @id
      end

      def name
        @name
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
galetahub-enum_field-0.5.0 lib/enum_field/define_enum.rb