Class: Command

Inherits:
Object
  • Object
show all
Defined in:
lib/YASP/command.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, args) ⇒ Command

Returns a new instance of Command.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/YASP/command.rb', line 3

def initialize(name, args)
  @label = name

  if (args.last.is_a?(Hash))
    @args = args[0...-1]
    @opts = args.last
  else
    @args = args
    @opts = {}
  end

  @children = []
end

Instance Method Details

#<<(child) ⇒ Object

TODO deferral mixin



17
18
19
20
# File 'lib/YASP/command.rb', line 17

def <<(child)#TODO deferral mixin
  #TODO what the hell does 'deferral mixin' mean? why was I so bad at coments?
  @children << child
end

#argstringObject



30
31
32
# File 'lib/YASP/command.rb', line 30

def argstring
  @args.map(&:inspect).join(", ").chomp(", ")
end

#indent(depth) ⇒ Object



38
39
40
# File 'lib/YASP/command.rb', line 38

def indent(depth)
  "  "*depth
end

#labelObject



26
27
28
# File 'lib/YASP/command.rb', line 26

def label
  @label
end

#optsObject



22
23
24
# File 'lib/YASP/command.rb', line 22

def opts
  @opts
end

#optstringObject



34
35
36
# File 'lib/YASP/command.rb', line 34

def optstring
  @opts.map { |key, value| "#{key}=#{value.inspect}"}.join(", ").chomp(", ")
end

#parse_argumentsObject



42
43
44
45
46
47
48
49
50
# File 'lib/YASP/command.rb', line 42

def parse_arguments
  if @opts.empty?
    argstring
  elsif @args.empty?
    optstring
  else
    "#{argstring}, #{optstring}"
  end
end

#parse_children(depth) ⇒ Object



52
53
54
# File 'lib/YASP/command.rb', line 52

def parse_children(depth)
  @children.map{ |c| c.to_scad(depth) }.join()
end

#parse_nested_children(depth) ⇒ Object



56
57
58
59
60
# File 'lib/YASP/command.rb', line 56

def parse_nested_children(depth)
  unless @children.empty?
    "{\n#{parse_children(depth)}#{indent(depth-1)}}"
  end
end

#to_scad(depth) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/YASP/command.rb', line 62

def to_scad(depth)
  if label
    "#{indent(depth)}#{label}(#{parse_arguments})#{parse_nested_children(depth+1)};\n"
  else
    parse_children(depth)
  end
end