Class: HaystackTag

Inherits:
BaseTag show all
Defined in:
lib/needle_in_a_haystack/models/haystack_tag.rb

Constant Summary collapse

PATH_ONTOLOGY =

PATH_ONTOLOGY = “/Users/frans/Documents/fouriq/fouriq_haystack/config/haystack_ontology.yml”

"/Users/frans/Documents/fouriq/fouriq_haystack/config/haystack_ontology.yml"
CATEGORIES =
YAML.load_file(PATH_ONTOLOGY)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_path(path) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/needle_in_a_haystack/models/haystack_tag.rb', line 34

def self.find_by_path(path)
  keys = path.split(".")
  current = nil
  keys.each do |key|
    current = current ? current.children.find_by(name: key) : find_by(name: key)
    return nil unless current
  end
  current
end

Instance Method Details

#ancestorsObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/needle_in_a_haystack/models/haystack_tag.rb', line 18

def ancestors
  ancestors = []
  current = self
  while current.parent_tag
    break if ancestors.include?(current.parent_tag)

    ancestors << current.parent_tag
    current = current.parent_tag
  end
  ancestors
end

#categoryObject



64
65
66
# File 'lib/needle_in_a_haystack/models/haystack_tag.rb', line 64

def category
  ancestors.empty? ? name : parent_tag.category
end

#depthObject



60
61
62
# File 'lib/needle_in_a_haystack/models/haystack_tag.rb', line 60

def depth
  ancestors.size
end

#descendantsObject



44
45
46
# File 'lib/needle_in_a_haystack/models/haystack_tag.rb', line 44

def descendants
  children + children.flat_map(&:descendants)
end

#full_pathObject



30
31
32
# File 'lib/needle_in_a_haystack/models/haystack_tag.rb', line 30

def full_path
  "#{ancestors.reverse.map(&:name).join(' > ')} > #{name}"
end

#leaf?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/needle_in_a_haystack/models/haystack_tag.rb', line 56

def leaf?
  children.empty?
end

#root?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/needle_in_a_haystack/models/haystack_tag.rb', line 52

def root?
  parent_tag.nil?
end

#siblingsObject



48
49
50
# File 'lib/needle_in_a_haystack/models/haystack_tag.rb', line 48

def siblings
  parent_tag ? parent_tag.children.where.not(id: id) : self.class.where(parent_tag_id: nil).where.not(id: id)
end