Class: SublimeDSL::SublimeText::Command

Inherits:
Object
  • Object
show all
Includes:
Tools::ValueEquality
Defined in:
lib/sublime_dsl/sublime_text/command.rb

Overview

A command.

Defined Under Namespace

Classes: Argument

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tools::ValueEquality

#eql?, #hash, #value_id

Constructor Details

#initialize(name, args, error = nil) ⇒ Command

Returns a new instance of Command.



63
64
65
66
67
68
69
70
71
72
# File 'lib/sublime_dsl/sublime_text/command.rb', line 63

def initialize(name, args, error = nil)
  @name = name
  @args =
    if error
      args
    else
      args ? args.to_a.map { |n,v| Argument.new(n,v) } : []
    end
  @error = error
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



61
62
63
# File 'lib/sublime_dsl/sublime_text/command.rb', line 61

def args
  @args
end

#errorObject (readonly)

Returns the value of attribute error.



61
62
63
# File 'lib/sublime_dsl/sublime_text/command.rb', line 61

def error
  @error
end

#nameObject (readonly)

Returns the value of attribute name.



61
62
63
# File 'lib/sublime_dsl/sublime_text/command.rb', line 61

def name
  @name
end

Class Method Details

.dsl_method(name) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/sublime_dsl/sublime_text/command.rb', line 28

def self.dsl_method(name)
  if ruby_keyword_hash[name]
    '_' << name
  elsif name.start_with?('$')
    '_dollar_' << name[1..-1]
  else
    name
  end
end

.from_method_missing(sym, args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sublime_dsl/sublime_text/command.rb', line 11

def self.from_method_missing(sym, args)
  method = sublime_method(sym.to_s)
  last_word = method.split('_').last
  case args.length
  when 0
  when 1
    args = args.first.is_a?(Hash) ? args.first : { last_word => args.first }
  when 2
    args.last.is_a?(Hash) or
      return new(sym, args, "invalid arguments to #{method}: #{cmd.args.inspect}")
    args = { last_word => args.first }.merge(args.last)
  else
    return new(sym, args, "invalid arguments to #{method}: #{cmd.args.inspect}")
  end
  new(method, args)
end

.ruby_keyword_hashObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/sublime_dsl/sublime_text/command.rb', line 50

def self.ruby_keyword_hash
  @ruby_keyword_hash ||= Hash[
    %w(
      alias and BEGIN begin break case class def defined? do
      else elsif END end ensure false for if in module
      next nil not or redo rescue retry return self super
      then true undef unless until when while yield
    ).map { |w| [w, true] }
  ]
end

.sublime_method(name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sublime_dsl/sublime_text/command.rb', line 38

def self.sublime_method(name)
  return name unless name.start_with?('_')
  m = name[1..-1]
  if m.start_with?('dollar_')
    '$' << m[7..-1]
  elsif ruby_keyword_hash[m]
    m
  else
    name
  end
end

Instance Method Details

#to_dsl(statement_style = false) ⇒ Object Also known as: to_s



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sublime_dsl/sublime_text/command.rb', line 84

def to_dsl(statement_style = false)
  method = Command.dsl_method(name)
  return method if args.empty?
  last = name.split('_').last
  if last == args.first.name
    arg_list = args.first.to_s(false)
    arg_list << ', ' << args[1..-1].map(&:to_s).join(', ') if args.length > 1
  else
    arg_list = args.map(&:to_s).join(', ')
  end
  statement_style ? "#{method} #{arg_list}" : "#{method}(#{arg_list})"
end

#to_h(command_key = 'command', args_key = 'args') ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/sublime_dsl/sublime_text/command.rb', line 74

def to_h(command_key = 'command', args_key = 'args')
  if args.empty?
    { command_key => name }
  else
    args_hash = {}
    args.each { |a| args_hash[a.name] = a.value }
    { command_key => name, args_key => args_hash }
  end
end