Class: Warg::LazilyFilteredHostCollection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HostCollection::Interaction

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

Constructor Details

#initialize(hosts) ⇒ LazilyFilteredHostCollection

Returns a new instance of LazilyFilteredHostCollection.



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

def initialize(hosts)
  @hosts = hosts
  @filters = {}
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



1229
1230
1231
# File 'lib/warg.rb', line 1229

def filters
  @filters
end

Class Method Details

.from(value) ⇒ Object



1231
1232
1233
# File 'lib/warg.rb', line 1231

def self.from(value)
  new HostCollection.from(value)
end

Instance Method Details

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



1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
# File 'lib/warg.rb', line 1267

def ==(other)
  case other
  when HostCollection, LazilyFilteredHostCollection
    to_a == other.to_a
  when Enumerable
    to_a == other
  else
    super
  end
end

#add(host_data) ⇒ Object



1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
# File 'lib/warg.rb', line 1250

def add(host_data)
  host = Host.from(host_data)

  if host.matches?(**@filters)
    @hosts.add(host)
    self
  else
    false
  end
end

#eachObject



1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# File 'lib/warg.rb', line 1284

def each
  if block_given?
    @hosts.each do |host|
      if host.matches?(**@filters)
        yield host
      end
    end
  else
    enum_for(:each)
  end
end

#lengthObject



1280
1281
1282
# File 'lib/warg.rb', line 1280

def length
  count
end

#oneObject



1240
1241
1242
1243
1244
1245
1246
1247
1248
# File 'lib/warg.rb', line 1240

def one
  host = to_a.sample

  if host.nil?
    raise "no hosts matched `#{inspect}'"
  else
    HostCollection.from(host)
  end
end

#with(**filters) ⇒ Object



1261
1262
1263
1264
1265
# File 'lib/warg.rb', line 1261

def with(**filters)
  combined = @filters.merge(filters)

  HostCollection.new(@hosts.select { |host| host.matches?(combined) })
end