Class: Expectant::Expectation

Inherits:
Object
  • Object
show all
Defined in:
lib/expectant/expectation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type: nil, default: nil, optional: false, fallback: nil, **options) ⇒ Expectation

Returns a new instance of Expectation.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/expectant/expectation.rb', line 7

def initialize(name, type: nil, default: nil, optional: false, fallback: nil, **options)
  @name = name
  @type = type
  @default = default
  @fallback = fallback
  @metadata = options

  base_type = Types.resolve(type)
  base_type = base_type.optional if optional
  # if default is a proc, we call it at runtime
  @dry_type = if !default.nil? && !default.respond_to?(:call)
    base_type.default(default)
  else
    base_type
  end
end

Instance Attribute Details

#default ⇒ Object (readonly)

Returns the value of attribute default.



5
6
7
# File 'lib/expectant/expectation.rb', line 5

def default
  @default
end

#dry_type ⇒ Object (readonly)

Returns the value of attribute dry_type.



5
6
7
# File 'lib/expectant/expectation.rb', line 5

def dry_type
  @dry_type
end

#fallback ⇒ Object (readonly)

Returns the value of attribute fallback.



5
6
7
# File 'lib/expectant/expectation.rb', line 5

def fallback
  @fallback
end

#metadata ⇒ Object (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'lib/expectant/expectation.rb', line 5

def 
  @metadata
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/expectant/expectation.rb', line 5

def name
  @name
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/expectant/expectation.rb', line 5

def type
  @type
end

Instance Method Details

#has_default? ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/expectant/expectation.rb', line 29

def has_default?
  @dry_type.default? || !@default.nil?
end

#has_fallback? ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/expectant/expectation.rb', line 33

def has_fallback?
  !@fallback.nil?
end

#required? ⇒ Boolean

A field is required if it's not optional and has no default

Returns:

  • (Boolean)


25
26
27
# File 'lib/expectant/expectation.rb', line 25

def required?
  !@dry_type.optional? && !has_default?
end