Sha256: fac9c9a21023fe810076bdde7933e9ee1e288ce60401edcb890ce47e2284b0df

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require 'renum/enumerated_value'

module Renum
  module EnumeratedValueTypeFactory
    class << self
      def create nest, type_name, values, &block
        klass = create_class nest, type_name
        create_values klass, values, &block
      end
    
      def create_class nest, type_name
        klass = Class.new EnumeratedValue
        nest.const_set(type_name, klass)
        klass
      end
    
      def create_values klass, values, &block
        setup_for_definition_in_block(klass) if values == :defined_in_block
        klass.class_eval &block if block_given?
        if values == :defined_in_block
          klass.block_defined_values.each do |value_name, init_args, instance_block|
            value = klass.new(value_name)
            klass.const_set(value_name, value)
            value.instance_eval &instance_block if instance_block
            value.init *init_args if value.respond_to? :init
          end
          teardown_from_definition_in_block(klass)
        else
          values.each do |name|
            klass.const_set(name, klass.new(name))
          end
        end
      end
    
      def setup_for_definition_in_block klass
        klass.class_eval do
          def self.block_defined_values
            @block_defined_values ||= []
          end

          def self.method_missing value_name, *init_args, &instance_block
            block_defined_values << [value_name, init_args, instance_block]
          end
        end
      end
    
      def teardown_from_definition_in_block klass
        class << klass
          remove_method :block_defined_values
          remove_method :method_missing
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
duelinmarkers-renum-1.2.0 lib/renum/enumerated_value_type_factory.rb
renum-1.2.0 lib/renum/enumerated_value_type_factory.rb