Guest User

Untitled

a guest
Jul 22nd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. require 'rubygems'
  2. require 'rspec'
  3.  
  4. def split(original)
  5. original.split("/").inject([]) do |result, element|
  6. result + [(result.last||[]) + [element]]
  7. end.reverse.map{ |variation| variation.join("/") }
  8. end
  9.  
  10.  
  11.  
  12.  
  13. describe "#category" do
  14. it "should split the categories out" do
  15. split("bc6/energy/fuel/organic").should == ["bc6/energy/fuel/organic", "bc6/energy/fuel", "bc6/energy", "bc6"]
  16. end
  17. end
  18.  
  19.  
  20.  
  21. def category=(cat)
  22. temp_cat = cat.clone
  23. self.categories = 3.upto(temp_cat.split("/").length).collect do |variable|
  24. temp_cat = temp_cat.split("/").tap(&:pop).join("/")
  25. end.insert(0,cat)
  26. end
Add Comment
Please, Sign In to add comment