Class: PackRb::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/pack_rb/packer.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Packer

Returns a new instance of Packer.



6
7
8
9
10
# File 'lib/pack_rb/packer.rb', line 6

def initialize(opts = {})
  @machine_readable = opts[:machine_readable]
  @bin_path         = opts[:bin_path]
  @tpl              = opts[:tpl]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pack_rb/packer.rb', line 33

def method_missing(name, *args, &block)
  return super unless commander.respond_to?(name)

  opts = {
    base_cmd: command,
    tpl: template,
    args: args.first
  }

  commander.send(name, opts)
end

Instance Method Details

#binObject



20
21
22
# File 'lib/pack_rb/packer.rb', line 20

def bin
  bin_path || 'packer'
end

#commandObject



12
13
14
15
16
17
18
# File 'lib/pack_rb/packer.rb', line 12

def command
  if machine_readable
    "#{bin} -machine-readable"
  else
    bin
  end
end

#commanderObject



29
30
31
# File 'lib/pack_rb/packer.rb', line 29

def commander
  @commander ||= PackRb::SubCommands.new
end

#templateObject



24
25
26
27
# File 'lib/pack_rb/packer.rb', line 24

def template
  obj = @tpl
  json?(obj) || path?(obj) || hash?(obj)
end