Class: Showcase::Path::Tree

Inherits:
Struct
  • Object
show all
Defined in:
app/models/showcase/path.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, children = []) ⇒ Tree

Returns a new instance of Tree.



3
4
5
# File 'app/models/showcase/path.rb', line 3

def initialize(id, children = [])
  super(id, children, false)
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children

Returns:

  • (Object)

    the current value of children



2
3
4
# File 'app/models/showcase/path.rb', line 2

def children
  @children
end

#idObject

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



2
3
4
# File 'app/models/showcase/path.rb', line 2

def id
  @id
end

#rootObject Also known as: root?

Returns the value of attribute root

Returns:

  • (Object)

    the current value of root



2
3
4
# File 'app/models/showcase/path.rb', line 2

def root
  @root
end

Class Method Details

.index(paths) ⇒ Object



32
33
34
35
36
# File 'app/models/showcase/path.rb', line 32

def self.index(paths)
  paths.each_with_object new(:discardable_root) do |path, root|
    yield(path).reduce(root, :edge_for) << path
  end.children.sort_by(&:id).each { _1.root = true }
end

Instance Method Details

#active?(id) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/models/showcase/path.rb', line 20

def active?(id)
  children.any? { _1.active?(id) }
end

#edge_for(id) ⇒ Object



38
39
40
# File 'app/models/showcase/path.rb', line 38

def edge_for(id)
  find(id) || insert(id)
end

#nameObject



12
13
14
# File 'app/models/showcase/path.rb', line 12

def name
  id == "." ? "Previews" : id
end

#open?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'app/models/showcase/path.rb', line 16

def open?
  Showcase.tree_opens.call(self)
end

#ordered_childrenObject



24
25
26
# File 'app/models/showcase/path.rb', line 24

def ordered_children
  children.partition { !_1.is_a?(Tree) }.flatten
end

#ordered_pathsObject



28
29
30
# File 'app/models/showcase/path.rb', line 28

def ordered_paths
  children.flat_map { _1.is_a?(Tree) ? _1.ordered_paths : _1 }
end