class SublimeDSL::SublimeText::Menu::Item

Attributes

caption[RW]
checkbox[RW]
command[RW]
id[RW]
items[R]
mnemonic[RW]
platform[RW]

Public Class Methods

from_json(json_hash) click to toggle source
# File lib/sublime_dsl/sublime_text/menu.rb, line 52
def self.from_json(json_hash)
  h = json_hash.dup
  item = Item.new.tap do |i|
    i.caption = h.delete('caption')
    i.mnemonic = h.delete('mnemonic')
    cmd = h.delete('command')
    args = h.delete('args')
    i.command = Command.new(cmd, args) if cmd
    i.id = h.delete('id')
    i.checkbox = h.delete('checkbox')
    i.platform = h.delete('platform')
  end
  children = h.delete('children') || []
  children.each do |c|
    item.items << Item.from_json(c)
  end
  h.empty? or warn "unkown keys ignored: #{h.inspect}"

  item
end
new() click to toggle source
# File lib/sublime_dsl/sublime_text/menu.rb, line 76
def initialize()
  @command = nil
  @caption = nil
  @mnemonic = nil
  @id = nil
  @checkbox = nil
  @platform = nil
  @items = []
end

Public Instance Methods

to_dsl(indent = ' ') click to toggle source
# File lib/sublime_dsl/sublime_text/menu.rb, line 86
def to_dsl(indent = '  ')
  args = ''
  options = []

  if caption
    cap = caption.gsub('&', '&&')
    if mnemonic
      if cap =~ /^(.*?)(#{mnemonic})(.*)$/i
        args << "#{$1}&#{$2}#{$3}".to_source
      else
        args << cap.to_source
        options << "mnemonic: #{mnemonic.to_source}"
      end
    else
      args << cap.to_source
    end
  end

  if command
    args << ', ' unless args.empty?
    args << command.to_dsl
    options << "mnemonic: #{mnemonic.to_source}" if caption.nil? && mnemonic
  end

  options << "id: #{id.to_source}" if id
  options << "checkbox: true" if checkbox
  options << "platform: #{platform.to_source}" if platform

  dsl = "#{indent}item #{args}"
  unless options.empty?
    dsl << ', ' unless args.empty?
    dsl << options.join(', ')
  end

  unless items.empty?
    i = indent + '  '
    dsl << " do\n"
    items.each do |c|
      dsl << c.to_dsl(i) << "\n"
    end
    dsl << "#{indent}end"
  end

  dsl
end
to_h(include_items = true) click to toggle source
# File lib/sublime_dsl/sublime_text/menu.rb, line 132
def to_h(include_items = true)
  h = {}
  h['caption'] = caption if caption
  h['mnemonic'] = mnemonic if mnemonic
  h.merge! command.to_h if command
  h['id'] = id if id
  h['checkbox'] = checkbox if checkbox
  h['platform'] = platform if platform
  h['children'] = items.map(&:to_h) if include_items && !items.empty?
  h
end
to_json(indent) click to toggle source
# File lib/sublime_dsl/sublime_text/menu.rb, line 144
def to_json(indent)
  return indent + JSON.generate(to_h) if items.empty?
  json = indent + JSON.pretty_generate(to_h(false))
  json = json[0..-3] # remove trailing "\n}"
  json.gsub!("\n", "\n#{indent}")
  json << %(,\n#{indent}  "children": [\n)
  ind = indent + '    '
  json << items.map { |i| i.to_json(ind) }.join(",\n")
  json << "\n#{indent}  ]\n#{indent}}"
end