Class: Cult::Role
- Inherits:
-
Object
- Object
- Cult::Role
- Includes:
- SingletonInstances
- Defined in:
- lib/cult/role.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#project ⇒ Object
Returns the value of attribute project.
Class Method Summary collapse
- .all(project) ⇒ Object
- .all_files(project) ⇒ Object
- .by_name(project, name) ⇒ Object
- .delegate_to_definition(method_name, definition_key = nil) ⇒ Object
- .path(project) ⇒ Object
Instance Method Summary collapse
- #==(rhs) ⇒ Object (also: #eql?)
- #artifacts ⇒ Object (also: #files)
- #build_order ⇒ Object
- #build_tasks ⇒ Object
- #collection_name ⇒ Object
- #definition ⇒ Object
- #definition_parameters ⇒ Object
- #definition_parents ⇒ Object
- #definition_path ⇒ Object
- #event_tasks ⇒ Object
- #exist? ⇒ Boolean
- #has_role?(role) ⇒ Boolean
- #hash ⇒ Object
- #includes ⇒ Object
-
#initialize(project, path) ⇒ Role
constructor
A new instance of Role.
- #inspect ⇒ Object (also: #to_s)
- #name ⇒ Object
- #names_for_role(*a) ⇒ Object
- #names_for_task ⇒ Object
- #node? ⇒ Boolean
- #parent_roles ⇒ Object
- #query_for_role ⇒ Object
- #recursive_parent_roles(seen = []) ⇒ Object
- #relative_path(obj_path) ⇒ Object
- #remote_path ⇒ Object
- #role_file ⇒ Object
- #tasks ⇒ Object
- #tree ⇒ Object
Methods included from SingletonInstances
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
#path ⇒ Object
Returns the value of attribute path.
17 18 19 |
# File 'lib/cult/role.rb', line 17 def path @path end |
#project ⇒ Object
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 |
#artifacts ⇒ Object Also known as: files
87 88 89 |
# File 'lib/cult/role.rb', line 87 def artifacts Artifact.all_for_role(project, self) end |
#build_order ⇒ Object
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_tasks ⇒ Object
77 78 79 |
# File 'lib/cult/role.rb', line 77 def build_tasks tasks.select { |t| t.is_a?(Cult::BuildTask) } end |
#collection_name ⇒ Object
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 |
#definition ⇒ Object
96 97 98 |
# File 'lib/cult/role.rb', line 96 def definition @definition ||= Definition.new(self) end |
#definition_parameters ⇒ Object
107 108 109 |
# File 'lib/cult/role.rb', line 107 def definition_parameters { project: project, role: self } end |
#definition_parents ⇒ Object
112 113 114 |
# File 'lib/cult/role.rb', line 112 def definition_parents parent_roles end |
#definition_path ⇒ Object
101 102 103 104 |
# File 'lib/cult/role.rb', line 101 def definition_path [ File.join(path, "extra.json"), role_file ] end |
#event_tasks ⇒ Object
82 83 84 |
# File 'lib/cult/role.rb', line 82 def event_tasks tasks.select { |t| t.is_a?(Cult::EventTask) } end |
#exist? ⇒ Boolean
28 29 30 |
# File 'lib/cult/role.rb', line 28 def exist? Dir.exist?(path) end |
#has_role?(role) ⇒ Boolean
186 187 188 |
# File 'lib/cult/role.rb', line 186 def has_role?(role) ! tree[role].nil? end |
#hash ⇒ Object
61 62 63 |
# File 'lib/cult/role.rb', line 61 def hash [self.class, project, path].hash end |
#includes ⇒ Object
117 118 119 |
# File 'lib/cult/role.rb', line 117 def includes definition.direct('includes') || ['base'] end |
#inspect ⇒ Object 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 |
#name ⇒ Object
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_task ⇒ Object
198 199 200 |
# File 'lib/cult/role.rb', line 198 def names_for_task tasks.map(&:name) end |
#node? ⇒ Boolean
24 25 26 |
# File 'lib/cult/role.rb', line 24 def node? false end |
#parent_roles ⇒ Object
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_role ⇒ Object
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_path ⇒ Object
44 45 46 |
# File 'lib/cult/role.rb', line 44 def remote_path File.join(project.remote_path, collection_name, name) end |
#role_file ⇒ Object
92 93 94 |
# File 'lib/cult/role.rb', line 92 def role_file File.join(path, "role.json") end |
#tasks ⇒ Object
72 73 74 |
# File 'lib/cult/role.rb', line 72 def tasks Task.all_for_role(project, self) end |
#tree ⇒ Object
141 142 143 |
# File 'lib/cult/role.rb', line 141 def tree ([self] + recursive_parent_roles).to_named_array end |