Class: SublimeDSL::SublimeText::MouseMap::MouseBinding

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

Overview

A mouse binding: a click with its command(s).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(click, count, press_command, command) ⇒ MouseBinding

Returns a new instance of MouseBinding.



92
93
94
95
96
97
# File 'lib/sublime_dsl/sublime_text/mousemap.rb', line 92

def initialize(click, count, press_command, command)
  @click = click
  @count = count
  @press_command = press_command
  @command = command
end

Instance Attribute Details

#clickObject (readonly)

Returns the value of attribute click.



89
90
91
# File 'lib/sublime_dsl/sublime_text/mousemap.rb', line 89

def click
  @click
end

#commandObject (readonly)

Returns the value of attribute command.



89
90
91
# File 'lib/sublime_dsl/sublime_text/mousemap.rb', line 89

def command
  @command
end

#countObject (readonly)

Returns the value of attribute count.



89
90
91
# File 'lib/sublime_dsl/sublime_text/mousemap.rb', line 89

def count
  @count
end

#press_commandObject (readonly)

Returns the value of attribute press_command.



89
90
91
# File 'lib/sublime_dsl/sublime_text/mousemap.rb', line 89

def press_command
  @press_command
end

#source_fileObject

Returns the value of attribute source_file.



90
91
92
# File 'lib/sublime_dsl/sublime_text/mousemap.rb', line 90

def source_file
  @source_file
end

Class Method Details

.from_json(json_hash) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sublime_dsl/sublime_text/mousemap.rb', line 63

def self.from_json(json_hash)
  h = json_hash.dup

  button = h.delete('button') or raise Error, 'no button: ' << json_hash.inspect
  modifiers = h.delete('modifiers')
  spec = [*modifiers, button].join('+')
  click = Mouse.sublime.ensure_click(spec)

  count = h.delete('count')
  count = count.to_i if count

  press = h.delete('press_command')
  press_command = press ? Command.new(press, h.delete('press_args')) : nil

  cmd = h.delete('command')
  command = cmd ? Command.new(cmd, h.delete('args')) : nil

  h.empty? or raise Error, 'unexpected JSON keys: ' << h.inspect
  new(click, count, press_command, command)

rescue => ex
  warn "error with binding #{json_hash.inspect}"
  warn ex.message
  raise
end

Instance Method Details

#to_dslObject



99
100
101
102
103
104
105
106
# File 'lib/sublime_dsl/sublime_text/mousemap.rb', line 99

def to_dsl
  spec = click.to_spec
  dsl = "click#{count} #{spec.to_source}"
  dsl << ", down: #{press_command.to_dsl}" if press_command
  dsl << ", up: #{command.to_dsl}" if command

  dsl
end

#to_hObject



108
109
110
111
112
113
114
# File 'lib/sublime_dsl/sublime_text/mousemap.rb', line 108

def to_h
  h = click.to_h
  h['count'] = count if count
  h.merge! press_command.to_h('press_command', 'press_args') if press_command
  h.merge! command.to_h if command
  h
end