Class: EasyType::ScriptBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_type/script_builder.rb

Overview

The command builder is a class that helps building an OS command sequence. You can specify a base command on creation. With the << you can add parameters and options in the order you would like. Sometimes you need to execute commands before and/or after the base command. The ScriptBuilder supports this by using the ‘before` and `after` methods

rubocop:disable ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ ScriptBuilder

Returns a new instance of ScriptBuilder.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/easy_type/script_builder.rb', line 23

def initialize(options = {}, &block)
  @entries ||= []
  @acceptable_commands = Array(options.fetch(:acceptable_commands) { [] })
  @binding = options[:binding]
  @context = BlankSlate.new
  if @binding
    CommandEntry.set_binding(@binding)
    copy_resource_info
  end
  @acceptable_commands.each do |command|
    @context.eigenclass.send(:define_method, command) do |*args|
      @entries[type] << CommandEntry.new(command, args)
    end
  end
  @context.type = :main
  @context.instance_eval(&block) if block
end

Instance Attribute Details

#acceptable_commandsObject (readonly)

Returns the value of attribute acceptable_commands.



21
22
23
# File 'lib/easy_type/script_builder.rb', line 21

def acceptable_commands
  @acceptable_commands
end

#bindingObject (readonly)

Returns the value of attribute binding.



21
22
23
# File 'lib/easy_type/script_builder.rb', line 21

def binding
  @binding
end

Instance Method Details

#<<(line) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/easy_type/script_builder.rb', line 67

def <<(line)
  catch(:no_last_commands) do
    check_last_command
    last_command.arguments << line if line
  end
  nil
end

#add(line = nil, command = default_command, options = {}, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/easy_type/script_builder.rb', line 53

def add(line = nil, command = default_command, options = {}, &block)
  if command.is_a?(Hash)
    # Probably called without a command. So the command entry is the options Hash
    options = command
    command = default_command
  end
  if block
    add_to_queue(:main, line, command, options, &block)
  else
    entries(:main) << CommandEntry.new(command, line, options) unless line.nil? # special case
  end
  nil
end

#after(line = nil, command = default_command, options = {}, &block) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/easy_type/script_builder.rb', line 117

def after(line = nil, command = default_command, options = {}, &block)
  if command.is_a?(Hash)
    # Probably called without a command. So the command entry is the options Hash
    options = command
    command = default_command
  end
  add_to_queue(:after, line, command, options, &block)
  nil
end

#append(line = nil, &block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/easy_type/script_builder.rb', line 96

def append(line = nil, &block)
  last_argument = last_command.arguments.pop
  new_argument = if block
                   last_argument + @context.instance_eval(&block)
                 else
                   last_argument + line
                 end
  last_command.arguments << new_argument
  nil
end

#before(line = nil, command = default_command, options = {}, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/easy_type/script_builder.rb', line 107

def before(line = nil, command = default_command, options = {}, &block)
  if command.is_a?(Hash)
    # Probably called without a command. So the command entry is the options Hash
    options = command
    command = default_command
  end
  add_to_queue(:before, line, command, options, &block)
  nil
end

#default_commandObject



45
46
47
# File 'lib/easy_type/script_builder.rb', line 45

def default_command
  @acceptable_commands.first || ''
end

#entries(type = :main) ⇒ Object



41
42
43
# File 'lib/easy_type/script_builder.rb', line 41

def entries(type = :main)
  @context.entries[type]
end

#executeObject



127
128
129
130
# File 'lib/easy_type/script_builder.rb', line 127

def execute
  @context.execute
  results
end

#last_command(type = :main) ⇒ Object



49
50
51
# File 'lib/easy_type/script_builder.rb', line 49

def last_command(type = :main)
  entries(type).last
end

#lineObject

For backward compatibility



78
79
80
81
82
83
# File 'lib/easy_type/script_builder.rb', line 78

def line
  catch(:no_last_commands) do
    check_last_command
    last_command.arguments.join(' ')
  end
end

#line=(line) ⇒ Object

For backward compatibility



88
89
90
91
92
93
94
# File 'lib/easy_type/script_builder.rb', line 88

def line=(line)
  catch(:no_last_commands) do
    check_last_command
    last_command.arguments.clear
    last_command.arguments << line.split(' ')
  end
end

#results(type = :main) ⇒ Object



132
133
134
# File 'lib/easy_type/script_builder.rb', line 132

def results(type = :main)
  @context.results[type].join("\n")
end