Module: Bcome::Selections

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

Instance Method Summary collapse

Instance Method Details

#add(resource_identifier = nil) ⇒ Object



54
55
56
57
58
# File 'lib/helpers/selections.rb', line 54

def add(resource_identifier = nil)
  resource_identifier = [resource_identifier] unless resource_identifier.is_a?(Array)
  resource_identifier.each {|ri| manage_object(:add_object, ri) }
  puts "\n" + "#{resource_identifier.join(",")} added.".menu_item_cyan + "\n\nType 'selections' or 's' to review your selections.'".menu_item_white
end

#add!Object



66
67
68
69
# File 'lib/helpers/selections.rb', line 66

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

#add_object(object) ⇒ Object



84
85
86
87
88
# File 'lib/helpers/selections.rb', line 84

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

#clear!Object



71
72
73
74
# File 'lib/helpers/selections.rb', line 71

def clear!
  @objects = []
  selections
end

#find_resource_by_identifier(resource_identifier) ⇒ Object



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

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

#get(remote_path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/helpers/selections.rb', line 43

def get(remote_path)
  if no_selections?
    no_selections_error
    return
  end
  @objects.each do |object|
    object.get(remote_path)
  end
  return
end

#in_resources?(object) ⇒ Boolean

Returns:

  • (Boolean)


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

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?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/helpers/selections.rb', line 27

def no_selections?
  !@objects || @objects.empty?
end

#no_selections_errorObject



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

def no_selections_error 
  puts "No nodes selected".informational
end

#object_in_selections?(object) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/helpers/selections.rb', line 100

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

#put(local_path, remote_path, bootstrap = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/helpers/selections.rb', line 31

def put(local_path, remote_path, bootstrap = false)
  if no_selections?
    no_selections_error
    return  
  end

  @objects.each do |object|
    object.put(local_path, remote_path, bootstrap)
  end
  return
end

#remove(resource_identifier = nil) ⇒ Object



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

def remove(resource_identifier = nil)
  resource_identifier = [resource_identifier] unless resource_identifier.is_a?(Array)
  resource_identifier.each { |ri| manage_object(:remove_object, ri) }
  puts "\n Nodes removed.".menu_item_cyan
end

#remove_object(object) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/helpers/selections.rb', line 90

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

#run(raw_commands, bootstrap = false) ⇒ Object

Runs commands over every object in the selection



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

def run(raw_commands, bootstrap = false)
  return unless @objects
 
  @objects.pmap {|object|
    object.run(raw_commands, bootstrap)
  }

  return
end

#selectionsObject Also known as: s



105
106
107
108
109
110
111
112
# File 'lib/helpers/selections.rb', line 105

def selections
  if no_selections?
    puts "\n" + "No added nodes.".headsup + "\n"
  else
    puts "\n\s\s" + "Selected nodes:".menu_title_green + "\n\n" 
    puts @objects.collect {|o| o.do_describe }.join + "\n"
  end
end