Class: Odca::Overlays::LabelOverlay::CategoryResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/odca/overlays/label_overlay.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCategoryResolver

Returns a new instance of CategoryResolver.



43
44
45
46
47
# File 'lib/odca/overlays/label_overlay.rb', line 43

def initialize
  @attr_categories = []
  @category_labels = {}
  @category_attributes = {}
end

Instance Attribute Details

#attr_categoriesObject (readonly)

Returns the value of attribute attr_categories.



41
42
43
# File 'lib/odca/overlays/label_overlay.rb', line 41

def attr_categories
  @attr_categories
end

#category_attributesObject (readonly)

Returns the value of attribute category_attributes.



41
42
43
# File 'lib/odca/overlays/label_overlay.rb', line 41

def category_attributes
  @category_attributes
end

#category_labelsObject (readonly)

Returns the value of attribute category_labels.



41
42
43
# File 'lib/odca/overlays/label_overlay.rb', line 41

def category_labels
  @category_labels
end

Instance Method Details

#call(attributes) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/odca/overlays/label_overlay.rb', line 49

def call(attributes)
  attributes.each do |attribute|
    next if attribute.categories.empty?
    categories = attribute.categories.dup
    category = categories.pop

    supercategory_numbers = []

    categories.each do |supercategory|
      supercategory_attr = find_or_create_category_attr(
        supercategory_numbers, supercategory
      )
      supercategory_numbers.push(
        supercategory_attr.delete('_').split('-').last
      )
      unless attr_categories.include? supercategory_attr
        attr_categories.push(supercategory_attr)
      end
      category_labels[supercategory_attr] = supercategory
    end

    category_attr = find_or_create_category_attr(
      supercategory_numbers, category
    )
    unless attr_categories.include? category_attr
      attr_categories.push(category_attr)
    end
    category_labels[category_attr] = category

    category_attributes[category_attr] = category_attributes
      .fetch(category_attr) { [] }.push(attribute.attr_name).uniq
  end
  self
end