Class: Results::Good

Inherits:
Struct
  • Object
show all
Defined in:
lib/results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



85
86
87
# File 'lib/results.rb', line 85

def value
  @value
end

Instance Method Details

#andObject



110
111
112
# File 'lib/results.rb', line 110

def and
  self
end

#flat_map {|value| ... } ⇒ Object Also known as: validate

Yields:



90
91
92
# File 'lib/results.rb', line 90

def flat_map
  yield(value)
end

#mapObject



86
87
88
# File 'lib/results.rb', line 86

def map
  flat_map { |v| Good.new(yield v) }
end

#validate_all(*validations) ⇒ Object



122
123
124
# File 'lib/results.rb', line 122

def validate_all(*validations)
  validations.flatten.inject(self) { |prev_result, validation| prev_result.and.validate(&validation) }
end

#when(msg_or_proc_or_filter) ⇒ Object



96
97
98
99
100
101
# File 'lib/results.rb', line 96

def when(msg_or_proc_or_filter)
  validate do |v|
    predicate, msg_or_proc = extract_predicate_and_message(msg_or_proc_or_filter, v) { yield v }
    predicate ? self : Bad.new(Results.call_or_yield_or_return(msg_or_proc, v) { |msg| 'not ' + msg }, v)
  end
end

#when_all(*filters) ⇒ Object



114
115
116
# File 'lib/results.rb', line 114

def when_all(*filters)
  filters.flatten.inject(self) { |prev_result, filter| prev_result.and.when(filter) }
end

#when_all_not(*filters) ⇒ Object



118
119
120
# File 'lib/results.rb', line 118

def when_all_not(*filters)
  filters.flatten.inject(self) { |prev_result, filter| prev_result.and.when_not(filter) }
end

#when_not(msg_or_proc_or_filter) ⇒ Object



103
104
105
106
107
108
# File 'lib/results.rb', line 103

def when_not(msg_or_proc_or_filter)
  validate do |v|
    predicate, msg_or_proc = extract_predicate_and_message(msg_or_proc_or_filter, v) { yield v }
    !predicate ? self : Bad.new(Results.call_or_yield_or_return(msg_or_proc, v), v)
  end
end

#zip(other) ⇒ Object



126
127
128
129
130
131
# File 'lib/results.rb', line 126

def zip(other)
  case other
  when Good then Good.new([value, other.value])
  when Bad  then other
  end
end