Class: Dry::Schema::Message

Inherits:
Object
  • Object
show all
Extended by:
Initializer
Defined in:
lib/dry/schema/message.rb,
lib/dry/schema/message/or.rb,
lib/dry/schema/extensions/hints.rb,
lib/dry/schema/message/or/abstract.rb,
lib/dry/schema/message/or/multi_path.rb,
lib/dry/schema/message/or/single_path.rb

Overview

Hint-specific Message extensions

See Also:

Direct Known Subclasses

Hint

Defined Under Namespace

Modules: Or

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsArray (readonly)

Optional list of arguments used by the predicate

Returns:

  • (Array)


36
# File 'lib/dry/schema/message.rb', line 36

option :args, default: proc { EMPTY_ARRAY }

#inputObject (readonly)

The input value

Returns:

  • (Object)


41
# File 'lib/dry/schema/message.rb', line 41

option :input

#metaHash (readonly)

Arbitrary meta data

Returns:

  • (Hash)


46
# File 'lib/dry/schema/message.rb', line 46

option :meta, optional: true, default: proc { EMPTY_HASH }

#pathString (readonly)

Path to the value

Returns:

  • (String)


26
# File 'lib/dry/schema/message.rb', line 26

option :path

#predicateSymbol (readonly)

Predicate identifier that was used to produce a message

Returns:

  • (Symbol)


31
# File 'lib/dry/schema/message.rb', line 31

option :predicate

#textString (readonly)

Message text representation created from a localized template

Returns:

  • (String)


21
# File 'lib/dry/schema/message.rb', line 21

option :text

Instance Method Details

#<=>(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See which message is higher in the hierarchy



95
96
97
98
99
100
101
102
103
104
# File 'lib/dry/schema/message.rb', line 95

def <=>(other)
  l_path = _path
  r_path = other._path

  unless l_path.same_root?(r_path)
    raise ArgumentError, "Cannot compare messages from different root paths"
  end

  l_path <=> r_path
end

#_pathObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



107
108
109
# File 'lib/dry/schema/message.rb', line 107

def _path
  @_path ||= Path[path]
end

#dumpString, Hash Also known as: to_s

Dump the message to a representation suitable for the message set hash

Returns:

  • (String, Hash)


53
54
55
# File 'lib/dry/schema/message.rb', line 53

def dump
  @dump ||= meta.empty? ? text : {text: text, **meta}
end

#eql?(other) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See if another message is the same

If a string is passed, it will be compared with the text

Parameters:

Returns:

  • (Boolean)


80
81
82
# File 'lib/dry/schema/message.rb', line 80

def eql?(other)
  other.is_a?(String) ? text == other : super
end

#hint?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


42
43
44
# File 'lib/dry/schema/extensions/hints.rb', line 42

def hint?
  false
end

#to_hHash

Dump the message into a hash

The hash will be deeply nested if the path's size is greater than 1

Returns:

  • (Hash)

See Also:



67
68
69
# File 'lib/dry/schema/message.rb', line 67

def to_h
  @to_h ||= _path.to_h(dump)
end

#to_or(root) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
89
90
# File 'lib/dry/schema/message.rb', line 85

def to_or(root)
  clone = dup
  clone.instance_variable_set("@path", path - root.to_a)
  clone.instance_variable_set("@_path", nil)
  clone
end