lib/metrojobb/category.rb in metrojobb-0.5.1 vs lib/metrojobb/category.rb in metrojobb-0.6.0
- old
+ new
@@ -3,12 +3,29 @@
module Metrojobb
class Category < Model
attr_accessor :id, :name
- NAME_ID_MAP = CSV.read(
- File.expand_path('../../../data/categories.csv', __FILE__)
- ).to_h.invert.freeze
+ NAME_ID_MAP = lambda {
+ top_categories = {}
+
+ data = CSV.read(
+ File.expand_path('../../../data/categories.csv', __FILE__)
+ ).map do |row|
+ id, name = row
+ if id.length < 3
+ top_categories[id] = name
+ else
+ top_id = id[0..1]
+ top_id = id[0] if id.length == 3
+ top_name = top_categories[top_id]
+ name = "#{top_name} > #{name}"
+ end
+ [id, name]
+ end
+
+ data.to_h.invert.freeze
+ }.call
ID_NAME_MAP = NAME_ID_MAP.invert.freeze
validate :validate_known_category