Class: EasyType::ScriptBuilder
- Inherits:
-
Object
- Object
- EasyType::ScriptBuilder
- 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
-
#acceptable_commands ⇒ Object
readonly
Returns the value of attribute acceptable_commands.
-
#binding ⇒ Object
readonly
Returns the value of attribute binding.
Instance Method Summary collapse
- #<<(line) ⇒ Object
- #add(line = nil, command = default_command, options = {}, &block) ⇒ Object
- #after(line = nil, command = default_command, options = {}, &block) ⇒ Object
- #append(line = nil, &block) ⇒ Object
- #before(line = nil, command = default_command, options = {}, &block) ⇒ Object
- #default_command ⇒ Object
- #entries(type = :main) ⇒ Object
- #execute ⇒ Object
-
#initialize(options = {}, &block) ⇒ ScriptBuilder
constructor
A new instance of ScriptBuilder.
- #last_command(type = :main) ⇒ Object
-
#line ⇒ Object
For backward compatibility.
-
#line=(line) ⇒ Object
For backward compatibility.
- #results(type = :main) ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ ScriptBuilder
Returns a new instance of ScriptBuilder.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/easy_type/script_builder.rb', line 16 def initialize( = {}, &block) @entries ||= [] @acceptable_commands = Array(.fetch(:acceptable_commands) { [] }) @binding = [: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_commands ⇒ Object (readonly)
Returns the value of attribute acceptable_commands.
14 15 16 |
# File 'lib/easy_type/script_builder.rb', line 14 def acceptable_commands @acceptable_commands end |
#binding ⇒ Object (readonly)
Returns the value of attribute binding.
14 15 16 |
# File 'lib/easy_type/script_builder.rb', line 14 def binding @binding end |
Instance Method Details
#<<(line) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/easy_type/script_builder.rb', line 60 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
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/easy_type/script_builder.rb', line 46 def add(line=nil, command = default_command, = {}, &block) if command.is_a?(Hash) # Probably called without a command. So the command entry is the options Hash = command command = default_command end if block add_to_queue(:main, line, command, , &block) else entries(:main) << CommandEntry.new(command, line, ) unless line.nil? # special case end nil end |
#after(line = nil, command = default_command, options = {}, &block) ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/easy_type/script_builder.rb', line 109 def after(line = nil, command = default_command, = {}, &block) if command.is_a?(Hash) # Probably called without a command. So the command entry is the options Hash = command command = default_command end add_to_queue(:after, line, command, , &block) nil end |
#append(line = nil, &block) ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/easy_type/script_builder.rb', line 88 def append(line = nil, &block) last_argument = last_command.arguments.pop if block new_argument = last_argument + @context.instance_eval(&block) else new_argument = last_argument + line end last_command.arguments << new_argument nil end |
#before(line = nil, command = default_command, options = {}, &block) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'lib/easy_type/script_builder.rb', line 99 def before(line = nil, command = default_command, = {}, &block) if command.is_a?(Hash) # Probably called without a command. So the command entry is the options Hash = command command = default_command end add_to_queue(:before, line, command, , &block) nil end |
#default_command ⇒ Object
38 39 40 |
# File 'lib/easy_type/script_builder.rb', line 38 def default_command @acceptable_commands.first || '' end |
#entries(type = :main) ⇒ Object
34 35 36 |
# File 'lib/easy_type/script_builder.rb', line 34 def entries(type = :main) @context.entries[type] end |
#execute ⇒ Object
119 120 121 122 |
# File 'lib/easy_type/script_builder.rb', line 119 def execute @context.execute results end |
#last_command(type = :main) ⇒ Object
42 43 44 |
# File 'lib/easy_type/script_builder.rb', line 42 def last_command(type = :main) entries(type).last end |
#line ⇒ Object
For backward compatibility
71 72 73 74 75 76 |
# File 'lib/easy_type/script_builder.rb', line 71 def line catch(:no_last_commands) do check_last_command last_command.arguments.join(' ') end end |
#line=(line) ⇒ Object
For backward compatibility
80 81 82 83 84 85 86 |
# File 'lib/easy_type/script_builder.rb', line 80 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
124 125 126 |
# File 'lib/easy_type/script_builder.rb', line 124 def results(type = :main) @context.results[type].join("\n") end |