Sha256: a8f2036c944d0744fe061ea5603c602ba3032aa2db39bf92b101dc0273a9aa16

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require "typical/type"

module Typical
  module DSL
    def self.define_scalar_type(mod, name = nil)
      name ||= mod.name
      define_method("#{name}!") { Type.new(mod) }
      define_method("#{name}?") { Type.new(mod) | null }
    end

    BUILTIN_SCALAR_TYPES = [
      Integer,
      Float,
      Object,
      Range,
      Regexp,
      String,
      Symbol,
      Time,
    ].freeze

    BUILTIN_SCALAR_TYPES.each do |klass|
      define_scalar_type(klass)
    end

    def null
      Type.new(NilClass)
    end

    def Hash!(types = {})
      Type::Hash.new(types)
    end

    def Hash?(types = {})
      Hash!(types) | null
    end

    def Set!(*types)
      Type::Set.new(*types)
    end

    # TODO: It actually appears that mongo/mongoid does not return `null` for set fields.
    def Set?(*types)
      Set!(*types) | null
    end

    def Array!(*types)
      Type::Array.new(types)
    end

    def Array?(*types)
      Array!(*types) | null
    end

    def Reference!(reference)
      Type::Reference.new(reference)
    end

    def Reference?(reference)
      Reference!(reference) | null
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
typical-0.1.0 lib/typical/dsl.rb