Class: Caml::Command

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Command

Returns a new instance of Command.



33
34
35
36
37
38
39
40
# File 'lib/caml/caml.rb', line 33

def initialize(options = {})
  @name = options['name']
  @desc = options['desc']
  @aliases = options['aliases']
  @args = options['args']
  @opts = options['opts']
  @execute = options['execute']
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



31
32
33
# File 'lib/caml/caml.rb', line 31

def aliases
  @aliases
end

#argsObject (readonly)

Returns the value of attribute args.



31
32
33
# File 'lib/caml/caml.rb', line 31

def args
  @args
end

#descObject (readonly)

Returns the value of attribute desc.



31
32
33
# File 'lib/caml/caml.rb', line 31

def desc
  @desc
end

#executeObject (readonly)

Returns the value of attribute execute.



31
32
33
# File 'lib/caml/caml.rb', line 31

def execute
  @execute
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/caml/caml.rb', line 31

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



31
32
33
# File 'lib/caml/caml.rb', line 31

def opts
  @opts
end

Instance Method Details

#dispatcherObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/caml/caml.rb', line 42

def dispatcher
  if opts.empty?
    executor
  else
    first_clause = "if options[:#{opts.first.name.to_sym}]\nexecute { \"#{opts.first.execute}\" }\n"
    # else_clause = directive.execute.nil? ? "#\n" : "else\nexecute { \"#{wrap(directive.execute).join(';')}\" }\nend"
    # clauses = directive.options.drop(1).map { |option| "elsif options[:#{option.name.to_sym}]\nexecute { \"#{wrap(option.execute).join(';')}\" }\n" }.join
    # dispatcher = first_clause + clauses + else_clause
    puts first_clause
    executor
  end
end

#executorObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/caml/caml.rb', line 55

def executor
  if execute.respond_to?(:join)
<<-EOS
execute do
<<-COMMANDS
#{execute.join("\n")}
COMMANDS
end
EOS
  else
    "execute { \"#{execute}\" }\n"
  end
end

#to_sObject



69
70
71
72
73
74
75
# File 'lib/caml/caml.rb', line 69

def to_s
  descriptor = "desc '#{name}', '#{desc}'\n"
  options = opts.empty? ? '' : opts.join("\n") + "\n"
  signature = "def #{name}\n" # TODO: args
  close_scope = "end\n"
  descriptor + options + signature + executor + close_scope
end