class FunctionalLightService::EnumBuilder::DataType

Public Class Methods

create(parent, args) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/functional-light-service/functional/enum.rb, line 69
def self.create(parent, args)
  # rubocop:disable Style/AccessModifierDeclarations
  if args.include? :value
    raise ArgumentError, "#{args} may not contain the reserved name :value"
  end

  dt = Class.new(parent)

  dt.instance_eval do
    public_class_method :new
    include AnyEnum
    define_method(:args) { args }

    define_method(:parent) { parent }
    private :parent
  end

  case args.count
  when 0
    dt.instance_eval do
      include Nullary
      private :value
    end
  when 1
    dt.instance_eval do
      define_method(args[0].to_sym) { value }
    end
  else
    dt.instance_eval do
      include Binary

      args.each do |m|
        define_method(m) do
          @value[m]
        end
      end
    end
  end

  dt
  # rubocop:enable Style/AccessModifierDeclarations
end