Class: Cat::SkeletonNode

Inherits:
Object
  • Object
show all
Defined in:
lib/cat/skeleton_node.rb

Constant Summary collapse

INDENT_SYMBOL =
"\t"

Instance Method Summary collapse

Constructor Details

#initialize(name = "cat", &block) ⇒ SkeletonNode

Returns a new instance of SkeletonNode.



5
6
7
8
9
10
11
# File 'lib/cat/skeleton_node.rb', line 5

def initialize(name = "cat", &block)
  @nodes = []
  @class = name
  @body = nil
  @skin = SkinNode.new(name)
  instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



41
42
43
44
# File 'lib/cat/skeleton_node.rb', line 41

def method_missing(name, *args, &block)
  return @skin.send(name, *args, &block) if @skin.methods.include?(name)
  super
end

Instance Method Details

#bodyObject



13
14
15
16
# File 'lib/cat/skeleton_node.rb', line 13

def body
  return if @body.nil?
  " #{@body}"
end

#expand(indent = 2) ⇒ Object



34
35
36
37
38
39
# File 'lib/cat/skeleton_node.rb', line 34

def expand(indent = 2)
  [
    INDENT_SYMBOL * indent + to_s,
    @nodes.map { |node| node.expand(indent + 1) }.join("\n")
  ].join("\n")
end

#node(name, &block) ⇒ Object



22
23
24
# File 'lib/cat/skeleton_node.rb', line 22

def node(name, &block)
  @nodes << SkeletonNode.new(name, &block)
end

#skin(&block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/cat/skeleton_node.rb', line 26

def skin(&block)
  if block_given?
    @skin.instance_eval(&block)
  else
    @skin.expand(with: @nodes)
  end
end

#to_sObject



18
19
20
# File 'lib/cat/skeleton_node.rb', line 18

def to_s
  ".#{@class}#{body}"
end