Class: BlackStack::Workmesh::Protocol

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

Overview

class Node

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Protocol

Returns a new instance of Protocol.



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

def initialize(h)
  errors = BlackStack::Workmesh::Protocol.descriptor_errors(h)
  raise "The protocol 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_id = h[:entity_field_id]
  self.entity_field_sort = h[:entity_field_sort]
  self.push_function = h[:push_function]
  self.entity_field_push_time = h[:entity_field_push_time]
  self.entity_field_push_success = h[:entity_field_push_success]
  self.entity_field_push_error_description = h[:entity_field_push_error_description]
  self.pull_function = h[:pull_function]
  #self.enttity_field_pull_time = h[:entity_field_pull_time]
  #self.entity_field_pull_success = h[:entity_field_pull_success]
  #self.entity_field_pull_error_description = h[:entity_field_pull_error_description]
end

Instance Attribute Details

#entity_field_idObject

Returns the value of attribute entity_field_id.



62
63
64
# File 'lib/workmesh.rb', line 62

def entity_field_id
  @entity_field_id
end

#entity_field_push_error_descriptionObject

Returns the value of attribute entity_field_push_error_description.



63
64
65
# File 'lib/workmesh.rb', line 63

def entity_field_push_error_description
  @entity_field_push_error_description
end

#entity_field_push_successObject

Returns the value of attribute entity_field_push_success.



63
64
65
# File 'lib/workmesh.rb', line 63

def entity_field_push_success
  @entity_field_push_success
end

#entity_field_push_timeObject

Returns the value of attribute entity_field_push_time.



63
64
65
# File 'lib/workmesh.rb', line 63

def entity_field_push_time
  @entity_field_push_time
end

#entity_field_sortObject

Returns the value of attribute entity_field_sort.



62
63
64
# File 'lib/workmesh.rb', line 62

def entity_field_sort
  @entity_field_sort
end

#entity_tableObject

Returns the value of attribute entity_table.



62
63
64
# File 'lib/workmesh.rb', line 62

def entity_table
  @entity_table
end

#nameObject

Returns the value of attribute name.



61
62
63
# File 'lib/workmesh.rb', line 61

def name
  @name
end

#pull_functionObject

, :enttity_field_pull_time, :entity_field_pull_success, :entity_field_pull_error_description



65
66
67
# File 'lib/workmesh.rb', line 65

def pull_function
  @pull_function
end

#pull_status_access_pointObject

Returns the value of attribute pull_status_access_point.



64
65
66
# File 'lib/workmesh.rb', line 64

def pull_status_access_point
  @pull_status_access_point
end

#push_functionObject

Returns the value of attribute push_function.



63
64
65
# File 'lib/workmesh.rb', line 63

def push_function
  @push_function
end

Class Method Details

.descriptor_errors(h) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/workmesh.rb', line 67

def self.descriptor_errors(h)
  errors = []
  # validate: the key :name exists and is a 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_id exists and is an symbol
  errors << "The key :entity_field_id is missing" if h[:entity_field_id].nil?
  errors << "The key :entity_field_id must be an Symbol" unless h[:entity_field_id].is_a?(Symbol)
  # validate: the key :entity_field_sort exists and is an symbol
  errors << "The key :entity_field_sort is missing" if h[:entity_field_sort].nil?
  errors << "The key :entity_field_sort must be an Symbol" unless h[:entity_field_sort].is_a?(Symbol)
  
  # validate: the key :push_function is null or it is a procedure
  #errors << "The key :push_function must be an Symbol" unless h[:push_function].nil? || h[:push_function].is_a?()
  # validate: if :push_function exists, the key :entity_field_push_time exists and it is a symbol
  errors << "The key :entity_field_push_time is missing" if h[:push_function] && h[:entity_field_push_time].nil?
  # validate: if :push_function exists, the key :entity_field_push_success exists and it is a symbol
  errors << "The key :entity_field_push_success is missing" if h[:push_function] && h[:entity_field_push_success].nil?
  # validate: if :push_function exists, the key :entity_field_push_error_description exists and it is a symbol
  errors << "The key :entity_field_push_error_description is missing" if h[:push_function] && h[:entity_field_push_error_description].nil?

  # valiudate: if :pull_function exists, the key :pull_status_access_point exists and it is a string
  #errors << "The key :pull_status_access_point is missing" if h[:pull_function] && h[:pull_status_access_point].nil?
  #errors << "The key :pull_status_access_point must be an String" unless h[:pull_function].nil? || h[:pull_status_access_point].is_a?(String)
  # validate: if :pull_function exists, the key :entity_field_pull_time exists and it is a symbol
  #errors << "The key :entity_field_pull_time is missing" if h[:pull_function] && h[:entity_field_pull_time].nil?
  # validate: if :pull_function exists, the key :entity_field_pull_success exists and it is a symbol
  #errors << "The key :entity_field_pull_success is missing" if h[:pull_function] && h[:entity_field_pull_success].nil?
  # validate: if :pull_function exists, the key :entity_field_pull_error_description exists and it is a symbol
  #errors << "The key :entity_field_pull_error_description is missing" if h[:pull_function] && h[:entity_field_pull_error_description].nil?

  # return list of errors
  errors.uniq
end

Instance Method Details

#pull(node, l = nil) ⇒ Object

execute the pull function of this protocol, and update the push flags



158
159
160
161
162
163
164
165
166
# File 'lib/workmesh.rb', line 158

def pull(node, l=nil)
  l = BlackStack::DummyLogger.new(nil) if l.nil?
  raise 'The pull function is not defined' if self.pull_function.nil?
  begin
    self.pull_function.call(node, l)
  rescue => e
    raise e
  end
end

#push(entity, node, l = nil) ⇒ Object

execute the push function of this protocol, and update the push flags



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/workmesh.rb', line 140

def push(entity, node, l=nil)
  l = BlackStack::DummyLogger.new(nil) if l.nil?
  raise 'The push function is not defined' if self.push_function.nil?
  entity[entity_field_push_time] = now()
  begin
    self.push_function.call(entity, node, l)
    entity[entity_field_push_success] = true
    entity[entity_field_push_error_description] = nil
    entity.save
  rescue => e
    entity[entity_field_push_success] = false
    entity[entity_field_push_error_description] = e.message
    entity.save
    raise e
  end
end

#to_hashObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/workmesh.rb', line 122

def to_hash()
  ret = super()
  ret[:name] = self.name
  ret[:entity_table] = self.entity_table
  ret[:entity_field_id] = self.entity_field_id
  ret[:entity_field_sort] = self.entity_field_sort
  ret[:push_function] = self.push_function
  ret[:entity_field_push_time] = self.entity_field_push_time
  ret[:entity_field_push_success] = self.entity_field_push_success
  ret[:entity_field_push_error_description] = self.entity_field_push_error_description
  ret[:pull_function] = self.pull_function
  #ret[:enttity_field_pull_time] = self.enttity_field_pull_time
  #ret[:entity_field_pull_success] = self.entity_field_pull_success
  #ret[:entity_field_pull_error_description] = self.entity_field_pull_error_description
  ret
end