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

#force_visible, #layer, #left, #parent, #top

Instance Method Summary collapse

Methods included from ParseLayers

#parse_layers

Methods included from HasChildren

#groups, #layers

Methods inherited from PSD::Node

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

Methods included from BuildPreview

#renderer, #save_as_png, #to_png

Methods included from Search

#children_at_path, #filter_by_comp

Methods included from Ancestry

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

Constructor Details

#initialize(psd) ⇒ Root

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



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

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

Returns the value of attribute children.



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

def children
  @children
end

#psdObject (readonly)

Returns the value of attribute psd.



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

def psd
  @psd
end

Instance Method Details

#depthObject

The depth of the root node is always 0.



58
59
60
# File 'lib/psd/node_root.rb', line 58

def depth
  0
end

#document_dimensionsObject

Returns the width and height of the entire PSD document.



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

def document_dimensions
  [document_width, document_height]
end

#document_heightObject Also known as: height

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



47
48
49
# File 'lib/psd/node_root.rb', line 47

def document_height
  @psd.header.height.to_i
end

#document_widthObject Also known as: width

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



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

def document_width
  @psd.header.width.to_i
end

#fill_opacityObject



67
# File 'lib/psd/node_root.rb', line 67

def fill_opacity; 255; end

#nameObject

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



53
54
55
# File 'lib/psd/node_root.rb', line 53

def name
  nil
end

#opacityObject



66
# File 'lib/psd/node_root.rb', line 66

def opacity; 255; end

#to_hashObject

Recursively exports the hierarchy to a Hash



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/psd/node_root.rb', line 20

def to_hash
  {
    children: children.map(&:to_hash),
    document: {
      width: document_width,
      height: document_height,
      resources: {
        layer_comps: @psd.layer_comps,
        guides: @psd.guides,
        slices: @psd.slices
      }
    }
  }
end