Module: Bcome::Selections

Included in:
Bcome::Stack::Environment
Defined in:
lib/helpers/selections.rb

Instance Method Summary collapse

Instance Method Details

#add(resource_identifier = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/helpers/selections.rb', line 49

def add(resource_identifier = nil)
  if resource_identifier.is_a?(Array)
    resource_identifier.each do |ri|
      manage_object(:add_object, ri)
    end
  else
    manage_object(:add_object, resource_identifier)
  end
end

#add!Object



69
70
71
72
# File 'lib/helpers/selections.rb', line 69

def add!
  @objects = resources
  puts "All nodes added".informational
end

#add_object(object) ⇒ Object



87
88
89
90
91
# File 'lib/helpers/selections.rb', line 87

def add_object(object)
  return if object_in_selections?(object)
  @objects = @objects ? (@objects << object) : [object]
  return
end

#clear!Object



74
75
76
77
# File 'lib/helpers/selections.rb', line 74

def clear!
  @objects = []
  selections
end

#find_resource_by_identifier(resource_identifier) ⇒ Object



79
80
81
# File 'lib/helpers/selections.rb', line 79

def find_resource_by_identifier(resource_identifier)
  resources.select{|r| r.identifier == resource_identifier }.first
end

#in_resources?(object) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/helpers/selections.rb', line 83

def in_resources?(object)
  resources.include?(object)
end

#manage_object(method, resource_identifier = nil) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/helpers/selections.rb', line 3

def manage_object(method, resource_identifier = nil)
  unless resource_identifier && resource_identifier.is_a?(String)
    print "You must select a resource identified by its name. #{available_resources_options_string}".headsup
  else
    object = find_resource_by_identifier(resource_identifier)
    unless object
      print "Cannot find object with identifier #{resource_identifier} within the resources for this collection. #{available_resources_options_string}".failure unless object
    else
      send(method, object)
    end
  end
end

#no_selections_errorObject



116
117
118
# File 'lib/helpers/selections.rb', line 116

def no_selections_error 
  puts "No nodes selected".informational
end

#object_in_selections?(object) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
# File 'lib/helpers/selections.rb', line 103

def object_in_selections?(object)
  return false unless @objects
  return @objects.include?(object)
end

#remove(resource_identifier = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/helpers/selections.rb', line 59

def remove(resource_identifier = nil)
  if resource_identifier.is_a?(Array)
    resource_identifier.each do |ri|
      manage_object(:remove_object, ri)
    end
  else
    manage_object(:remove_object, resource_identifier)
  end
end

#remove_object(object) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/helpers/selections.rb', line 93

def remove_object(object)
  return if !@objects || @objects.empty?
  unless object_in_selections?(object)
    print "\n#{object.identifier} is not within selections\n".headsup
  else
    @objects = @objects - [object]
  end
  return
end

#rsync(local_path, remote_path) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/helpers/selections.rb', line 25

def rsync(local_path, remote_path)
  if !@objects || @objects.empty?
    no_selections_error
    return  
  end
  @objects.each do |object|
    object.rsync(local_path, remote_path)
  end
  return
end

#run(raw_commands) ⇒ Object

Runs commands over every object in the selection



17
18
19
20
21
22
23
# File 'lib/helpers/selections.rb', line 17

def run(raw_commands)
  return unless @objects
  @objects.each do |object|
    object.run(raw_commands)
  end
  return
end

#scp(files, remote_path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/helpers/selections.rb', line 36

def scp(files, remote_path)
  if !@objects || @objects.empty?
    no_selections_error
    return
  end

  return unless @objects
  @objects.each do |object|
    object.scp(files, remote_path)
  end 
  return
end

#selectionsObject



108
109
110
111
112
113
114
# File 'lib/helpers/selections.rb', line 108

def selections
  if !@objects || @objects.empty?
    print "\nNo added nodes.\n".headsup
  else
    print "\nAdded nodes: #{@objects.collect(&:identifier).join(", ")}\n".headsup
  end
end