Class: Cult::Role

Inherits:
Object
  • Object
show all
Includes:
SingletonInstances
Defined in:
lib/cult/role.rb

Direct Known Subclasses

Node

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SingletonInstances

included

Constructor Details

#initialize(project, path) ⇒ Role

Returns a new instance of Role.



19
20
21
22
# File 'lib/cult/role.rb', line 19

def initialize(project, path)
  @project = project
  @path = path
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



17
18
19
# File 'lib/cult/role.rb', line 17

def path
  @path
end

#projectObject

Returns the value of attribute project.



16
17
18
# File 'lib/cult/role.rb', line 16

def project
  @project
end

Class Method Details

.all(project) ⇒ Object



163
164
165
166
167
168
# File 'lib/cult/role.rb', line 163

def self.all(project)
  fail if block_given?
  all_files(project).map do |filename|
    new(project, filename)
  end.select(&:exist?).to_named_array
end

.all_files(project) ⇒ Object



156
157
158
159
160
# File 'lib/cult/role.rb', line 156

def self.all_files(project)
  Dir.glob(File.join(path(project), "*")).select do |file|
    Dir.exist?(file)
  end
end

.by_name(project, name) ⇒ Object



146
147
148
# File 'lib/cult/role.rb', line 146

def self.by_name(project, name)
  new(project, File.join(path(project), name))
end

.delegate_to_definition(method_name, definition_key = nil) ⇒ Object



7
8
9
10
11
12
# File 'lib/cult/role.rb', line 7

def self.delegate_to_definition(method_name, definition_key = nil)
  definition_key ||= method_name
  define_method(method_name) do
    definition[definition_key.to_s]
  end
end

.path(project) ⇒ Object



151
152
153
# File 'lib/cult/role.rb', line 151

def self.path(project)
  File.join(project.path, "roles")
end

Instance Method Details

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



66
67
68
# File 'lib/cult/role.rb', line 66

def ==(rhs)
  [self.class, project, path] == [rhs.class, rhs.project, rhs.path]
end

#artifactsObject Also known as: files



87
88
89
# File 'lib/cult/role.rb', line 87

def artifacts
  Artifact.all_for_role(project, self)
end

#build_orderObject



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/cult/role.rb', line 171

def build_order
  all_items = [self] + parent_roles

  each_node = ->(&block) {
    all_items.each(&block)
  }

  each_child = ->(node, &block) {
    node.parent_roles.each(&block)
  }

  TSort.tsort(each_node, each_child).to_named_array
end

#build_tasksObject



77
78
79
# File 'lib/cult/role.rb', line 77

def build_tasks
  tasks.select { |t| t.is_a?(Cult::BuildTask) }
end

#collection_nameObject



38
39
40
41
# File 'lib/cult/role.rb', line 38

def collection_name
  class_name = self.class.name.split('::')[-1]
  class_name.downcase + 's'
end

#definitionObject



96
97
98
# File 'lib/cult/role.rb', line 96

def definition
  @definition ||= Definition.new(self)
end

#definition_parametersObject



107
108
109
# File 'lib/cult/role.rb', line 107

def definition_parameters
  { project: project, role: self }
end

#definition_parentsObject



112
113
114
# File 'lib/cult/role.rb', line 112

def definition_parents
  parent_roles
end

#definition_pathObject



101
102
103
104
# File 'lib/cult/role.rb', line 101

def definition_path
  [ File.join(path, "extra.json"),
    role_file ]
end

#event_tasksObject



82
83
84
# File 'lib/cult/role.rb', line 82

def event_tasks
  tasks.select { |t| t.is_a?(Cult::EventTask) }
end

#exist?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/cult/role.rb', line 28

def exist?
  Dir.exist?(path)
end

#has_role?(role) ⇒ Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/cult/role.rb', line 186

def has_role?(role)
  ! tree[role].nil?
end

#hashObject



61
62
63
# File 'lib/cult/role.rb', line 61

def hash
  [self.class, project, path].hash
end

#includesObject



117
118
119
# File 'lib/cult/role.rb', line 117

def includes
  definition.direct('includes') || ['base']
end

#inspectObject Also known as: to_s



55
56
57
# File 'lib/cult/role.rb', line 55

def inspect
  "\#<#{self.class.name} id:#{object_id.to_s(36)} #{name.inspect}>"
end

#nameObject



33
34
35
# File 'lib/cult/role.rb', line 33

def name
  File.basename(path)
end

#names_for_role(*a) ⇒ Object



190
191
192
# File 'lib/cult/role.rb', line 190

def names_for_role(*a)
  build_order.map(&:name)
end

#names_for_taskObject



198
199
200
# File 'lib/cult/role.rb', line 198

def names_for_task
  tasks.map(&:name)
end

#node?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/cult/role.rb', line 24

def node?
  false
end

#parent_rolesObject



122
123
124
125
126
# File 'lib/cult/role.rb', line 122

def parent_roles
  Array(includes).map do |name|
    Role.by_name(project, name)
  end.to_named_array
end

#query_for_roleObject



194
195
196
# File 'lib/cult/role.rb', line 194

def query_for_role
  build_order
end

#recursive_parent_roles(seen = []) ⇒ Object



129
130
131
132
133
134
135
136
137
138
# File 'lib/cult/role.rb', line 129

def recursive_parent_roles(seen = [])
  result = []
  parent_roles.each do |role|
    next if seen.include?(role)
    seen.push(role)
    result.push(role)
    result += role.recursive_parent_roles(seen)
  end
  result.to_named_array
end

#relative_path(obj_path) ⇒ Object



49
50
51
52
# File 'lib/cult/role.rb', line 49

def relative_path(obj_path)
  fail unless obj_path.start_with?(path)
  obj_path[path.size + 1 .. -1]
end

#remote_pathObject



44
45
46
# File 'lib/cult/role.rb', line 44

def remote_path
  File.join(project.remote_path, collection_name, name)
end

#role_fileObject



92
93
94
# File 'lib/cult/role.rb', line 92

def role_file
  File.join(path, "role.json")
end

#tasksObject



72
73
74
# File 'lib/cult/role.rb', line 72

def tasks
  Task.all_for_role(project, self)
end

#treeObject



141
142
143
# File 'lib/cult/role.rb', line 141

def tree
  ([self] + recursive_parent_roles).to_named_array
end