class SublimeDSL::SublimeText::Command

A command.

Attributes

args[R]
error[R]
name[R]

Public Class Methods

dsl_method(name) click to toggle source
# 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) click to toggle source
# 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
new(name, args, error = nil) click to toggle source
# 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
ruby_keyword_hash() click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

to_dsl(statement_style = false) click to toggle source
# 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
Also aliased as: to_s
to_h(command_key = 'command', args_key = 'args') click to toggle source
# 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
to_s(statement_style = false)
Alias for: to_dsl