Guest User

Untitled

a guest
Apr 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. Given /^the following (.+?)(|\s+only)$/ do |model_name, only, table|
  2. parser = Mongoid::Cucumber::Parser.new(model_name)
  3. model, klass = parser.model, parser.klass
  4. klass.destroy_all if only =~ /only$/
  5. @objects = Mongoid::Cucumber::Factory.create(klass, table)
  6. @objects.each(&:save!)
  7. @object = @objects.last if @objects.size == 1
  8. instance_variable_set("@#{model}", @object)
  9. end
  10.  
  11. Given /^(?:the )?(.*?) (?:has|have) the following (.+?)$/ do |parent_name, association_name, table|
  12. parent_name.gsub!(/\s/, '_')
  13. parent = instance_variable_get("@#{parent_name}")
  14. parser = Mongoid::Cucumber::Parser.new(association_name)
  15. model, klass = parser.model, parser.klass
  16. @children = Mongoid::Cucumber::Factory.create(klass, table)
  17. parent.send("#{association_name}=", @children)
  18. parent.save!
  19. instance_variable_set("@#{association_name}", @children.first)
  20. end
  21.  
  22. module Mongoid
  23. module Cucumber
  24. class Factory
  25. def self.create(klass, table)
  26. objects = table.hashes.map do |hash|
  27. attributes = hash.dup.inject({}) do |h, (k, v)|
  28. h.delete(k)
  29. h.update(k.gsub(/\s+/, "_") => v)
  30. end
  31. object = klass.make(attributes)
  32. end
  33. end
  34. end
  35. class Parser
  36. def initialize(name)
  37. @name = name
  38. end
  39. def model
  40. @name.parameterize("_").singularize.to_sym
  41. end
  42. def klass
  43. model.to_s.classify.constantize
  44. end
  45. end
  46. end
  47. end
Add Comment
Please, Sign In to add comment