Class: SublimeDSL::SublimeText::Menu::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/sublime_dsl/sublime_text/menu.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeItem

Returns a new instance of Item.



76
77
78
79
80
81
82
83
84
# 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

Instance Attribute Details

#captionObject

Returns the value of attribute caption.



73
74
75
# File 'lib/sublime_dsl/sublime_text/menu.rb', line 73

def caption
  @caption
end

#checkboxObject

Returns the value of attribute checkbox.



73
74
75
# File 'lib/sublime_dsl/sublime_text/menu.rb', line 73

def checkbox
  @checkbox
end

#commandObject

Returns the value of attribute command.



73
74
75
# File 'lib/sublime_dsl/sublime_text/menu.rb', line 73

def command
  @command
end

#idObject

Returns the value of attribute id.



73
74
75
# File 'lib/sublime_dsl/sublime_text/menu.rb', line 73

def id
  @id
end

#itemsObject (readonly)

Returns the value of attribute items.



74
75
76
# File 'lib/sublime_dsl/sublime_text/menu.rb', line 74

def items
  @items
end

#mnemonicObject

Returns the value of attribute mnemonic.



73
74
75
# File 'lib/sublime_dsl/sublime_text/menu.rb', line 73

def mnemonic
  @mnemonic
end

#platformObject

Returns the value of attribute platform.



73
74
75
# File 'lib/sublime_dsl/sublime_text/menu.rb', line 73

def platform
  @platform
end

Class Method Details

.from_json(json_hash) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

Instance Method Details

#to_dsl(indent = ' ') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# 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) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# 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) ⇒ Object



144
145
146
147
148
149
150
151
152
153
# 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