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::GreaterThan(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

#BooleanObject

Example: Boolean



74
75
76
# File 'lib/definition/dsl.rb', line 74

def Boolean # rubocop:disable Naming/MethodName
  Types::Or.new(:boolean, Type(TrueClass), Type(FalseClass))
end

#CoercibleModel(klass) ⇒ Object

Example: CoercibleModel(ModelClass)



80
81
82
83
84
85
86
87
88
89
# File 'lib/definition/dsl.rb', line 80

def CoercibleModel(klass) # rubocop:disable Naming/MethodName
  Types::Or.new(:coercible_model,
                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: { model_class: klass }) do |value|
                    conform_with(klass.new(value)) # Actually coerce the input to klass
                  end
                ))
end

#CoercibleType(klass) ⇒ Object

Example: CoercibleType(Integer)



38
39
40
41
42
43
44
45
46
47
48
# 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|
    method(klass.name).call(value)
  rescue ArgumentError
    value
  end
end

#Each(definition) ⇒ Object

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



68
69
70
# File 'lib/definition/dsl.rb', line 68

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

#Enum(*allowed_values) ⇒ Object

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



60
61
62
63
64
# File 'lib/definition/dsl.rb', line 60

def Enum(*allowed_values) # rubocop:disable Naming/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 Naming/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



54
55
56
# File 'lib/definition/dsl.rb', line 54

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

#Nilable(definition) ⇒ Object

Example: Nilable(Definition.Type(Integer))



93
94
95
# File 'lib/definition/dsl.rb', line 93

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