The Wayback Machine - https://web.archive.org/web/20220304011706/https://github.com/serradura/u-case/issues/88
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micro::Case::Result#on - Enable pattern matching of result type and data for Ruby >= 2.2.0 #88

Open
serradura opened this issue Aug 25, 2020 · 0 comments

Comments

@serradura
Copy link
Owner

@serradura serradura commented Aug 25, 2020

result.on(
  success: { 
    greet: -> (first, last) { puts "Hi #{first} #{last}!" },
    print_name: -> (last, first) { puts "#{last}, #{first}!" }
  }
)

Original POC:

https://gist.github.com/serradura/499e403e64d53dfcb8c891dfff594e20

ezgif com-video-to-gif

def on(arg, spec)
  track, result = arg.to_a[0]
  type, data = result.to_a[0]

  hook = spec.fetch(track, {})[type]

  return unless hook

  keys = hook.parameters.map(&:last)

  keys.empty? ? hook.call : hook.call(*data.values_at(*keys))
end

result = { success: { print_name: { first: 'Rodrigo', last: 'Serradura' } } }

on(
  result,
  success: { 
    greet: -> (first, last) { puts "Hi #{first} #{last}!" },
    print_name: -> (last, first) { puts "#{last}, #{first}!" }
  }
)
@serradura serradura added this to the 4.x milestone Aug 25, 2020
@serradura serradura changed the title Micro::Case::Result.on - Enable pattern matching of result type and data for Ruby >= 2.2.0 Micro::Case::Result#on - Enable pattern matching of result type and data for Ruby >= 2.2.0 Aug 25, 2020
@serradura serradura removed this from the 4.x milestone Aug 26, 2020
@serradura serradura added this to the Ideas milestone Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant