Class: VcoWorkflows::WorkflowParameter
- Inherits:
-
Object
- Object
- VcoWorkflows::WorkflowParameter
- Defined in:
- lib/vcoworkflows/workflowparameter.rb
Overview
WorkflowParameter is an object wrapper for workflow input and output parameters.
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
Parameter name.
-
#subtype ⇒ String
readonly
Parameter subtype (used when type is ‘Array’).
-
#type ⇒ String
readonly
Parameter type.
-
#value ⇒ Object
readonly
Parameter value.
Instance Method Summary collapse
-
#as_struct ⇒ Hash
Hashify the parameter (primarily useful for converting to JSON or YAML).
-
#initialize(name = nil, type = nil, options = {}) ⇒ VcoWorkflows::WorkflowParameter
constructor
Create a new workflow parameter object.
-
#required(required = true) ⇒ Object
Set whether or not this WorkflowParameter is required.
-
#required? ⇒ Boolean
Determine whether or not this WorkflowParameter has been marked as required.
-
#set(value) ⇒ Object
Set the parameter value.
-
#set? ⇒ Boolean
Has a value been set for this parameter?.
-
#to_json ⇒ String
Public Return a JSON document representation of this object.
-
#to_s ⇒ String
Return a string representation of the parameter.
Constructor Details
#initialize(name = nil, type = nil, options = {}) ⇒ VcoWorkflows::WorkflowParameter
Create a new workflow parameter object
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, = {}) # Merge provided options with our defaults = { required: false, value: nil }.merge() @name = name case type when %r{\/} @type = type.gsub(%r{\/.*$}, '') @subtype = type.gsub(%r{^.*\/}, '') else @type = type @subtype = nil end @required = [: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 [:value].nil? @type.eql?('Array') ? [] : nil else set([:value]) end end |
Instance Attribute Details
#name ⇒ String (readonly)
Parameter name
10 11 12 |
# File 'lib/vcoworkflows/workflowparameter.rb', line 10 def name @name end |
#subtype ⇒ String (readonly)
Parameter subtype (used when type is ‘Array’)
18 19 20 |
# File 'lib/vcoworkflows/workflowparameter.rb', line 18 def subtype @subtype end |
#type ⇒ String (readonly)
Parameter type
14 15 16 |
# File 'lib/vcoworkflows/workflowparameter.rb', line 14 def type @type end |
#value ⇒ Object (readonly)
Parameter value
22 23 24 |
# File 'lib/vcoworkflows/workflowparameter.rb', line 22 def value @value end |
Instance Method Details
#as_struct ⇒ Hash
Hashify the parameter (primarily useful for converting to JSON or YAML)
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
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
96 97 98 |
# File 'lib/vcoworkflows/workflowparameter.rb', line 96 def required? @required end |
#set(value) ⇒ Object
Set the parameter value
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?
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_json ⇒ String
Public Return a JSON document representation of this object
125 126 127 |
# File 'lib/vcoworkflows/workflowparameter.rb', line 125 def to_json as_struct.to_json end |
#to_s ⇒ String
Return a string representation of the parameter
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 |