Class: Docker::Template::Meta
- Inherits:
-
Object
- Object
- Docker::Template::Meta
- Extended by:
- Forwardable::Extended
- Defined in:
- lib/docker/template/meta.rb
Constant Summary collapse
- DEFAULTS =
– rubocop:enable Style/MultilineBlockLayout –
HashWithIndifferentAccess.new({ "squash" => true, "startup" => true, "aliases" => {}, "push" => false, "build" => true, "cache" => false, "type" => "normal", "local_prefix" => "local", "project_data_dir" => "docker", "rootfs_base_img" => "envygeeks/alpine", "maintainer" => "Random User <[email protected]>", "user" => ENV["USER"] || ENV["USERNAME"] || "random", "name" => Template.root.basename.to_s, "project_copy_dir" => "project", "rootfs_template" => "alpine", "cache_dir" => "cache", "repos_dir" => "repos", "copy_dir" => "copy", "tag" => "latest", "clean" => false, "tty" => false, "tags" => {}, # "log_filters" => [ /^The push refers to a repository/, /\sdigest: sha256:/ ], # "project_copy_ignore" => %w( .git .bundle Dockerfile vendor/bundle .gitattributes .node_modules .gitignore docker tmp log ), }).freeze
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ Object
– Pull an indifferent key from the hash.
-
#[]=(key, val) ⇒ Object
–.
-
#alias? ⇒ Boolean
– Checks to see if the current meta is an alias of another.
-
#aliased_group(tag: current_tag) ⇒ Object
–.
-
#aliased_tag(tag: current_tag) ⇒ Object
–.
-
#by_group(group: current_group, query_data: @data) ⇒ Object
–.
-
#by_parent_group(tag: current_tag, query_data: @data) ⇒ Object
–.
-
#by_parent_tag(tag: current_tag, query_data: @data) ⇒ Object
–.
-
#by_tag(tag: current_tag, query_data: @data) ⇒ Object
–.
-
#complex_alias? ⇒ Boolean
– A complex alias happens when the user has an alias but also tries to add extra data, this allows them to use data from all parties.
-
#current_group ⇒ Object
(also: #group)
–.
-
#debug! ⇒ Object
–.
-
#fallback(group: current_group, tag: current_tag, query_data: @data) ⇒ Object
– Fallback, determining which route is the best.
-
#for_all(query_data: @data) ⇒ Object
– rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity –.
-
#groups ⇒ Object
– HELPER: Get a list of all the groups.
-
#include?(val) ⇒ Boolean
– Check if a part of the hash or a value is inside.
-
#initialize(overrides, root: nil) ⇒ Meta
constructor
– Create a new instance of
self.class. -
#merge(new_) ⇒ Object
(also: #deep_merge)
– Merge a hash into the meta.
-
#merge!(new_) ⇒ Object
– Destructive merging (@see self#merge) –.
-
#mergeable_array?(key = nil) ⇒ Boolean
–.
-
#mergeable_hash?(key = nil) ⇒ Boolean
– rubocop:enable Metrics/AbcSize –.
-
#normalize! ⇒ Object
–.
-
#queryable?(query_data: @data) ⇒ Boolean
– Check if a hash is queryable.
-
#root ⇒ Object
–.
-
#root_data ⇒ Object
–.
-
#tags ⇒ Object
– HELPER: Get a list of all the tags.
-
#to_a(raw: false, shell: false) ⇒ Object
– rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity –.
-
#to_enum ⇒ Object
–.
-
#to_h(raw: false) ⇒ Object
– rubocop:eanble Metrics/CyclomaticComplexity rubocop:eanble Metrics/PerceivedComplexity – Convert a ‘Meta’ into a normal hash.
-
#to_s(raw: false, shell: false) ⇒ Object
– Converts the current meta into a string.
-
#update(hash) ⇒ Object
–.
Constructor Details
#initialize(overrides, root: nil) ⇒ Meta
– Create a new instance of self.class.
“‘ – rubocop:disable Metrics/AbcSize –
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/docker/template/meta.rb', line 102 def initialize(overrides, root: nil) overrides = overrides.to_h :raw => true if overrides.is_a?(self.class) root = root.to_h :raw => true if root.is_a?(self.class) if root.nil? if Template.project? load_project_config( overrides ) else load_normal_config( overrides ) end @root = true else @data = overrides.stringify.with_indifferent_access @root_data = root.stringify.with_indifferent_access end debug! normalize! return end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, shell: false, &block) ⇒ Object (private)
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
# File 'lib/docker/template/meta.rb', line 670 def method_missing(method, *args, shell: false, &block) key = method.to_s.gsub(/\?$/, "") val = self[key] || self[key.singularize] \ || self[key.pluralize] if !args.empty? || block_given? super elsif method !~ /\?$/ string_wrapper(val, { :shell => shell }) else val = val.fallback if val.is_a?(self.class) && val.queryable? [true, false].include?(val) ? val : \ if val.respond_to?(:empty?) then !val.empty? else !!val end end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/docker/template/meta.rb', line 13 def data @data end |
Class Method Details
.opts_file(force: nil) ⇒ Object
81 82 83 84 85 |
# File 'lib/docker/template/meta.rb', line 81 def opts_file(force: nil) if force == :project || Template.project? then "docker/template.yml" else "opts.yml" end end |
Instance Method Details
#[](key) ⇒ Object
we make the getter slightly more indifferent because of tags.
– Pull an indifferent key from the hash. –
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/docker/template/meta.rb', line 216 def [](key) val = begin if key =~ /^\d+\.\d+$/ @data[key] || @data[ key.to_f ] elsif key =~ /^\d+$/ @data[key] || @data[ key.to_i ] else @data[key] end end if val.is_a?(Hash) return self.class.new(val, { :root => root_data }) end val end |
#[]=(key, val) ⇒ Object
–
244 245 246 247 248 249 |
# File 'lib/docker/template/meta.rb', line 244 def []=(key, val) hash = { key => val }.stringify @data.update( hash ) end |
#alias? ⇒ Boolean
– Checks to see if the current meta is an alias of another. This happens when the user has the tag in aliases but it’s not complex. –
447 448 449 |
# File 'lib/docker/template/meta.rb', line 447 def alias? !!(aliased_tag && aliased_tag != tag) end |
#aliased_group(tag: current_tag) ⇒ Object
–
486 487 488 489 490 |
# File 'lib/docker/template/meta.rb', line 486 def aliased_group(tag: current_tag) root_data[:tags][aliased_tag({ :tag => tag })] end |
#aliased_tag(tag: current_tag) ⇒ Object
–
472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/docker/template/meta.rb', line 472 def aliased_tag(tag: current_tag) aliases = root_data[:aliases] if aliases.nil? || !aliases.key?(tag) tag else aliases[ tag ] end end |
#by_group(group: current_group, query_data: @data) ⇒ Object
–
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/docker/template/meta.rb', line 410 def by_group(group: current_group, query_data: @data) if query_data.is_a?(self.class) then query_data.by_group({ :group => group }) elsif !query_data || !query_data.is_a?(Hash) return nil else query_data.fetch("group", {}).fetch( group, nil ) end end |
#by_parent_group(tag: current_tag, query_data: @data) ⇒ Object
–
428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/docker/template/meta.rb', line 428 def by_parent_group(tag: current_tag, query_data: @data) if aliased_tag == current_tag || !complex_alias? return nil else by_group({ :query_data => query_data, :group => aliased_group({ :tag => tag }) }) end end |
#by_parent_tag(tag: current_tag, query_data: @data) ⇒ Object
–
394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/docker/template/meta.rb', line 394 def by_parent_tag(tag: current_tag, query_data: @data) if aliased_tag == current_tag || !complex_alias? return nil else by_tag({ :query_data => query_data, :tag => aliased_tag({ :tag => tag }) }) end end |
#by_tag(tag: current_tag, query_data: @data) ⇒ Object
–
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/docker/template/meta.rb', line 376 def by_tag(tag: current_tag, query_data: @data) if query_data.is_a?(self.class) then query_data.by_tag({ :tag => tag }) elsif !query_data || !query_data.is_a?(Hash) return nil else query_data.fetch("tag", {}).fetch( tag, nil ) end end |
#complex_alias? ⇒ Boolean
– A complex alias happens when the user has an alias but also tries to add extra data, this allows them to use data from all parties. This allows them to reap the benefits of having shared data but sometimes independent data that diverges into it’s own. –
458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/docker/template/meta.rb', line 458 def complex_alias? if !alias? return false else !!root_data.find do |_, v| (v.is_a?(self.class) || v.is_a?(Hash)) && queryable?(:query_data => v) \ && by_tag(:query_data => v) end end end |
#current_group ⇒ Object Also known as: group
–
622 623 624 625 |
# File 'lib/docker/template/meta.rb', line 622 def current_group root_data[:tags][current_tag] || "normal" end |
#debug! ⇒ Object
–
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/docker/template/meta.rb', line 147 def debug! if root? && root_data["debug"] if !key?(:env) || self[:env].queryable? self[:env] ||= {} merge!({ :env => { :all => { :DEBUG => true } } }) end end end |
#fallback(group: current_group, tag: current_tag, query_data: @data) ⇒ Object
– Fallback, determining which route is the best. Tag > Group > All. – rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity –
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/docker/template/meta.rb', line 335 def fallback(group: current_group, tag: current_tag, query_data: @data) if query_data.is_a?(self.class) then query_data.fallback({ :group => group, :tag => tag }) elsif !query_data || !query_data.is_a?(Hash) || query_data.empty? return nil else if !(v = by_tag(:tag => tag, :query_data => query_data)).nil? then return v elsif !(v = by_parent_tag(:tag => tag, :query_data => query_data)).nil? then return v elsif !(v = by_group(:group => group, :query_data => query_data)).nil? then return v elsif !(v = by_parent_group(:tag => tag, :query_data => query_data)).nil? then return v else return for_all(:query_data => query_data) end end end |
#for_all(query_data: @data) ⇒ Object
– rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity –
359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/docker/template/meta.rb', line 359 def for_all(query_data: @data) if query_data.is_a?(self.class) then query_data \ .for_all elsif !query_data || !query_data.is_a?(Hash) return nil else query_data.fetch( "all", nil ) end end |
#groups ⇒ Object
– HELPER: Get a list of all the groups. –
639 640 641 |
# File 'lib/docker/template/meta.rb', line 639 def groups root_data["tags"].values.uniq end |
#include?(val) ⇒ Boolean
– Check if a part of the hash or a value is inside. –
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/docker/template/meta.rb', line 193 def include?(val) if val.is_a?(Hash) then val.stringify.each do |k, v| unless @data.key?(k) && @data[k] == v return false end end else return @data.include?( val ) end true end |
#merge(new_) ⇒ Object Also known as: deep_merge
– Merge a hash into the meta. If you merge non-queryable data it will then get merged into the queryable data. –
278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/docker/template/meta.rb', line 278 def merge(new_) if !queryable?(:query_data => new_) && queryable? new_ = { :all => new_ } end new_ = new_.stringify self.class.new(@data.deep_merge(new_), { :root => root_data }) end |
#merge!(new_) ⇒ Object
– Destructive merging (@see self#merge) –
295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/docker/template/meta.rb', line 295 def merge!(new_) if !queryable?(:query_data => new_) && queryable? new_ = { :all => new_ } end @data = @data.deep_merge( new_.stringify ) self end |
#mergeable_array?(key = nil) ⇒ Boolean
–
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 |
# File 'lib/docker/template/meta.rb', line 602 def mergeable_array?(key = nil) return false unless queryable? vals = [by_parent_tag, by_parent_group, \ by_tag, for_all, by_group].compact if key vals = vals.map do |val| val[key] end end !vals.empty? && !vals.any? do |val| !val.is_a?( Array ) end end |
#mergeable_hash?(key = nil) ⇒ Boolean
– rubocop:enable Metrics/AbcSize –
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
# File 'lib/docker/template/meta.rb', line 582 def mergeable_hash?(key = nil) return false unless queryable? vals = [by_parent_tag, by_parent_group, \ by_tag, for_all, by_group].compact if key vals = vals.map do |val| val[key] end end !vals.empty? && !vals.any? do |val| !val.is_a?(Hash) && !val.is_a?( self.class ) end end |
#normalize! ⇒ Object
–
131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/docker/template/meta.rb', line 131 def normalize! if root? opts = { :allowed_keys => [], :allowed_vals => [] } merge!({ "tags" => @data[ "tags"].stringify(**opts), "aliases" => @data["aliases"].stringify(**opts) }) end end |
#queryable?(query_data: @data) ⇒ Boolean
– Check if a hash is queryable. AKA has “all”, “group”, “tag”. –
313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/docker/template/meta.rb', line 313 def queryable?(query_data: @data) if query_data.is_a?(self.class) then query_data .queryable? elsif !query_data || !query_data.is_a?(Hash) || query_data.empty? return false else (query_data.keys - %w( group tag all )).empty? end end |
#root ⇒ Object
–
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/docker/template/meta.rb', line 171 def root if Template.project? then return Template.root.join(root_data[ :project_data_dir ]) else Template.root.join( root_data[:repos_dir], root_data[ :name ] ) end end |
#root_data ⇒ Object
–
165 166 167 |
# File 'lib/docker/template/meta.rb', line 165 def root_data return @root_data || @data end |
#tags ⇒ Object
– HELPER: Get a list of all the tags. –
631 632 633 |
# File 'lib/docker/template/meta.rb', line 631 def (root_data[:tags] || {}).keys | (root_data[:aliases] || {}).keys end |
#to_a(raw: false, shell: false) ⇒ Object
– rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity –
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/docker/template/meta.rb', line 516 def to_a(raw: false, shell: false) if raw return to_h({ :raw => true }).to_a elsif !mergeable_array? to_h.each_with_object([]) do |(k, v), a| a << "#{k}=#{ shell ? v.to_s.shellescape : v }" end else (for_all || []) | (by_parent_group || []) | (by_group || []) | \ (by_parent_tag || []) | (by_tag || []) end end |
#to_enum ⇒ Object
–
261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/docker/template/meta.rb', line 261 def to_enum @data.each_with_object({}) do |(k, v), h| if v.is_a?(Hash) then v = self.class.new(v, { :root => root_data }) end h[k] = v end.to_enum end |
#to_h(raw: false) ⇒ Object
– rubocop:eanble Metrics/CyclomaticComplexity rubocop:eanble Metrics/PerceivedComplexity – Convert a ‘Meta’ into a normal hash. If ‘self’ is queryable then we go and start merging values smartly. This means that we will merge all the arrays into one another and we will merge hashes into hashes. – rubocop:disable Metrics/AbcSize –
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/docker/template/meta.rb', line 545 def to_h(raw: false) return @data.to_h if raw || !queryable? || !mergeable_hash? keys = [for_all, by_group, by_parent_group, by_tag, \ by_parent_tag].compact.map(&:keys) keys.reduce(:+).each_with_object({}) do |k, h| vals = [for_all, by_group, by_parent_group, by_tag, \ by_parent_tag].compact h[k] = \ if mergeable_array?(k) vals.map { |v| v[k].to_a } \ .compact.reduce( :+ ) elsif mergeable_hash?(k) vals.map { |v| v[k].to_h } \ .compact.reduce( :deep_merge ) else vals.find do |v| v.key?( k ) end \ [k] end end end |
#to_s(raw: false, shell: false) ⇒ Object
– Converts the current meta into a string. –
496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'lib/docker/template/meta.rb', line 496 def to_s(raw: false, shell: false) if !raw && (mergeable_hash? || mergeable_array?) to_a(:shell => shell).join(" #{ "\n" if shell }") elsif !raw && queryable? then fallback \ .to_s else @data.to_s end end |
#update(hash) ⇒ Object
–
253 254 255 256 257 |
# File 'lib/docker/template/meta.rb', line 253 def update(hash) @data.update( hash.stringify ) end |