Skip to content

Instantly share code, notes, and snippets.

@serradura
Created July 9, 2020 00:40
Show Gist options
  • Save serradura/2491361ea4f5eaa0af9e3c9a2ce7a561 to your computer and use it in GitHub Desktop.
Save serradura/2491361ea4f5eaa0af9e3c9a2ce7a561 to your computer and use it in GitHub Desktop.
Live code - u-case 101
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'u-case'
gem 'pry'
end
class Sum < Micro::Case
attributes :a, :b
def call!
if a.kind_of?(Numeric) && b.kind_of?(Numeric)
Success(number: a + b)
else
Failure(:attributes_must_be_numerics)
end
end
end
Sum
.call(a: 1, b: 2)
.on_success { |result| puts result[:number] }
.on_failure { puts 'Oops' }
class Double < Micro::Case
attribute :number
def call!
if number.kind_of?(Numeric)
Success(number: number * 2)
else
Failure(:attributes_must_be_numerics)
end
end
end
# SumNumberAndDouble = Micro::Case::Flow([
# Sum, Double
# ])
# class SumNumberAndDouble < Micro::Case
# flow Sum,
# Double
# end
class SumNumberAndDouble < Micro::Case
flow Sum,
self.call!
attribute :number
def call!
if number.kind_of?(Numeric)
Success(number: number * 2)
else
Failure(:attributes_must_be_numerics)
end
end
end
Sum
.call(a: 2, b: 2)
.then(Double)
.on_success { |result| puts result[:number] }
.on_failure { puts 'Oops' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment