Class: Hubbado::Sequence::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/hubbado/sequence/result.rb

Constant Summary collapse

FRAMEWORK_I18N_SCOPE =
"sequence.errors".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, ctx, error:, trail:, i18n_scope:) ⇒ Result

Returns a new instance of Result.



23
24
25
26
27
28
29
# File 'lib/hubbado/sequence/result.rb', line 23

def initialize(status, ctx, error:, trail:, i18n_scope:)
  @status = status
  @ctx = ctx
  @error = error
  @trail = trail
  @i18n_scope = i18n_scope
end

Instance Attribute Details

#ctxObject (readonly)

Returns the value of attribute ctx.



6
7
8
# File 'lib/hubbado/sequence/result.rb', line 6

def ctx
  @ctx
end

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/hubbado/sequence/result.rb', line 7

def error
  @error
end

#i18n_scopeObject (readonly)

Returns the value of attribute i18n_scope.



9
10
11
# File 'lib/hubbado/sequence/result.rb', line 9

def i18n_scope
  @i18n_scope
end

#trailObject (readonly)

Returns the value of attribute trail.



8
9
10
# File 'lib/hubbado/sequence/result.rb', line 8

def trail
  @trail
end

Class Method Details

.fail(ctx, error:, trail: [], i18n_scope: nil) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/hubbado/sequence/result.rb', line 15

def self.fail(ctx, error:, trail: [], i18n_scope: nil)
  unless error.is_a?(Hash) && error[:code]
    raise ArgumentError, "Result.fail requires error: { code: ... }"
  end

  new(:fail, ctx, error: error, trail: trail, i18n_scope: i18n_scope)
end

.ok(ctx, trail: [], i18n_scope: nil) ⇒ Object



11
12
13
# File 'lib/hubbado/sequence/result.rb', line 11

def self.ok(ctx, trail: [], i18n_scope: nil)
  new(:ok, ctx, error: nil, trail: trail, i18n_scope: i18n_scope)
end

Instance Method Details

#failure?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/hubbado/sequence/result.rb', line 35

def failure?
  @status == :fail
end

#messageObject



49
50
51
52
53
54
55
56
# File 'lib/hubbado/sequence/result.rb', line 49

def message
  return nil if ok?

  translation = translate_with_chain
  return translation if translation

  @error[:message] || humanize_code
end

#ok?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/hubbado/sequence/result.rb', line 31

def ok?
  @status == :ok
end

#with_i18n_scope(scope) ⇒ Object



43
44
45
46
47
# File 'lib/hubbado/sequence/result.rb', line 43

def with_i18n_scope(scope)
  return self unless @i18n_scope.nil?

  self.class.new(@status, @ctx, error: @error, trail: @trail, i18n_scope: scope)
end

#with_trail(trail) ⇒ Object



39
40
41
# File 'lib/hubbado/sequence/result.rb', line 39

def with_trail(trail)
  self.class.new(@status, @ctx, error: @error, trail: trail, i18n_scope: @i18n_scope)
end