module ReSorcery::Fielded::ClassMethods

Private Instance Methods

field(name, type, pro = -> { send(name) } click to toggle source

Set a field for instances of a class

There is intentionally no way to make fields optionally nil. Use a type that more meaningfully represents an empty value instead, such as a ‘Maybe` type or discriminated unions.

@param [Symbol] name @param [arg of Decoder.is] type @see ‘ReSorcery::Decoder.is` for details @param [Proc] pro: in the context of an instance of the class, return the value of the field

# File lib/re_sorcery/fielded.rb, line 23
def field(name, type, pro = -> { send(name) })
  ArgCheck['name', name, Symbol]
  ArgCheck['pro', pro, Proc]

  (@fields ||= {})[name] = { type: is(type), pro: pro }
end