Module: PSD::Node::Search

Included in:
PSD::Node
Defined in:
lib/psd/nodes/search.rb

Instance Method Summary collapse

Instance Method Details

#children_at_path(path, opts = {}) ⇒ Object Also known as: children_with_path

Searches the tree structure for a node at the given path. The path is defined by the layer/folder names. Because the PSD format does not require unique layer/folder names, we always return an array of all found nodes.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/psd/nodes/search.rb', line 8

def children_at_path(path, opts={})
  path = path.split('/').delete_if { |p| p == "" } unless path.is_a?(Array)

  query = path.shift
  matches = children.select do |c|
    if opts[:case_sensitive]
      c.name == query
    else
      c.name.downcase == query.downcase
    end
  end

  if path.length == 0
    return matches
  else
    return matches.map { |m| m.children_at_path(path, opts) }.flatten
  end
end

#filter_by_comp(id) ⇒ Object

Given a layer comp ID, name, or :last for last document state, create a new tree based on the layers/groups that belong to the comp only.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/psd/nodes/search.rb', line 30

def filter_by_comp(id)
  if id.is_a?(String)
    comp = psd.layer_comps.select { |c| c[:name] == id }.first
    raise "Layer comp not found" if comp.nil?

    id = comp[:id]
  elsif id == :last
    id = 0
  end

  root = PSD::Node::Root.new(psd)
  filter_for_comp!(id, root)

  return root
end