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



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

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



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

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

#add_object(object) ⇒ Object



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

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

#clear!Object



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

def clear!
  @objects = []
  selections
end

#find_resource_by_identifier(resource_identifier) ⇒ Object



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

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

#get(remote_path) ⇒ Object



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

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)


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

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)


25
26
27
# File 'lib/helpers/selections.rb', line 25

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

#no_selections_errorObject



119
120
121
# File 'lib/helpers/selections.rb', line 119

def no_selections_error 
  puts "No nodes selected".informational
end

#object_in_selections?(object) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/helpers/selections.rb', line 106

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

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



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

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



62
63
64
65
66
67
68
69
70
# File 'lib/helpers/selections.rb', line 62

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



96
97
98
99
100
101
102
103
104
# File 'lib/helpers/selections.rb', line 96

def remove_object(object)
  return if no_selections?
  unless object_in_selections?(object)
    print "\n#{object.identifier} is not within selections\n".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
# File 'lib/helpers/selections.rb', line 17

def run(raw_commands, bootstrap = false)
  return unless @objects
  @objects.each do |object|
    object.run(raw_commands, bootstrap)
  end
  return
end

#selectionsObject



111
112
113
114
115
116
117
# File 'lib/helpers/selections.rb', line 111

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