Class: SublimeDSL::SublimeText::Macro

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

Defined Under Namespace

Classes: DSLReader

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Macro

Returns a new instance of Macro.



29
30
31
32
# File 'lib/sublime_dsl/sublime_text/macro.rb', line 29

def initialize(name)
  @name = name
  @commands = []
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



27
28
29
# File 'lib/sublime_dsl/sublime_text/macro.rb', line 27

def commands
  @commands
end

#nameObject (readonly) Also known as: to_s

Returns the value of attribute name.



26
27
28
# File 'lib/sublime_dsl/sublime_text/macro.rb', line 26

def name
  @name
end

Class Method Details

.import(file) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sublime_dsl/sublime_text/macro.rb', line 7

def self.import(file)
  # Delete to Hard EOL.sublime-macro:
  # [
  #     {"command": "move_to", "args": {"to": "hardeol", "extend": true}},
  #     {"command": "add_to_kill_ring", "args": {"forward": true}},
  #     {"command": "right_delete"}
  # ]
  macro = new(File.basename(file, '.sublime-macro'))
  JSON[File.read(file, encoding: 'utf-8')].each do |command_hash|
    cmd = command_hash.delete('command')
    cmd or raise Error, "no 'command' key in '#{file}'"
    args = command_hash.delete('args')
    command_hash.empty? or raise Error, 'unknown sublime-macro keys: ' << command_hash.inspect
    macro.commands << Command.new(cmd, args)
  end

  macro
end

Instance Method Details

#export(dir) ⇒ Object



47
48
49
50
# File 'lib/sublime_dsl/sublime_text/macro.rb', line 47

def export(dir)
  file = "#{dir}/#{name}.sublime-macro"
  File.open(file, 'wb:utf-8') { |f| f.write to_json }
end

#to_dslObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/sublime_dsl/sublime_text/macro.rb', line 36

def to_dsl
  # macro 'Delete to Hard EOL' do
  #   move_to "hardeol", extend: true
  #   add_to_kill_ring forward: true
  #   right_delete
  # end
  dsl = "macro #{name.inspect} do\n"
  commands.each { |c| dsl << "  #{c.to_dsl(true)}\n" }
  dsl << "end"
end

#to_jsonObject



52
53
54
55
56
57
# File 'lib/sublime_dsl/sublime_text/macro.rb', line 52

def to_json
  # JSON.pretty_generate(commands.map(&:to_h))
  "[\n" <<
    commands.map { |c| JSON.generate(c.to_h) }.join(",\n").indent(2) <<
  "\n]"
end