Module: ReSorcery::Fielded

Includes:
Helpers
Included in:
ReSorcery, Maybe::Just, Maybe::Nothing
Defined in:
lib/re_sorcery/fielded.rb,
lib/re_sorcery/fielded/expand_internal_fields.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



35
36
37
# File 'lib/re_sorcery/fielded.rb', line 35

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#fieldsResult<String, Hash>

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.

Returns:



45
46
47
48
49
50
51
52
53
# File 'lib/re_sorcery/fielded.rb', line 45

def fields
  self.class.send(:fields).inject(ok({})) do |result_hash, (name, field_hash)|
    result_hash.assign(name) do
      field_hash[:type].test(instance_exec(&field_hash[:pro]))
        .and_then { |tested| ExpandInternalFields.expand(tested) }
        .map_error { |error| "Error at field `#{name}` of `#{self.class}`: #{error}" }
    end
  end
end