Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,25 @@ category.ancestors # [NTEE.category("R60"), NTEE.category("R")]

Easy-peasy!

There is also a helper method to make this easy to build Rails selection dropdowns

```ruby
NTEE.as_list
```

## search_dimensions integration

The file `lib/ntee/search_dimension.rb` defines a few classes you can use in conjunction with Gively's `search_dimensions` gem to deal with NTEE categories stored in a Solr search index. `NTEE::HierarchicalDimension` lets you treat the categories as a hierarchical tree, and `NTEE::FlatDimension` lets you treat them as a plain string field.

## Licensing

This gem is Copyright © 2011-2012 Gively, Inc. and is released under the MIT license. For more details, please see the LICENSE file.
This gem is Copyright © 2011-2012 Gively, Inc. and is released under the MIT license. For more details, please see the LICENSE file.

## Testing and Debugging

To test in console

```shell
bundle install
bundle exec rake test:console
```
9 changes: 9 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"

namespace :test do
desc "Test Task"

desc "Load stuff in IRB."
task :console do
exec "irb -r rubygems -r pp -r ./lib/ntee"
end
end
61 changes: 41 additions & 20 deletions lib/ntee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,60 @@
module NTEE
class Category
attr_accessor :name, :code, :subcategories, :parent

def initialize
self.subcategories ||= {}
end


def to_s
"#{name} (#{code})"
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 +77,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 +97,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 +106,31 @@ def add_category!(category)
all_categories[category.code.to_s] = category
end
end

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

def self.as_list
@as_list ||= sort_by_code(flattened_categories)
end

private

def self.flattened_categories
all_categories.map do |parent_category|
c = parent_category[1]
[c.to_s, c.code]
end
end

def self.sort_by_code(arr)
arr.sort {|a, b| a[1] <=> b[1] }
end
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 +142,4 @@ def add_category!(category)
rescue
puts "WARNING: Couldn't load NTEE categories!"
puts $!
end
end
2 changes: 1 addition & 1 deletion lib/ntee/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module NTEE
VERSION = "0.0.3"
VERSION = "0.0.4"
end
1 change: 1 addition & 0 deletions ntee.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Gem::Specification.new do |gem|

gem.add_development_dependency 'i18n'
gem.add_development_dependency 'activesupport', '>= 3.0'
gem.add_development_dependency 'rake'
end