Class: VcoWorkflows::WorkflowParameter

Inherits:
Object
  • Object
show all
Defined in:
lib/vcoworkflows/workflowparameter.rb

Overview

WorkflowParameter is an object wrapper for workflow input and output parameters.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, type = nil, options = {}) ⇒ VcoWorkflows::WorkflowParameter

Create a new workflow parameter object

Parameters:

  • name (String) (defaults to: nil)

    Name of the workflow parameter

  • type (String) (defaults to: nil)

    Data type of the parameter (according to vCO)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vcoworkflows/workflowparameter.rb', line 30

def initialize(name = nil, type = nil, options = {})
  # Merge provided options with our defaults
  options = {
    required: false,
    value: nil
  }.merge(options)

  @name = name

  case type
  when %r{\/}
    @type = type.gsub(%r{\/.*$}, '')
    @subtype = type.gsub(%r{^.*\/}, '')
  else
    @type = type
    @subtype = nil
  end

  @required = options[:required]

  # If value is supposed to be an array but we dont' have a value yet,
  # create an empty array. If it's not supposed to be an array, just
  # set the value, even if it's still nil.
  if options[:value].nil?
    @type.eql?('Array') ? @value = [] : @value = nil
  else
    @value = set(options[:value])
  end
end

Instance Attribute Details

#nameString (readonly)

Parameter name

Returns:

  • (String)

    parameter name



10
11
12
# File 'lib/vcoworkflows/workflowparameter.rb', line 10

def name
  @name
end

#subtypeString (readonly)

Parameter subtype (used when type is ‘Array’)

Returns:

  • (String)

    parameter subtype



18
19
20
# File 'lib/vcoworkflows/workflowparameter.rb', line 18

def subtype
  @subtype
end

#typeString (readonly)

Parameter type

Returns:

  • (String)

    parameter type



14
15
16
# File 'lib/vcoworkflows/workflowparameter.rb', line 14

def type
  @type
end

#valueObject (readonly)

Parameter value

Returns:

  • (Object)

    parameter value



22
23
24
# File 'lib/vcoworkflows/workflowparameter.rb', line 22

def value
  @value
end

Instance Method Details

#as_structHash

Hashify the parameter (primarily useful for converting to JSON or YAML)

Returns:

  • (Hash)

    Contents of this object as a hash



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/vcoworkflows/workflowparameter.rb', line 139

def as_struct
  attributes = { type: @type, name: @name, scope: 'local' }

  # If the value is an array, we need to build it in the somewhat silly
  # manner that vCO requires it to be presented. Otherwise, just paste
  # it on the end of the hash.
  if @type.eql?('Array')
    fail(IOError, ERR[:wtf]) unless @value.is_a?(Array)
    attributes[:value] = { @type.downcase => { elements: [] } }
    @value.each { |val| attributes[:value][@type.downcase][:elements] << { @subtype => { value: val } } }
  else
    attributes[:value] = { @type => { value: @value } }
  end
  attributes
end

#required(required = true) ⇒ Object

Set whether or not this WorkflowParameter is required

Parameters:

  • required (Boolean) (defaults to: true)

    Set this parameter as required (if not specified)



91
92
93
# File 'lib/vcoworkflows/workflowparameter.rb', line 91

def required(required = true)
  @required = required
end

#required?Boolean

Determine whether or not this WorkflowParameter has been marked as required

Returns:

  • (Boolean)


99
100
101
# File 'lib/vcoworkflows/workflowparameter.rb', line 99

def required?
  @required
end

#set(value) ⇒ Object

Set the parameter value

Parameters:

  • value (Object)

    Value for the parameter



65
66
67
68
69
70
71
72
# File 'lib/vcoworkflows/workflowparameter.rb', line 65

def set(value)
  # Do some basic checking for Arrays.
  case @type
  when 'Array'
    fail(IOError, ERR[:param_verify_failed]) unless value.is_a?(Array)
  end unless value.nil?
  @value = value
end

#set?Boolean

Has a value been set for this parameter?

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/vcoworkflows/workflowparameter.rb', line 77

def set?
  case value.class
  when Array
    value.size == 0 ? false : true
  else
    value.nil? ? false : true
  end
end

#to_jsonString

Public Return a JSON document representation of this object

Returns:

  • (String)

    JSON representation



131
132
133
# File 'lib/vcoworkflows/workflowparameter.rb', line 131

def to_json
  as_struct.to_json
end

#to_sString

Return a string representation of the parameter

Returns:

  • (String)

    Pretty-formatted string



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/vcoworkflows/workflowparameter.rb', line 108

def to_s
  string = "#{@name}"
  # If value is either nil or an empty array
  if @value.nil? || @value.is_a?(Array) && @value.size == 0
    string << " (#{@type}"
    string << "/#{@subtype}" if @subtype
    string << ')'
    string << ' [required]' if @required
  else
    if @type.eql?('Array')
      string << ' ='
      @value.each { |v| string << "\n  - #{v}" }
    else
      string << " = #{@value}"
    end
  end
  string << "\n"
end