Module: Definition::Dsl

Included in:
Definition
Defined in:
lib/definition/dsl.rb,
lib/definition/dsl/nil.rb,
lib/definition/dsl/comparators.rb

Defined Under Namespace

Modules: Comparators, Nil

Instance Method Summary collapse

Instance Method Details

#And(*definitions) ⇒ Object

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



20
21
22
# File 'lib/definition/dsl.rb', line 20

def And(*definitions) # rubocop:disable Naming/MethodName
  Types::And.new(:and, *definitions)
end

#CoercibleType(klass) ⇒ Object

Example: CoercibleType(Integer)



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/definition/dsl.rb', line 38

def CoercibleType(klass) # rubocop:disable Naming/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

#Each(definition) ⇒ Object

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



70
71
72
# File 'lib/definition/dsl.rb', line 70

def Each(definition) # rubocop:disable Naming/MethodName
  Types::Each.new(:each, definition: definition)
end

#Enum(*allowed_values) ⇒ Object

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



62
63
64
65
66
# File 'lib/definition/dsl.rb', line 62

def Enum(*allowed_values) # rubocop:disable Naming/MethodName
  Lambda("enum #{allowed_values.inspect}") do |value|
    conform_with(value) if allowed_values.include?(value)
  end
end

#Keys(&block) ⇒ Object

Example: Keys do

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

end



12
13
14
15
16
# File 'lib/definition/dsl.rb', line 12

def Keys(&block) # rubocop:disable Naming/MethodName
  Types::Keys.new(:hash).tap do |instance|
    instance.instance_exec(&block)
  end
end

#Lambda(name, &block) ⇒ Object

Example: Lambda(:even) do |value|

value.even?

end



56
57
58
# File 'lib/definition/dsl.rb', line 56

def Lambda(name, &block) # rubocop:disable Naming/MethodName
  Types::Lambda.new(name, &block)
end

#Or(*definitions) ⇒ Object

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



26
27
28
# File 'lib/definition/dsl.rb', line 26

def Or(*definitions) # rubocop:disable Naming/MethodName
  Types::Or.new(:or, *definitions)
end

#Type(klass) ⇒ Object

Example: Type(Integer)



32
33
34
# File 'lib/definition/dsl.rb', line 32

def Type(klass) # rubocop:disable Naming/MethodName
  Types::Type.new(:type, klass)
end