Sha256: ad5bd51330e8b47ed471607926cdc8e9864f93e068cbd3aa1d5bdcef69aafe82

Contents?: true

Size: 1.83 KB

Versions: 15

Compression:

Stored size: 1.83 KB

Contents

require 'degu/renum/enumerated_value'

module Degu
  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
            begin
              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
            ensure
              teardown_from_definition_in_block(klass)
            end
          else
            values.each do |name|
              klass.const_set(name, klass.new(name))
            end
          end
          klass.values.freeze
        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
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
degu-0.9.0 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.8.1 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.8.0 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.7.0 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.6.0 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.5.0 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.4.0 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.3.0 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.2.0 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.1.3 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.1.2 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.1.1 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.1.0 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.0.5 lib/degu/renum/enumerated_value_type_factory.rb
degu-0.0.4 lib/degu/renum/enumerated_value_type_factory.rb