Class: Warg::HostCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Interaction
Defined in:
lib/warg.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.



1126
1127
1128
# File 'lib/warg.rb', line 1126

def initialize
  @hosts = []
end

Class Method Details

.from(value) ⇒ Object



1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
# File 'lib/warg.rb', line 1085

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?



1148
1149
1150
1151
1152
1153
1154
# File 'lib/warg.rb', line 1148

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



1138
1139
1140
1141
1142
# File 'lib/warg.rb', line 1138

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

  self
end

#eachObject



1166
1167
1168
1169
1170
1171
1172
1173
1174
# File 'lib/warg.rb', line 1166

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

  self
end

#lazyObject



1162
1163
1164
# File 'lib/warg.rb', line 1162

def lazy
  LazilyFilteredHostCollection.new(self)
end

#lengthObject



1158
1159
1160
# File 'lib/warg.rb', line 1158

def length
  @hosts.length
end

#oneObject



1130
1131
1132
1133
1134
1135
1136
# File 'lib/warg.rb', line 1130

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

  HostCollection.from @hosts.sample
end

#to_aObject



1216
1217
1218
# File 'lib/warg.rb', line 1216

def to_a
  @hosts.dup
end

#with(**filters) ⇒ Object



1144
1145
1146
# File 'lib/warg.rb', line 1144

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