Sha256: 52a5c47ec2105d42c00921767660f22b852bc9f5cd0a9643604af5ad0485a2e4

Contents?: true

Size: 823 Bytes

Versions: 1

Compression:

Stored size: 823 Bytes

Contents

module Domain
  module Union

    def self.new(*sub_domains)
      DomainFactory.factor [ Methods, class_module(sub_domains) ]
    end

    def self.class_module(sub_domains)
      Module.new{
        define_method(:sub_domains){ sub_domains }
      }
    end

    module Methods

      # Creates a new instance of this domain
      def new(first = nil, *args)
        return first if args.empty? && self===first
        return new super(first, *args) if superclass.respond_to?(:new) && (superclass != Object)
        domain_error!(first, *args)
      end

      def predicate
        lambda{|value| sub_domains.any?{|d| d===value } }
      end

      # Checks if `value` belongs to this domain
      def ===(value)
        predicate.call(value)
      end

    end # module Methods

  end # class Union
end # module Domain

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
domain-1.0.0.rc2 lib/domain/factory/union.rb