Class: Rum::Docker::Options

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/rumrunner/docker.rb

Overview

Collection of Docker command options to be applied on execution.

Instance Method Summary collapse

Constructor Details

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

Initialize a new OPTIONS collection for Docker executable. Evaluates the &block if given.


106
107
108
109
# File 'lib/rumrunner/docker.rb', line 106

def initialize(options = {}, &block)
  @data = Hash.new{|hash, key| hash[key] = [] }.update(options)
  instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Missing methods are interpreted as options to be added to the underlying collection.

Example:

opts = Options.new
opts.fizz "buzz"
# => @data={:fizz=>["buzz"]}

120
121
122
123
# File 'lib/rumrunner/docker.rb', line 120

def method_missing(m, *args, &block)
  @data[m] += args unless args.empty?
  self
end

Instance Method Details

#eachObject

Yield each option as a CLI flag/option, with - or -- prefix.

Example:

opts = Options.new
opts.fizz "buzz"
opts.to_a
# => ["--fizz", "buzz"]

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rumrunner/docker.rb', line 134

def each
  @data.each do |name, values|
    option = flagify name
    values.each do |value|
      if value.is_a?(Hash)
        value.map{|kv| kv.join("=") }.each do |val|
          yield option
          yield val
        end
      elsif [true, false].include? value
        yield "#{option}=#{value}"
      else
        yield option
        yield value.to_s
      end
    end
  end
end

#to_sObject

Convert options to string.

Example:

opts = Options.new
opts.fizz "buzz"
opts.to_s
# => "--fizz buzz"

162
163
164
# File 'lib/rumrunner/docker.rb', line 162

def to_s
  to_a.join(" ")
end