Module: Dry::Types::Builder

Defined in:
lib/forget-passwords/types.rb

Instance Method Summary collapse

Instance Method Details

#hash_defaultObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/forget-passwords/types.rb', line 99

def hash_default
  # obtain all the required keys from the spec
  reqd = keys.select(&:required?)

  if reqd.empty?
    # there aren't any required keys, but we'll set the empty hash
    # as a default if there exist optional keys, otherwise any
    # default will interfere with input from upstream.
    return default({}.freeze) unless keys.empty?
  else
    # similarly, we only set a default if all the required keys have them.
    return default(reqd.map { |k| [k.name, k.value] }.to_h.freeze) if
      reqd.all?(&:default?)
    # XXX THIS WILL FAIL IF THE DEFAULT IS A PROC; THE FAIL IS IN DRY-RB
  end

  # otherwise just return self
  self
end