Skip to content

Commit

Permalink
Clean up whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
rickychilcott committed Sep 19, 2018
1 parent 5d90f40 commit a9754f5
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions lib/ntee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,56 @@
module NTEE
class Category
attr_accessor :name, :code, :subcategories, :parent

def initialize
self.subcategories ||= {}
end

def inspect
"<NTEE Category #{code} (#{name})>"
end

def [](subcategory_code)
self.subcategories[subcategory_code.to_s]
end

def parent=(parent)
if parent && parent[code] != self
parent.add_subcategory!(self)
parent.add_subcategory!(self)
end

@parent = parent
end

def ancestors
if parent
[parent] + parent.ancestors
else
[]
end
end

def descendants
(subcategories.values + subcategories.values.map(&:descendants)).flatten
end

def add_subcategory!(subcategory)
subcategories[subcategory.code.to_s] = subcategory
subcategory.parent = self
subcategories
end

def as_json(options={})
hsh = {
'code' => code,
'name' => name
}

hsh['subcategories'] = subcategories.values.as_json(options) if subcategories && subcategories.count > 0

hsh
end

def attributes=(attributes)
attributes.each do |name, value|
case name.to_s
Expand All @@ -73,18 +73,18 @@ def attributes=(attributes)
raise "Subcategory #{value.inspect} is neither a Category nor a Hash"
end
end

subcats.each do |subcat|
add_subcategory!(subcat)
end
end
end
end
end

class << self
attr_accessor :root_categories, :all_categories

def category(cat_or_code)
case cat_or_code
when NTEE::Category
Expand All @@ -93,7 +93,7 @@ def category(cat_or_code)
all_categories[cat_or_code.to_s]
end
end

def add_category!(category)
root_categories[category.code.to_s] = category if category.parent.nil?
category.subcategories.each do |code, subcategory|
Expand All @@ -102,14 +102,14 @@ def add_category!(category)
all_categories[category.code.to_s] = category
end
end

self.root_categories = {}
self.all_categories = {}
end

begin
require 'json'

File.open(File.expand_path("../ntee_categories.json", __FILE__), 'r') do |file|
JSON.load(file).each do |attributes|
NTEE::Category.new.tap do |category|
Expand All @@ -121,4 +121,4 @@ def add_category!(category)
rescue
puts "WARNING: Couldn't load NTEE categories!"
puts $!
end
end

0 comments on commit a9754f5

Please sign in to comment.