Guest User

Untitled

a guest
Apr 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. module FollowEventTransitionMatcher
  2.  
  3. class FollowEventTransition
  4.  
  5. def initialize(event, options)
  6. @event = event
  7. @from = options[:from].is_a?(Array) ? options[:from] : [options[:from]]
  8. @to = options[:to]
  9. end
  10.  
  11. def matches?(actual)
  12. actual.aasm_states.each do |state|
  13. o = build_type(actual, :state => state.name.to_sym)
  14. if @from.to_a.include? state.name.to_sym
  15. o.send(@event.to_sym)
  16. if !o.send("#{@to}?".to_sym)
  17. return false
  18. end
  19. else
  20. begin
  21. o.send(@event.to_sym)
  22. return false
  23. rescue AASM::InvalidTransition
  24. end
  25. end
  26. end
  27. end
  28.  
  29. def build_type(klass, opts = {})
  30. Factory.build(klass.to_s.downcase.to_sym, opts)
  31. end
  32.  
  33. def failure_message_for_should
  34. "Expected #{@event} to only allow transitions from #{@from} to #{@to}"
  35. end
  36.  
  37. def failure_message_for_should_not
  38. "WARN: follow event transition shouldn't be used with the should_not syntax."
  39. end
  40.  
  41. end
  42.  
  43. def follow_event_transition(event, opts = {})
  44. FollowEventTransition.new(event, opts)
  45. end
  46.  
  47. end
Add Comment
Please, Sign In to add comment