Class: Rudder::DSL::Resource

Inherits:
Component show all
Defined in:
lib/rudder/dsl/resource.rb

Overview

Concourse Resource, defines inputs and outputs of a Concourse Job

DSL Usage:

Resource are defined by a name, type, and source.

Examples:

# Name's are set during initialization, and may not be nil.
resource :awesome_resource # => resource.name = :awesome_resource

resource nil # => Raises ArgumentError
# Type's are typically set during initialization
resource :awesome_resource, :git # => resource.type = :git

# but it may be set in the +resource+ block
resource :awesome_resource do
  type :git
end # => resource.type = :git
# this is useful when definining +Resources+ to be included in multiple pipelines,
# where the type does not change but the name may
# Source is set after construction
resource :awesome_resource, :git do
  source[:uri]    = 'https://github.com/jhmcstanton/rudder.git'
  source[:branch] = 'master'
end

Direct Known Subclasses

ResourceType

Instance Method Summary collapse

Methods inherited from Component

#method_missing, #respond_to?, #respond_to_missing?

Methods included from Util

#_convert_h_val, #_deep_to_h

Constructor Details

#initialize(name, type = nil) ⇒ Resource

All resources require:

  • Name of the resource. Must be unique across resources (not enforcable here).

  • The concourse resource type. Not verified until rendered to a Hash.

Parameters:

  • name (String, Symbol)

    name of this Concourse resource. Must not be nil.

  • type (String, Symbol) (defaults to: nil)

    of this Concoure Resource. May be nil here, must be set at compile time.



49
50
51
52
53
# File 'lib/rudder/dsl/resource.rb', line 49

def initialize(name, type = nil)
  raise super.ArgumentError 'Name cannot be nil' if name.nil?

  @resource = { name: name, type: type, source: {} }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rudder::DSL::Component

Instance Method Details

#_inner_hashObject



77
78
79
# File 'lib/rudder/dsl/resource.rb', line 77

def _inner_hash
  @resource
end

#sub_path(child_path) ⇒ String

Useful for creating the full path to a file in a Resource without knowing it's underlying concourse name.

Returns:

  • (String)

    the child_path prefixed by this resource's name



61
62
63
# File 'lib/rudder/dsl/resource.rb', line 61

def sub_path(child_path)
  File.join(@resource[:name].to_s, child_path)
end

#to_hHash

Returns YAML friendly Hash representation of this resource

Returns:

  • (Hash)

    YAML friendly Hash representation of this resource

Raises:

  • (RuntimeError)

    if type is nil or source is empty



70
71
72
73
74
75
# File 'lib/rudder/dsl/resource.rb', line 70

def to_h
  raise 'Type must be set for Concourse Resources'   if @resource[:type].nil?
  raise 'Source must be set for Concourse Resources' if @resource[:source].empty?

  super.to_h
end