Module: ToFooFromStory

Included in:
String
Defined in:
lib/branston/vendor/plugins/restful_authentication/generators/authenticated/templates/features/step_definitions/rest_auth_features_helper.rb

Overview

Sugar for turning a story’s attribute list into list, array, etc.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fix_key(key) ⇒ Object



23
24
25
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/templates/features/step_definitions/rest_auth_features_helper.rb', line 23

def ToFooFromStory.fix_key key
  key.downcase.gsub(/\s+/, '_')
end

.fix_value(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/templates/features/step_definitions/rest_auth_features_helper.rb', line 26

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

Instance Method Details

#to_array_from_storyObject

Coverts an attribute list found in the steps into an array Example:

login, email, updated_at, and gravatar
# => ['login', 'email', 'updated_at', 'gravatar']


53
54
55
56
57
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/templates/features/step_definitions/rest_auth_features_helper.rb', line 53

def to_array_from_story
  self.split(/,? and |, /).map do |value|
    ToFooFromStory::fix_value(value)
  end
end

#to_hash_from_storyObject

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"}


42
43
44
45
46
47
48
# File 'lib/branston/vendor/plugins/restful_authentication/generators/authenticated/templates/features/step_definitions/rest_auth_features_helper.rb', line 42

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