lorj 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +46 -0
  4. data/Gemfile +15 -15
  5. data/README.md +22 -17
  6. data/Rakefile +12 -2
  7. data/bin/cloud_test.rb +13 -65
  8. data/example/students_1/process/students.rb +39 -0
  9. data/example/students_1/students.rb +22 -5
  10. data/example/students_2/process/students.rb +48 -0
  11. data/example/students_2/students.rb +39 -16
  12. data/example/students_3/controller/yaml_students.rb +50 -43
  13. data/example/students_3/controller/yaml_students_controller.rb +100 -87
  14. data/example/students_3/process/students.rb +161 -97
  15. data/example/students_3/students.rb +85 -56
  16. data/example/yaml_students/students.rb +40 -40
  17. data/example/yaml_students/yaml_students.rb +103 -90
  18. data/lib/core/core.rb +356 -696
  19. data/lib/core/core_controller.rb +227 -0
  20. data/lib/core/core_internal.rb +339 -0
  21. data/lib/core/core_model.rb +328 -0
  22. data/lib/core/core_object_data.rb +330 -0
  23. data/lib/core/core_object_params.rb +230 -0
  24. data/lib/core/core_process.rb +391 -0
  25. data/lib/core/core_process_setup.rb +353 -0
  26. data/lib/core/core_setup_ask.rb +241 -0
  27. data/lib/core/core_setup_encrypt.rb +146 -0
  28. data/lib/core/core_setup_init.rb +229 -0
  29. data/lib/core/core_setup_list.rb +160 -0
  30. data/lib/core/definition.rb +647 -469
  31. data/lib/core/definition_internal.rb +264 -308
  32. data/lib/core/lorj_basecontroller.rb +95 -0
  33. data/lib/core/lorj_basedefinition.rb +307 -0
  34. data/lib/core/lorj_baseprocess.rb +265 -0
  35. data/lib/core/lorj_data.rb +583 -0
  36. data/lib/core/lorj_keypath.rb +119 -0
  37. data/lib/core_process/cloud/process/common.rb +63 -0
  38. data/lib/core_process/cloud/process/connection.rb +93 -0
  39. data/lib/core_process/cloud/process/external_network.rb +94 -0
  40. data/lib/core_process/cloud/process/flavor.rb +99 -0
  41. data/lib/core_process/cloud/process/images.rb +87 -0
  42. data/lib/core_process/cloud/process/internet_network.rb +34 -0
  43. data/lib/core_process/cloud/process/internet_server.rb +30 -0
  44. data/lib/core_process/cloud/process/keypairs.rb +276 -0
  45. data/lib/core_process/cloud/process/network.rb +108 -0
  46. data/lib/core_process/cloud/process/public_ip.rb +100 -0
  47. data/lib/core_process/cloud/process/router.rb +260 -0
  48. data/lib/core_process/cloud/process/rules.rb +120 -0
  49. data/lib/core_process/cloud/process/security_groups.rb +121 -0
  50. data/lib/core_process/cloud/process/server.rb +127 -0
  51. data/lib/core_process/cloud/process/server_log.rb +35 -0
  52. data/lib/core_process/cloud/process/subnetwork.rb +108 -0
  53. data/lib/core_process/cloud_process.rb +30 -0
  54. data/lib/logging.rb +298 -0
  55. data/lib/lorj/version.rb +18 -1
  56. data/lib/lorj.rb +58 -18
  57. data/lib/lorj_account.rb +556 -0
  58. data/lib/lorj_config.rb +468 -0
  59. data/lib/lorj_defaults.rb +278 -0
  60. data/lib/prc.rb +136 -104
  61. data/lib/prc_base_config.rb +285 -0
  62. data/lib/prc_core_config.rb +878 -0
  63. data/lib/prc_section_config.rb +57 -0
  64. data/lib/providers/hpcloud/compute.rb +81 -93
  65. data/lib/providers/hpcloud/hpcloud.rb +462 -0
  66. data/lib/providers/hpcloud/network.rb +96 -98
  67. data/lib/providers/hpcloud/security_groups.rb +41 -40
  68. data/lib/providers/mock/mock.rb +144 -0
  69. data/lib/providers/openstack/openstack.rb +45 -0
  70. data/lib/providers/templates/compute.rb +21 -23
  71. data/lib/providers/templates/mycloud.rb +72 -0
  72. data/lib/providers/templates/network.rb +11 -12
  73. data/lib/rh.rb +339 -0
  74. data/lorj-spec/defaults.yaml +4 -0
  75. data/lorj.gemspec +6 -0
  76. data/spec/00_lorj_log_spec.rb +53 -0
  77. data/spec/01_hash_rh_spec.rb +243 -0
  78. data/spec/02_prc_base_config_spec.rb +216 -0
  79. data/spec/04_prc_core_config_spec.rb +83 -0
  80. data/spec/11_lorj_config_spec.rb +263 -0
  81. data/spec/12_lorj_account_spec.rb +181 -0
  82. metadata +76 -28
  83. data/Gemfile.lock +0 -37
  84. data/example/students_1/process/Students.rb +0 -20
  85. data/example/students_2/process/Students.rb +0 -27
  86. data/example/students_4/controller/yaml_students.rb +0 -82
  87. data/example/students_4/controller/yaml_students_controller.rb +0 -141
  88. data/example/students_4/process/students.rb +0 -112
  89. data/example/students_4/students.rb +0 -103
  90. data/lib/core/lorj-basecontroller.rb +0 -90
  91. data/lib/core/lorj-basedefinition.rb +0 -1139
  92. data/lib/core/lorj-baseprocess.rb +0 -236
  93. data/lib/core/lorj-data.rb +0 -567
  94. data/lib/core/lorj-keypath.rb +0 -115
  95. data/lib/core_process/CloudProcess.rb +0 -337
  96. data/lib/core_process/global_process.rb +0 -502
  97. data/lib/core_process/network_process.rb +0 -605
  98. data/lib/prc-account.rb +0 -339
  99. data/lib/prc-config.rb +0 -1030
  100. data/lib/prc-logging.rb +0 -261
  101. data/lib/providers/hpcloud/Hpcloud.rb +0 -426
  102. data/lib/providers/mock/Mock.rb +0 -141
  103. data/lib/providers/openstack/Openstack.rb +0 -47
  104. data/lib/providers/templates/core.rb +0 -61
  105. data/spec/forj-account_spec.rb +0 -75
  106. data/spec/forj-config_spec.rb +0 -196
@@ -0,0 +1,227 @@
1
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ # Module Lorj which contains several classes.
16
+ #
17
+ # Those classes describes :
18
+ # - processes (BaseProcess) : How to create/delete/edit/query object.
19
+ # - controler (BaseControler) : If a provider is defined, define how will do
20
+ # object creation/etc...
21
+ # - definition(BaseDefinition): Functions to declare objects, query/data mapping
22
+ # and setup
23
+ # this task to make it to work.
24
+ module Lorj
25
+ # Adding controller core functions.
26
+ class BaseDefinition
27
+ # a Process can execute any kind of predefined controler task.
28
+ # Those function build controller_params with Provider compliant data
29
+ # (mapped)
30
+ # Results are formatted as usual framework Data object and stored.
31
+
32
+ # controller_connect call lorj framework to execute a
33
+ # controller connection task
34
+ #
35
+ # parameters:
36
+ # - +object_type+: Lorj object type to use for the connection.
37
+ # - +params+ : Parameters to use for connection.
38
+ def controller_connect(sObjectType, params = {})
39
+ controller_params = _get_object_params(sObjectType, :create_e, :connect,
40
+ true, params)
41
+ controller_obj = @controller.connect(sObjectType, controller_params)
42
+ data_obj = Lorj::Data.new
43
+ data_obj.set(controller_obj, sObjectType) do | sObjType, oObject |
44
+ begin
45
+ _return_map(sObjType, oObject)
46
+ rescue => e
47
+ PrcLib.runtime_fail 'connect %s.%s : %s',
48
+ @process.class, sObjectType, e.message
49
+ end
50
+ end
51
+ @object_data.add data_obj
52
+ data_obj
53
+ end
54
+ # controller_create call lorj framework to execute a
55
+ # controller creation task
56
+ #
57
+ # parameters:
58
+ # - +object_type+: Lorj object type to use for the creation.
59
+ # - +params+ : Parameters to use for creation.
60
+ def controller_create(sObjectType, params = {})
61
+ # The process ask the controller to create the object.
62
+ # controller_params have to be fully readable by the controller.
63
+ controller_params = _get_object_params(sObjectType, :create_e, :create,
64
+ true, params)
65
+ controller_obj = @controller.create(sObjectType, controller_params)
66
+ data_obj = Lorj::Data.new
67
+ data_obj.set(controller_obj, sObjectType) do | sObjType, oObject |
68
+ begin
69
+ _return_map(sObjType, oObject)
70
+ rescue => e
71
+ PrcLib.runtime_fail 'create %s.%s : %s',
72
+ @process.class, sObjectType, e.message
73
+ end
74
+ end
75
+ @object_data.add data_obj
76
+
77
+ data_obj
78
+ end
79
+
80
+ # controller_delete call lorj framework to execute a
81
+ # controller deletion task
82
+ #
83
+ # parameters:
84
+ # - +object_type+: Lorj object type to use for the deletion.
85
+ # - +params+ : Parameters to use for deletion.
86
+ #
87
+ # returns:
88
+ # - The controller must return true to inform about the real deletion
89
+ def controller_delete(sObjectType, params = {})
90
+ controller_params = _get_object_params(sObjectType, :delete_e, :delete,
91
+ true, params)
92
+ PrcLib.runtime_fail "delete Controller - %s: Object '%s' is not loaded.",
93
+ @controller.class,
94
+ key unless controller_params.exist?(sObjectType)
95
+ state = @controller.delete(sObjectType, controller_params)
96
+ @object_data.delete(sObjectType) if state
97
+ state
98
+ end
99
+
100
+ # controller_get call lorj framework to execute a controller get task
101
+ #
102
+ # parameters:
103
+ # - +object_type+: Lorj object type to use for the get.
104
+ # - +params+ : Parameters to use for get.
105
+ #
106
+ # returns:
107
+ # - Return a Lorj::Data representing the data retrieved by the controller.
108
+ def controller_get(sObjectType, sUniqId, params = {})
109
+ controller_params = _get_object_params(sObjectType, :get_e, :get,
110
+ true, params)
111
+
112
+ controller_obj = @controller.get(sObjectType, sUniqId, controller_params)
113
+ data_obj = Lorj::Data.new
114
+ data_obj.set(controller_obj, sObjectType) do | sObjType, oObject |
115
+ begin
116
+ _return_map(sObjType, oObject)
117
+ rescue => e
118
+ PrcLib.runtime_fail 'get %s.%s : %s',
119
+ @process.class, sObjectType, e.message
120
+ end
121
+ end
122
+ @object_data.add data_obj
123
+
124
+ data_obj
125
+ end
126
+
127
+ # controller_query call lorj framework to execute a controller query task
128
+ #
129
+ # parameters:
130
+ # - +object_type+: Lorj object type to use for the query.
131
+ # - +params+ : Parameters to use for query.
132
+ #
133
+ # returns:
134
+ # - Returns a Lorj::Data object, containing a list of Lorj::Data element.
135
+ def controller_query(sObjectType, hQuery, params = {})
136
+ # Check if we can re-use a previous query
137
+ list = @object_data[:query, sObjectType]
138
+ unless list.nil?
139
+ if list[:query] == hQuery
140
+ Lorj.debug(3, "Using Object '%s' query cache : %s",
141
+ sObjectType, hQuery)
142
+ return list
143
+ end
144
+ end
145
+
146
+ controller_params = _get_object_params(sObjectType, :query_e, :query,
147
+ true, params)
148
+ controller_query = _query_map(sObjectType, hQuery)
149
+
150
+ controller_obj = @controller.query(sObjectType, controller_query,
151
+ controller_params)
152
+
153
+ data_obj = Lorj::Data.new :list
154
+ data_obj.set(controller_obj,
155
+ sObjectType, hQuery) do | sObjType, key |
156
+ begin
157
+ _return_map(sObjType, key)
158
+ rescue => e
159
+ PrcLib.runtime_fail 'query %s.%s : %s',
160
+ @process.class, sObjectType, e.message
161
+ end
162
+ end
163
+
164
+ Lorj.debug(2, 'Object %s - queried. Found %s object(s).',
165
+ sObjectType, data_obj.length)
166
+
167
+ @object_data.add data_obj
168
+ data_obj
169
+ end
170
+
171
+ # controller_update call lorj framework to execute a controller update task
172
+ #
173
+ # parameters:
174
+ # - +object_type+: Lorj object type to use for the update.
175
+ # - +params+ : Parameters to use for update.
176
+ #
177
+ # returns:
178
+ # - The controller must return true to inform about the real deletion
179
+ def controller_update(sObjectType, params = {})
180
+ # Need to detect data updated and update the Controler object with the
181
+ # controler
182
+
183
+ controller_params = _get_object_params(sObjectType, :update_e, :update,
184
+ true, params)
185
+
186
+ data_obj = @object_data[sObjectType, :ObjectData]
187
+ controller_obj = data_obj[:object]
188
+
189
+ is_updated = false
190
+ attributes = data_obj[:attrs]
191
+ attributes.each do |key, value |
192
+ attribute_obj = KeyPath.new(key)
193
+
194
+ attribute_map = PrcLib.model.meta_obj.rh_get(sObjectType, :returns,
195
+ attribute_obj.fpath)
196
+ attr_map_obj = KeyPath.new(attribute_map)
197
+ old_value = @controller.get_attr(controller_obj, attr_map_obj.tree)
198
+
199
+ next if value == old_value
200
+
201
+ is_updated = true
202
+ @controller.set_attr(controller_obj, attr_map_obj.tree, value)
203
+ Lorj.debug(2, '%s.%s - Updating: %s = %s (old : %s)',
204
+ @process.class, sObjectType, key, value, old_value)
205
+ end
206
+
207
+ is_done = @controller.update(sObjectType, data_obj,
208
+ controller_params) if is_updated
209
+
210
+ PrcLib.runtime_fail "Controller function 'update' must return True or "\
211
+ "False. Class returned: '%s'",
212
+ is_done.class unless is_done.boolean?
213
+
214
+ Lorj.debug(1, '%s.%s - updated.',
215
+ @process.class, sObjectType) if is_done
216
+ data_obj.set(controller_obj, sObjectType) do | sObjType, an_object |
217
+ begin
218
+ _return_map(sObjType, an_object)
219
+ rescue => e
220
+ PrcLib.runtime_fail 'update %s.%s : %s',
221
+ @process.class, sObjectType, e.message
222
+ end
223
+ end
224
+ is_done
225
+ end
226
+ end
227
+ end
@@ -0,0 +1,339 @@
1
+ # encoding: UTF-8
2
+
3
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # rubocop: disable Metrics/AbcSize
18
+
19
+ # Module Lorj which contains several classes.
20
+ module Lorj
21
+ # Define private initialize functions for processes
22
+ class Core
23
+ private
24
+
25
+ # Create core objects
26
+ # Create a BaseDefinition object => Core
27
+ # Attach Config, Process and Controller object
28
+ def initialize_core_object(model)
29
+ ensure_class_exist(model[:process_class_name])
30
+
31
+ # Ex: Hpcloud(ForjAccount, HpcloudProvider)
32
+ def_class = model[:def_class]
33
+ if model[:controller_class]
34
+ @core_object = def_class.new(@config,
35
+ model[:process_class].new,
36
+ model[:controller_class].new)
37
+ else
38
+ @core_object = def_class.new(@config,
39
+ model[:process_class].new)
40
+ end
41
+ end
42
+
43
+ # Function to ensure a Class_name as already been loaded or not
44
+ # It raises an error if not.
45
+ def ensure_class_exist(class_name)
46
+ # Ensure Process class exists ---------------
47
+ Object.const_get(class_name)
48
+ rescue => e
49
+ raise Lorj::PrcError.new, format('Lorj::Core: Unable to find class'\
50
+ " '%s'\n%s", class_name, e.message)
51
+ end
52
+
53
+ # Initialize the Application model structure for Initialization
54
+ #
55
+ # * *Returns* :
56
+ # - model : Basic model.
57
+ def initialize_model
58
+ { :def_class => BaseDefinition,
59
+ :process_class => nil, :process_class_name => nil,
60
+ :controller_class => nil, :controller_class_name => nil }
61
+ end
62
+
63
+ # Initialize a basic Config Object and use the one passed
64
+ # as parameter.
65
+ # It sets @config instance attribute
66
+ def init_core_config(the_config)
67
+ if the_config.nil?
68
+ @config = Lorj::Config.new
69
+ Lorj.debug(2, 'Using an internal Lorj::Config object.')
70
+ else
71
+ @config = the_config
72
+ end
73
+ end
74
+
75
+ # Function to initialize itself with application controller.
76
+ #
77
+ # * *Args* :
78
+ # - +model+ : Application model loaded.
79
+ # - +controller+ : Processes to load.
80
+ # supports :
81
+ # - nil => return []
82
+ # - String/Symbol => return [String/Symbol]
83
+ # - Array => return Array
84
+ #
85
+ # * *Returns* :
86
+ # - model : Application model loaded.
87
+ def init_controller(model, the_controller)
88
+ Lorj.debug(1, "Loading Controller/definition '%s'", the_controller)
89
+
90
+ ok = load_controller(model, the_controller)
91
+
92
+ PrcLib.warning("Controller '%s' not properly loaded.",
93
+ the_controller) unless ok
94
+
95
+ model
96
+ end
97
+
98
+ # Function to load a controller.
99
+ # This function helps to load a Controller process
100
+ # (See load_process_controller_file) if exists
101
+ # and load the core controller file
102
+ # (Seeload_controllerfile)
103
+ #
104
+ # * *Args* :
105
+ # - +the_controller+ : Controller to load. Can be a string or
106
+ # a path to a file
107
+ #
108
+ # * *Returns* :
109
+ # - load_status : true if loaded, false otherwise
110
+ def load_controller(model, the_controller)
111
+ load_process_controller(model, the_controller)
112
+
113
+ the_controller = the_controller.to_s unless the_controller.is_a?(String)
114
+ if the_controller.include?('/')
115
+ file = File.expand_path(the_controller)
116
+ else
117
+ file = controllerfile_from_default(the_controller)
118
+ end
119
+
120
+ return PrcLib.warning('Controller not loaded: Controller file '\
121
+ "definition '%s' is missing.",
122
+ file) unless File.exist?(file)
123
+ load_controllerfile(model, file, classname_from_file(file))
124
+ end
125
+
126
+ # Load a Controller process file in ruby.
127
+ # If controller is a file, the process file name will be suffixed by
128
+ # _process, before the .rb file name.
129
+ #
130
+ # If controller is just a name (String/Symbol), it will load
131
+ # a file suffixed by _process.rb from PrcLib.controller_path
132
+ #
133
+ # * *Args* :
134
+ # - +model+ : Application/controller model loaded.
135
+ # - +controller+ : Symbol/String. Controller name or file to load.
136
+ #
137
+ # * *Returns* :
138
+ # - loaded : load status
139
+ def load_process_controller(model, the_controller)
140
+ Lorj.debug(1, "Loading process for controller '%s'", the_controller)
141
+
142
+ the_controller = the_controller.to_s unless the_controller.is_a?(String)
143
+ if the_controller.include?('/')
144
+ the_controller_process = the_controller.clone
145
+ the_controller_process['.rb'] = '_process.rb'
146
+ file = File.expand_path(the_controller_process)
147
+ else
148
+ file = controllerfile_from_default(the_controller, '_process')
149
+ end
150
+
151
+ return Lorj.debug(2, 'Process not loaded: Process controller file '\
152
+ "definition '%s' is missing.",
153
+ file) unless File.exist?(file)
154
+ load_processfile(model, file, classname_from_file(file))
155
+ end
156
+
157
+ # Load a Controller file in ruby.
158
+ #
159
+ # * *Args* :
160
+ # - +model+ : Application/controller model loaded.
161
+ # - +file+ : Process file to load.
162
+ # - +controller+ : Controller name or file to load.
163
+ #
164
+ # * *Returns* :
165
+ # - loaded : load status
166
+ def load_controllerfile(model, file, the_controller)
167
+ # Initialize an empty class derived from BaseDefinition.
168
+ # This to ensure provider Class will be derived from this Base Class
169
+ # If this class is derived from a different Class, ruby will raise an
170
+ # error.
171
+
172
+ # Create Definition and Controler derived from respectively
173
+ # BaseDefinition and BaseControler
174
+ base_definition_class = Class.new(BaseDefinition)
175
+ # Finally, name that class!
176
+ Lorj.debug(2, "Declaring Definition '%s'", the_controller)
177
+ Object.const_set the_controller, base_definition_class
178
+
179
+ model[:controller_class_name] = the_controller + 'Controller'
180
+ base_controller_class = Class.new(BaseController)
181
+ Lorj.debug(2, "Declaring Controller '%s'", model[:controller_class_name])
182
+
183
+ model[:controller_class] = Object.const_set model[:controller_class_name],
184
+ base_controller_class
185
+
186
+ # Loading Provider base file. This file should load a class
187
+ # which have the same name as the file.
188
+ load file
189
+ end
190
+
191
+ # Determine the controller file path from the single name.
192
+ # Uses PrcLib.controller_path as path to load this process.
193
+ #
194
+ # * *Args* :
195
+ # - +the_controller_class+ : Controller to load.
196
+ #
197
+ # * *Returns* :
198
+ # - file : absolute file path.
199
+ def controllerfile_from_default(the_controller, suffix = '')
200
+ File.join(PrcLib.controller_path, the_controller,
201
+ the_controller + suffix + '.rb')
202
+ end
203
+ end
204
+
205
+ # Define private Initialize functions for controllers
206
+ class Core
207
+ private
208
+
209
+ # Function to initialize itself with application processes.
210
+ #
211
+ # * *Args* :
212
+ # - +model+ : Application model loaded.
213
+ # - +processes+ : Processes to load.
214
+ # supports:
215
+ # - nil => return []
216
+ # - String/Symbol => return [String/Symbol]
217
+ # - Array => return Array
218
+ #
219
+ # * *Returns* :
220
+ # - model : Application model loaded.
221
+ def init_processes(model, processes)
222
+ process_array = processes_as_array(processes)
223
+
224
+ process_array.each do | a_process |
225
+ a_process = a_process.to_s if a_process.is_a?(Symbol)
226
+ unless load_process(model, a_process)
227
+ PrcLib.warning("Process '%s' not properly loaded.", a_process)
228
+ end
229
+ end
230
+ model
231
+ end
232
+
233
+ # Function analyzing the process class parameter
234
+ # and return the list of processes in an
235
+ # array of processes.
236
+ #
237
+ # * *Args* :
238
+ # - +processes_parameter+ : Parameter to interpret.
239
+ # supports:
240
+ # - nil => return []
241
+ # - String/Symbol => return [String/Symbol]
242
+ # - Array => return Array
243
+ #
244
+ # * *Returns* :
245
+ # - array_processes : Array of processes.
246
+ def processes_as_array(processes_parameter)
247
+ return [] if processes_parameter.nil?
248
+
249
+ return [processes_parameter] unless processes_parameter.is_a?(Array)
250
+
251
+ processes_parameter
252
+ end
253
+
254
+ # Function to load a process.
255
+ #
256
+ # * *Args* :
257
+ # - +the_process_+ : Process to load. Can be a string or
258
+ # a path to a file
259
+ #
260
+ # * *Returns* :
261
+ # - load_status : true if loaded, false otherwise
262
+ def load_process(model, the_process)
263
+ Lorj.debug(1, "Loading Process '%s'", the_process)
264
+
265
+ if the_process.include?('/')
266
+ file = File.expand_path(the_process)
267
+ else
268
+ file = processfile_from_default(the_process)
269
+ end
270
+
271
+ return PrcLib.warning("Process file definition '%s' is missing. ",
272
+ file) unless File.exist?(file)
273
+
274
+ load_processfile(model, file, classname_from_file(file))
275
+ end
276
+
277
+ # Function which determine the class name from the file name.
278
+ # rules:
279
+ # - First character : Capitalized
280
+ # - Any character prefixed by '_' : capitalized. '_' is removed.
281
+ #
282
+ # * *Args* :
283
+ # - +the_process_file+ : Process file to analyze.
284
+ #
285
+ # * *Returns* :
286
+ # - ProcessClassName : string representing the name of the class.
287
+ def classname_from_file(file)
288
+ the_process_class = File.basename(file)
289
+
290
+ the_process_class['.rb'] = '' if the_process_class['.rb']
291
+
292
+ if (/[A-Z]/ =~ the_process_class) != 0
293
+ the_process_class = the_process_class.capitalize
294
+ end
295
+
296
+ match_found = the_process_class.scan(/_[a-z]/)
297
+ if match_found
298
+ match_found.each { | str | the_process_class[str] = str[1].capitalize }
299
+ end
300
+
301
+ the_process_class
302
+ end
303
+
304
+ # Determine the process file path from the single name.
305
+ # Uses PrcLib.process_path as path to load this process.
306
+ #
307
+ # * *Args* :
308
+ # - +the_process_class+ : Process to load.
309
+ #
310
+ # * *Returns* :
311
+ # - file : absolute file path composed by:
312
+ # PrcLib.process_path/the_process_class + '.rb'
313
+ def processfile_from_default(the_process_class)
314
+ File.join(PrcLib.process_path, the_process_class + '.rb')
315
+ end
316
+
317
+ # Load a process file in ruby.
318
+ #
319
+ # * *Args* :
320
+ # - +file+ : Process file to load.
321
+ #
322
+ # * *Returns* :
323
+ # - loaded : load status
324
+ def load_processfile(model, file, the_process_class)
325
+ model[:process_class] = BaseProcess if model[:process_class].nil?
326
+
327
+ new_class = Class.new(model[:process_class])
328
+ unless /Process$/ =~ the_process_class
329
+ the_process_class = format('%sProcess', the_process_class)
330
+ end
331
+
332
+ Lorj.debug(1, "Declaring Process '%s'", the_process_class)
333
+ model[:process_class] = Object.const_set(the_process_class, new_class)
334
+ model[:process_class_name] = the_process_class
335
+ BaseDefinition.current_process(model[:process_class])
336
+ load file
337
+ end
338
+ end
339
+ end