Class: BlackStack::Workmesh::Service

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

Overview

stub worker class

Constant Summary collapse

ASSIGANTIONS =
[:entityweight, :roundrobin, :entitynumber]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Service

setup dispatcher configuration here



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/workmesh.rb', line 205

def initialize(h)
  errors = BlackStack::Workmesh::Service.descriptor_errors(h)
  raise "The service descriptor is not valid: #{errors.uniq.join(".\n")}" if errors.length > 0
  self.name = h[:name]
  self.entity_table = h[:entity_table]
  self.entity_field_assignation = h[:entity_field_assignation]
  self.protocols = []
  if h[:protocols]
    h[:protocols].each do |i|
      self.protocols << BlackStack::Workmesh::Protocol.new(i)
    end
  end 
  self.assignation = h[:assignation]     
end

Instance Attribute Details

#assignationObject

name to identify uniquely the worker



174
175
176
# File 'lib/workmesh.rb', line 174

def assignation
  @assignation
end

#entity_field_assignationObject

name to identify uniquely the worker



174
175
176
# File 'lib/workmesh.rb', line 174

def entity_field_assignation
  @entity_field_assignation
end

#entity_tableObject

name to identify uniquely the worker



174
175
176
# File 'lib/workmesh.rb', line 174

def entity_table
  @entity_table
end

#nameObject

name to identify uniquely the worker



174
175
176
# File 'lib/workmesh.rb', line 174

def name
  @name
end

#protocolsObject

name to identify uniquely the worker



174
175
176
# File 'lib/workmesh.rb', line 174

def protocols
  @protocols
end

Class Method Details

.descriptor_errors(h) ⇒ Object

return an array with the errors found in the description of the job



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/workmesh.rb', line 176

def self.descriptor_errors(h)
  errors = []
  # validate: the key :name exists and is an string
  errors << "The key :name is missing" if h[:name].nil?
  errors << "The key :name must be an String" unless h[:name].is_a?(String)
  # validate: the key :entity_table exists and is an symbol
  errors << "The key :entity_table is missing" if h[:entity_table].nil?
  errors << "The key :entity_table must be an Symbol" unless h[:entity_table].is_a?(Symbol)
  # validate: the key :entity_field_assignation exists and is an symbol
  errors << "The key :entity_field_assignation is missing" if h[:entity_field_assignation].nil?
  errors << "The key :entity_field_assignation must be an Symbol" unless h[:entity_field_assignation].is_a?(Symbol)
  # validate: the key :protocols exists is nil or it is an array of valid hash descritors of the Protocol class.
  errors << "The key :protocols must be an Array" unless h[:protocols].nil? || h[:protocols].is_a?(Array)
  if h[:protocols].is_a?(Array)
    h[:protocols].each do |protocol|
      errors << "The key :protocols must be an Array of valid hash descritors of the Protocol class" unless protocol.is_a?(Hash)
      z = Protocol.descriptor_errors(protocol)
      errors << "The key :protocols must be an Array of valid hash descritors of the Protocol class (errors: #{z.join('. ')})" unless z.length == 0
    end
  end
  # validate: the key :assignation is nil or it is a symbol belonging the array ASSIGANTIONS
  errors << "The key :assignation must be an Symbol" unless h[:assignation].nil? || h[:assignation].is_a?(Symbol)
  unless h[:assignation].nil?
    errors << "The key :assignation must be one of the following values: #{ASSIGANTIONS.join(", ")}" unless ASSIGANTIONS.include?(h[:assignation])
  end
  # return list of errors
  errors.uniq
end

Instance Method Details

#protocol(name) ⇒ Object

get a protocol from its name



230
231
232
# File 'lib/workmesh.rb', line 230

def protocol(name)
  self.protocols.select { |p| p.name.to_s == name.to_s }.first
end

#to_hashObject

return a hash descriptor of the worker



220
221
222
223
224
225
226
227
228
# File 'lib/workmesh.rb', line 220

def to_hash()
  {
    :name => self.name,
    :entity_table => self.entity_table,
    :entity_field_assignation => self.entity_field_assignation,
    :protocols => self.protocols.map { |p| p.to_hash },
    :assignation => self.assignation
  }
end