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 Style/MethodName
  Types::And.new(:and, *definitions)
end

#BooleanObject

Example: Boolean



76
77
78
# File 'lib/definition/dsl.rb', line 76

def Boolean # rubocop:disable Style/MethodName
  Types::Or.new(:boolean, Type(TrueClass), Type(FalseClass))
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 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) ⇒ Object

Example: CoercibleValueObject(ValueObjectClass)



82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object

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



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

def Each(definition) # rubocop:disable Style/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 Style/MethodName
  Lambda("enum", context: { allowed_values: allowed_values }) 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 Style/MethodName
  Types::Keys.new(:hash).tap do |instance|
    instance.instance_exec(&block)
  end
end

#Lambda(name, context: {}, &block) ⇒ Object

Example: Lambda(:even) do |value|

value.even?

end



56
57
58
# 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) ⇒ Object

Example: Nilable(Definition.Type(Integer))



95
96
97
# File 'lib/definition/dsl.rb', line 95

def Nilable(definition) # rubocop:disable Style/MethodName
  Types::Or.new(:nilable, Nil(), definition)
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 Style/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 Style/MethodName
  Types::Type.new(:type, klass)
end