module ReSorcery::Fielded

Public Class Methods

included(base) click to toggle source
# File lib/re_sorcery/fielded.rb, line 31
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

fields() click to toggle source

Returns the ‘Decoder#test`ed fields of the object, wrapped in a `Result`

If all the ‘Decoder`s pass, this will return an `Ok`. If any of them fail, it will return an `Err` instead.

@return [Result<String, Hash>]

# File lib/re_sorcery/fielded.rb, line 41
def fields
  self.class.instance_exec { @fields ||= [] }.inject(ok({})) do |result_hash, (name, field_hash)|
    result_hash.and_then do |ok_hash|
      field_hash[:type].test(instance_exec(&field_hash[:pro]))
        .and_then { |tested| ExpandInternalFields.expand(tested) }
        .map { |fielded| ok_hash.merge(name => fielded) }
        .map_error { |error| "Error at field `#{name}` of `#{self.class}`: #{error}" }
    end
  end
end