Guest User

Untitled

a guest
Apr 23rd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. require 'spec_helper'
  2.  
  3. describe LoseItDispatcher do
  4. describe ".deliver_phone_confirmation(user)" do
  5. it "should call the same method on the SmsNotifier" do
  6. user = mock_model(User, :phone_number => '12673346833', :null_object => true)
  7. SmsNotifier.should_receive(:deliver_phone_confirmation).with(user) # wrong number of arguments (2 for 1)
  8. LoseItDispatcher.deliver_phone_confirmation(user)
  9. end
  10. end
  11. end
  12.  
  13. class ActionTextMessager
  14. def initialize(method_name=nil, *parameters)
  15. obj = self
  16. self.send(method_name, *parameters)
  17. obj
  18. end
  19.  
  20. class << self
  21. def method_missing(method_symbol, *parameters)
  22. if match = matches_dynamic_method?(method_symbol)
  23. case match[1]
  24. when 'create' then new(match[2], *parameters)
  25. when 'deliver' then new(match[2], *parameters).deliver!
  26. else super
  27. end
  28. else
  29. super
  30. end
  31. end
  32.  
  33. # problem goes away when I get rid of this
  34. def respond_to?(method_symbol)
  35. if match = matches_dynamic_method?(method_symbol)
  36. instance_methods.include?(match[2])
  37. else
  38. super
  39. end
  40. end
  41.  
  42. private
  43. def matches_dynamic_method?(method_name)
  44. method_name = method_name.to_s
  45. /^(create|deliver)_([_a-z]\w*)/.match(method_name)
  46. end
  47. end
  48.  
  49. %w(number message).each do |method_name|
  50. define_method(method_name) do |*args|
  51. arg = *args # we can't set a default when defining a method this way
  52. if arg
  53. instance_variable_set("@#{method_name}", arg)
  54. else
  55. instance_variable_get("@#{method_name}")
  56. end
  57. end
  58. end
  59.  
  60. def deliver!
  61. CLICKATELL_API.send_message(number, message)
  62. end
  63. end
Add Comment
Please, Sign In to add comment