Skip to content

Instantly share code, notes, and snippets.

@stellard
Forked from opsb/category breakdown
Created February 17, 2011 17:37
Show Gist options
  • Save stellard/832214 to your computer and use it in GitHub Desktop.
Save stellard/832214 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'rspec'
def split(original)
original.split("/").inject([]) do |result, element|
result + [(result.last||[]) + [element]]
end.reverse.map{ |variation| variation.join("/") }
end
describe "#category" do
it "should split the categories out" do
split("bc6/energy/fuel/organic").should == ["bc6/energy/fuel/organic", "bc6/energy/fuel", "bc6/energy", "bc6"]
end
end
def category=(cat)
temp_cat = cat.clone
self.categories = 3.upto(temp_cat.split("/").length).collect do |variable|
temp_cat = temp_cat.split("/").tap(&:pop).join("/")
end.insert(0,cat)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment