Class: BoPeep::HostCollection

Inherits:
Object
  • Object
show all
Includes:
Interaction, Enumerable
Defined in:
lib/bopeep.rb

Defined Under Namespace

Modules: Interaction

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Interaction

#create_file_from, #download, #run, #run_command, #run_script, #upload

Constructor Details

#initializeHostCollection

Returns a new instance of HostCollection.



1222
1223
1224
# File 'lib/bopeep.rb', line 1222

def initialize
  @hosts = []
end

Class Method Details

.from(value) ⇒ Object



1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
# File 'lib/bopeep.rb', line 1181

def self.from(value)
  case value
  when String, Host
    new.add(value)
  when HostCollection
    value
  when Array
    is_array_host_specification = value.any? do |item|
      # Check key=value items by looking for `=` missing `?`.  If it has `?`, then we
      # assume it is in the form `host?key=value`
      String === item and item.index("=") and not item.index("?")
    end

    if is_array_host_specification
      new.add(value)
    else
      value.inject(new) { |collection, host_data| collection.add(host_data) }
    end
  when Hash
    value.inject(new) do |collection, (property, hosts_data)|
      name, value = property.to_s.split(":", 2)

      if value.nil?
        value = name
        name = "stage"
      end

      from(hosts_data).each do |host|
        host[name] = value
        collection.add(host)
      end

      collection
    end
  when nil
    new
  else
    raise InvalidHostDataError.new(value)
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



1244
1245
1246
1247
1248
1249
1250
# File 'lib/bopeep.rb', line 1244

def ==(other)
  self.class == other.class &&
    length == other.length &&
    # both are the same length and their intersection is the same length (all the same
    # elements in common)
    length == @hosts.&(other.hosts).length
end

#add(host_data) ⇒ Object



1234
1235
1236
1237
1238
# File 'lib/bopeep.rb', line 1234

def add(host_data)
  @hosts << Host.from(host_data)

  self
end

#eachObject



1262
1263
1264
1265
1266
1267
1268
1269
1270
# File 'lib/bopeep.rb', line 1262

def each
  if block_given?
    @hosts.each { |host| yield host }
  else
    enum_for(:each)
  end

  self
end

#lazyObject



1258
1259
1260
# File 'lib/bopeep.rb', line 1258

def lazy
  LazilyFilteredHostCollection.new(self)
end

#lengthObject



1254
1255
1256
# File 'lib/bopeep.rb', line 1254

def length
  @hosts.length
end

#oneObject



1226
1227
1228
1229
1230
1231
1232
# File 'lib/bopeep.rb', line 1226

def one
  if @hosts.empty?
    raise "cannot pick a host from `#{inspect}'; collection is empty"
  end

  HostCollection.from @hosts.sample
end

#to_aObject



1312
1313
1314
# File 'lib/bopeep.rb', line 1312

def to_a
  @hosts.dup
end

#with(**filters) ⇒ Object



1240
1241
1242
# File 'lib/bopeep.rb', line 1240

def with(**filters)
  HostCollection.from(select { |host| host.matches?(**filters) })
end