module Definition::Dsl

Public Instance Methods

And(*definitions) click to toggle source

Example: And(Types::Type(Float), Types::GreaterThen(10.0))

# File lib/definition/dsl.rb, line 20
def And(*definitions) # rubocop:disable Style/MethodName
  Types::And.new(:and, *definitions)
end
Boolean() click to toggle source

Example: Boolean

# File lib/definition/dsl.rb, line 76
def Boolean # rubocop:disable Style/MethodName
  Types::Or.new(:boolean, Type(TrueClass), Type(FalseClass))
end
CoercibleType(klass) click to toggle source

Example: CoercibleType(Integer)

# File lib/definition/dsl.rb, line 38
def CoercibleType(klass) # rubocop:disable Style/MethodName
  unless Kernel.respond_to?(klass.name)
    raise ArgumentError.new("#{klass} can't be used as CoercibleType because its not "\
                            "a primitive that has a coercion function defined")
  end
  Types::Type.new(:type, klass) do |value|
    begin
      method(klass.name).call(value)
    rescue ArgumentError
      value
    end
  end
end
CoercibleValueObject(klass) click to toggle source

Example: CoercibleValueObject(ValueObjectClass)

# File lib/definition/dsl.rb, line 82
def CoercibleValueObject(klass) # rubocop:disable Style/MethodName
  Types::Or.new(:coercible_value_object,
                Definition.Type(klass), # If its of ther correct type already this will let it pass already
                And(
                  klass, # First make sure that the input could be coerced to 'klass'
                  Lambda("value_object_coercion", context: { value_object_class: klass }) do |value|
                    conform_with(klass.new(value)) # Actually coerce the input to klass
                  end
                ))
end
Each(definition) click to toggle source

Example: Each(Definition::Type(Integer))

# File lib/definition/dsl.rb, line 70
def Each(definition) # rubocop:disable Style/MethodName
  Types::Each.new(:each, definition: definition)
end
Enum(*allowed_values) click to toggle source

Example: Enum(“allowed_value1”, “allowed_value2”)

# File lib/definition/dsl.rb, line 62
def Enum(*allowed_values) # rubocop:disable Style/MethodName
  Lambda("enum", context: { allowed_values: allowed_values }) do |value|
    conform_with(value) if allowed_values.include?(value)
  end
end
Keys(&block) click to toggle source

Example: Keys do

required :name, Types::Type(String)
optional :age, Types::Type(Integer)

end

# File lib/definition/dsl.rb, line 12
def Keys(&block) # rubocop:disable Style/MethodName
  Types::Keys.new(:hash).tap do |instance|
    instance.instance_exec(&block)
  end
end
Lambda(name, context: {}, &block) click to toggle source

Example: Lambda(:even) do |value|

value.even?

end

# File lib/definition/dsl.rb, line 56
def Lambda(name, context: {}, &block) # rubocop:disable Style/MethodName
  Types::Lambda.new(name, context: context, &block)
end
Nilable(definition) click to toggle source

Example: Nilable(Definition.Type(Integer))

# File lib/definition/dsl.rb, line 95
def Nilable(definition) # rubocop:disable Style/MethodName
  Types::Or.new(:nilable, Nil(), definition)
end
Or(*definitions) click to toggle source

Example: Or(Types::Type(Float), Types::Type(Integer))

# File lib/definition/dsl.rb, line 26
def Or(*definitions) # rubocop:disable Style/MethodName
  Types::Or.new(:or, *definitions)
end
Type(klass) click to toggle source

Example: Type(Integer)

# File lib/definition/dsl.rb, line 32
def Type(klass) # rubocop:disable Style/MethodName
  Types::Type.new(:type, klass)
end