peterc / rehub

ReHub - Rails App To Maintain "Resource Hubs"

This URL has Read+Write access

peterc (author)
Sun Jun 08 10:24:58 -0700 2008
commit  8f4e654e22fb21f48707bcfc16be5cb398611853
tree    52c1b8d54063b0893887729b3be24462933432d1
parent  88fd46de20a76195d16d16bd02d4cf2a5965058b
rehub / stories / rest_auth_stories_helper.rb
100644 82 lines (76 sloc) 2.817 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# If you have a global stories helper, move this line there:
include AuthenticatedTestHelper
 
# Most of the below came out of code from Ben Mabey
# http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/
 
# These allow exceptions to come through as opposed to being caught and hvaing non-helpful responses returned.
ActionController::Base.class_eval do
  def perform_action
    perform_action_without_rescue
  end
end
Dispatcher.class_eval do
  def self.failsafe_response(output, status, exception = nil)
    raise exception
  end
end
 
#
# Sugar for turning a story's attribute list into list, array, etc.
#
module ToFooFromStory
  def ToFooFromStory.fix_key key
    key.downcase.gsub(/\s+/, '_')
  end
  def ToFooFromStory.fix_value value
    return '' if !value
    value.strip!
    case
    when value =~ /^'(.*)'$/ then value = $1
    when value =~ /^"(.*)"$/ then value = $1
    when value == 'nil!' then value = nil
    when value == 'non-nil!' then value = be_nil
    when value =~ /^#\{(.*)\}$/ then value = eval($1)
    end
    value
  end
  # Converts a key: value list found in the steps into a hash.
  # Example:
  # ISBN: '0967539854' and comment: 'I love this book' and Quality rating: '4'
  # # => {"quality_rating"=>"4", "isbn"=>"0967539854", "comment"=>"I love this book"}
  def to_hash_from_story
    hsh = self.split(/,? and |, /).inject({}) do |hash_so_far, key_value|
      key, value = key_value.split(":")
      if !value then warn "Couldn't understand story '#{self}': only understood up to the part '#{hash_so_far.to_yaml}'" end
      hash_so_far.merge(ToFooFromStory::fix_key(key) => ToFooFromStory::fix_value(value))
    end
  end
  # Coverts an attribute list found in the steps into an array
  # Example:
  # login, email, updated_at, and gravatar
  # # => ['login', 'email', 'updated_at', 'gravatar']
  def to_array_from_story
    self.split(/,? and |, /).map do |value|
      ToFooFromStory::fix_value(value)
    end
  end
end
class String
  include ToFooFromStory
end
 
def instantize(string)
  instance_variable_get("@#{string}")
end
 
#
# Spew response onto screen -- painful but scrolling >> debugger
#
def dump_response
  # note that @request and @template won't to_yaml and that @session includes @cgi
  response_methods = response.instance_variables - ['@request', '@template', '@cgi']
  request_methods = response.request.instance_variables - ['@session_options_with_string_keys', '@cgi', '@session']
  response_methods.map!{|attr| attr.gsub(/^@/,'')}.sort!
  request_methods.map!{ |attr| attr.gsub(/^@/,'')}.sort!
  puts '', '*' * 75,
    response.instance_values.slice(*response_methods).to_yaml,
    "*" * 75, '',
    response.request.instance_values.slice(*request_methods).to_yaml,
    "*" * 75, ''
end