Class: Sheetah::Types::CastChain

Inherits:
Object
  • Object
show all
Includes:
Utils::MonadicResult
Defined in:
lib/sheetah/types/cast_chain.rb

Constant Summary

Constants included from Utils::MonadicResult

Utils::MonadicResult::Unit

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::MonadicResult

#Do, #Failure, #Success

Constructor Details

#initialize(casts = []) ⇒ CastChain

Returns a new instance of CastChain.



10
11
12
# File 'lib/sheetah/types/cast_chain.rb', line 10

def initialize(casts = [])
  @casts = casts
end

Instance Attribute Details

#castsObject (readonly)

Returns the value of attribute casts.



14
15
16
# File 'lib/sheetah/types/cast_chain.rb', line 14

def casts
  @casts
end

Instance Method Details

#append(cast) ⇒ Object



21
22
23
24
# File 'lib/sheetah/types/cast_chain.rb', line 21

def append(cast)
  @casts.push(cast)
  self
end

#call(value, messenger) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sheetah/types/cast_chain.rb', line 32

def call(value, messenger)
  failure = catch(:failure) do
    success = catch(:success) do
      @casts.reduce(value) do |prev_value, cast|
        cast.call(prev_value, messenger)
      end
    end

    return Success(success)
  end

  messenger.error(failure) if failure

  Failure()
end

#freezeObject



26
27
28
29
30
# File 'lib/sheetah/types/cast_chain.rb', line 26

def freeze
  @casts.each(&:freeze)
  @casts.freeze
  super
end

#prepend(cast) ⇒ Object



16
17
18
19
# File 'lib/sheetah/types/cast_chain.rb', line 16

def prepend(cast)
  @casts.unshift(cast)
  self
end