Class: ReSorcery::Maybe::Just

Inherits:
Object
  • Object
show all
Includes:
Fielded
Defined in:
lib/re_sorcery/maybe/just.rb

Instance Method Summary collapse

Methods included from Fielded

#fields, included

Constructor Details

#initialize(value) ⇒ Just

Returns a new instance of Just.



11
12
13
# File 'lib/re_sorcery/maybe/just.rb', line 11

def initialize(value)
  @value = value
end

Instance Method Details

#==(other) ⇒ Object



38
39
40
# File 'lib/re_sorcery/maybe/just.rb', line 38

def ==(other)
  other.class == Just && other.instance_eval { @value } == @value
end

#and_then(&block) ⇒ Object



15
16
17
# File 'lib/re_sorcery/maybe/just.rb', line 15

def and_then(&block)
  ArgCheck['block', block.call(@value), Just, Nothing]
end

#as_jsonObject



42
43
44
45
46
47
# File 'lib/re_sorcery/maybe/just.rb', line 42

def as_json(*)
  {
    kind: :just,
    value: @value,
  }
end

#assign(name, &block) ⇒ Object



31
32
33
34
35
36
# File 'lib/re_sorcery/maybe/just.rb', line 31

def assign(name, &block)
  raise Error::NonHashAssignError, @value unless @value.is_a?(Hash)

  ArgCheck['block', block.call(@value), Just, Nothing]
    .map { |k| @value.merge(name => k) }
end

#get_or_elseObject



27
28
29
# File 'lib/re_sorcery/maybe/just.rb', line 27

def get_or_else
  @value
end

#map(&block) ⇒ Object



19
20
21
# File 'lib/re_sorcery/maybe/just.rb', line 19

def map(&block)
  Just.new(block.call(@value))
end

#or_elseObject



23
24
25
# File 'lib/re_sorcery/maybe/just.rb', line 23

def or_else
  self
end