Class: PSD::Node::Root

Inherits:
PSD::Node show all
Includes:
HasChildren, ParseLayers
Defined in:
lib/psd/node_root.rb

Overview

Represents the root node of a Photoshop document

Constant Summary

Constants inherited from PSD::Node

PROPERTIES

Instance Attribute Summary collapse

Attributes inherited from PSD::Node

#parent

Instance Method Summary collapse

Methods included from ParseLayers

#parse_layers

Methods included from HasChildren

#groups, #layers

Methods inherited from PSD::Node

#group?, #hidden?, #layer?, #visible?

Methods included from Search

#children_at_path

Methods included from Ancestry

#ancestors, #childless?, #descendants, #has_children?, #has_siblings?, #method_missing, #only_child?, #root, #root?, #siblings, #subtree

Constructor Details

#initialize(psd) ⇒ Root

Stores a reference to the parsed PSD and builds the tree hierarchy.



13
14
15
16
# File 'lib/psd/node_root.rb', line 13

def initialize(psd)
  @psd = psd
  build_hierarchy
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PSD::Node::Ancestry

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/psd/node_root.rb', line 9

def children
  @children
end

Instance Method Details

#depthObject

The depth of the root node is always 0.



50
51
52
# File 'lib/psd/node_root.rb', line 50

def depth
  0
end

#document_dimensionsObject

Returns the width and height of the entire PSD document.



30
31
32
# File 'lib/psd/node_root.rb', line 30

def document_dimensions
  [@psd.header.width, @psd.header.height]
end

#document_heightObject

The height of the full PSD document as defined in the header.



40
41
42
# File 'lib/psd/node_root.rb', line 40

def document_height
  @psd.header.height.to_i
end

#document_widthObject

The width of the full PSD document as defined in the header.



35
36
37
# File 'lib/psd/node_root.rb', line 35

def document_width
  @psd.header.width.to_i
end

#nameObject

The root node has no name since it’s not an actual layer or group.



45
46
47
# File 'lib/psd/node_root.rb', line 45

def name
  nil
end

#to_hashObject

Recursively exports the hierarchy to a Hash



19
20
21
22
23
24
25
26
27
# File 'lib/psd/node_root.rb', line 19

def to_hash
  {
    children: children.map(&:to_hash),
    document: {
      width: document_width,
      height: document_height
    }
  }
end