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.
  @value = if options[:value].nil?
             @type.eql?('Array') ? [] : nil
           else
             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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/vcoworkflows/workflowparameter.rb', line 133

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')
    raise(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)



88
89
90
# File 'lib/vcoworkflows/workflowparameter.rb', line 88

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

#required?Boolean

Determine whether or not this WorkflowParameter has been marked as required

Returns:

  • (Boolean)


96
97
98
# File 'lib/vcoworkflows/workflowparameter.rb', line 96

def required?
  @required
end

#set(value) ⇒ Object

Set the parameter value

Parameters:

  • value (Object)

    Value for the parameter



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

def set(value)
  # Do some basic checking for Arrays.
  unless value.nil?
    case @type
    when 'Array'
      raise(IOError, ERR[:param_verify_failed]) unless value.is_a?(Array)
    end
  end
  @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.empty? ? false : true
  else
    value.nil? ? false : true
  end
end

#to_jsonString

Public Return a JSON document representation of this object

Returns:

  • (String)

    JSON representation



125
126
127
# File 'lib/vcoworkflows/workflowparameter.rb', line 125

def to_json
  as_struct.to_json
end

#to_sString

Return a string representation of the parameter

Returns:

  • (String)

    Pretty-formatted string



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vcoworkflows/workflowparameter.rb', line 104

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