lorj 0.2.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +46 -0
- data/Gemfile +15 -15
- data/README.md +22 -17
- data/Rakefile +12 -2
- data/bin/cloud_test.rb +13 -65
- data/example/students_1/process/students.rb +39 -0
- data/example/students_1/students.rb +22 -5
- data/example/students_2/process/students.rb +48 -0
- data/example/students_2/students.rb +39 -16
- data/example/students_3/controller/yaml_students.rb +50 -43
- data/example/students_3/controller/yaml_students_controller.rb +100 -87
- data/example/students_3/process/students.rb +161 -97
- data/example/students_3/students.rb +85 -56
- data/example/yaml_students/students.rb +40 -40
- data/example/yaml_students/yaml_students.rb +103 -90
- data/lib/core/core.rb +356 -696
- data/lib/core/core_controller.rb +227 -0
- data/lib/core/core_internal.rb +339 -0
- data/lib/core/core_model.rb +328 -0
- data/lib/core/core_object_data.rb +330 -0
- data/lib/core/core_object_params.rb +230 -0
- data/lib/core/core_process.rb +391 -0
- data/lib/core/core_process_setup.rb +353 -0
- data/lib/core/core_setup_ask.rb +241 -0
- data/lib/core/core_setup_encrypt.rb +146 -0
- data/lib/core/core_setup_init.rb +229 -0
- data/lib/core/core_setup_list.rb +160 -0
- data/lib/core/definition.rb +647 -469
- data/lib/core/definition_internal.rb +264 -308
- data/lib/core/lorj_basecontroller.rb +95 -0
- data/lib/core/lorj_basedefinition.rb +307 -0
- data/lib/core/lorj_baseprocess.rb +265 -0
- data/lib/core/lorj_data.rb +583 -0
- data/lib/core/lorj_keypath.rb +119 -0
- data/lib/core_process/cloud/process/common.rb +63 -0
- data/lib/core_process/cloud/process/connection.rb +93 -0
- data/lib/core_process/cloud/process/external_network.rb +94 -0
- data/lib/core_process/cloud/process/flavor.rb +99 -0
- data/lib/core_process/cloud/process/images.rb +87 -0
- data/lib/core_process/cloud/process/internet_network.rb +34 -0
- data/lib/core_process/cloud/process/internet_server.rb +30 -0
- data/lib/core_process/cloud/process/keypairs.rb +276 -0
- data/lib/core_process/cloud/process/network.rb +108 -0
- data/lib/core_process/cloud/process/public_ip.rb +100 -0
- data/lib/core_process/cloud/process/router.rb +260 -0
- data/lib/core_process/cloud/process/rules.rb +120 -0
- data/lib/core_process/cloud/process/security_groups.rb +121 -0
- data/lib/core_process/cloud/process/server.rb +127 -0
- data/lib/core_process/cloud/process/server_log.rb +35 -0
- data/lib/core_process/cloud/process/subnetwork.rb +108 -0
- data/lib/core_process/cloud_process.rb +30 -0
- data/lib/logging.rb +298 -0
- data/lib/lorj/version.rb +18 -1
- data/lib/lorj.rb +58 -18
- data/lib/lorj_account.rb +556 -0
- data/lib/lorj_config.rb +468 -0
- data/lib/lorj_defaults.rb +278 -0
- data/lib/prc.rb +136 -104
- data/lib/prc_base_config.rb +285 -0
- data/lib/prc_core_config.rb +878 -0
- data/lib/prc_section_config.rb +57 -0
- data/lib/providers/hpcloud/compute.rb +81 -93
- data/lib/providers/hpcloud/hpcloud.rb +462 -0
- data/lib/providers/hpcloud/network.rb +96 -98
- data/lib/providers/hpcloud/security_groups.rb +41 -40
- data/lib/providers/mock/mock.rb +144 -0
- data/lib/providers/openstack/openstack.rb +45 -0
- data/lib/providers/templates/compute.rb +21 -23
- data/lib/providers/templates/mycloud.rb +72 -0
- data/lib/providers/templates/network.rb +11 -12
- data/lib/rh.rb +339 -0
- data/lorj-spec/defaults.yaml +4 -0
- data/lorj.gemspec +6 -0
- data/spec/00_lorj_log_spec.rb +53 -0
- data/spec/01_hash_rh_spec.rb +243 -0
- data/spec/02_prc_base_config_spec.rb +216 -0
- data/spec/04_prc_core_config_spec.rb +83 -0
- data/spec/11_lorj_config_spec.rb +263 -0
- data/spec/12_lorj_account_spec.rb +181 -0
- metadata +76 -28
- data/Gemfile.lock +0 -37
- data/example/students_1/process/Students.rb +0 -20
- data/example/students_2/process/Students.rb +0 -27
- data/example/students_4/controller/yaml_students.rb +0 -82
- data/example/students_4/controller/yaml_students_controller.rb +0 -141
- data/example/students_4/process/students.rb +0 -112
- data/example/students_4/students.rb +0 -103
- data/lib/core/lorj-basecontroller.rb +0 -90
- data/lib/core/lorj-basedefinition.rb +0 -1139
- data/lib/core/lorj-baseprocess.rb +0 -236
- data/lib/core/lorj-data.rb +0 -567
- data/lib/core/lorj-keypath.rb +0 -115
- data/lib/core_process/CloudProcess.rb +0 -337
- data/lib/core_process/global_process.rb +0 -502
- data/lib/core_process/network_process.rb +0 -605
- data/lib/prc-account.rb +0 -339
- data/lib/prc-config.rb +0 -1030
- data/lib/prc-logging.rb +0 -261
- data/lib/providers/hpcloud/Hpcloud.rb +0 -426
- data/lib/providers/mock/Mock.rb +0 -141
- data/lib/providers/openstack/Openstack.rb +0 -47
- data/lib/providers/templates/core.rb +0 -61
- data/spec/forj-account_spec.rb +0 -75
- data/spec/forj-config_spec.rb +0 -196
|
@@ -0,0 +1,328 @@
|
|
|
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
|
+
# TODO: Move most declaration functions to Model class for simplification.
|
|
20
|
+
|
|
21
|
+
# Module Lorj which contains several classes.
|
|
22
|
+
#
|
|
23
|
+
# Those classes describes :
|
|
24
|
+
# - processes (BaseProcess) : How to create/delete/edit/query object.
|
|
25
|
+
# - controler (BaseControler) : If a provider is defined, define how will do
|
|
26
|
+
# object creation/etc...
|
|
27
|
+
# - definition(BaseDefinition): Functions to declare objects, query/data mapping
|
|
28
|
+
# and setup
|
|
29
|
+
# this task to make it to work.
|
|
30
|
+
module Lorj
|
|
31
|
+
# Model Object
|
|
32
|
+
class Model
|
|
33
|
+
attr_accessor :meta_obj, :meta_data, :meta_predefined_values
|
|
34
|
+
attr_accessor :query_auto_map
|
|
35
|
+
|
|
36
|
+
# Model initialisation
|
|
37
|
+
def initialize
|
|
38
|
+
###################################################
|
|
39
|
+
# Class management Section
|
|
40
|
+
###################################################
|
|
41
|
+
|
|
42
|
+
# Meta Object declaration structure
|
|
43
|
+
# <Object>
|
|
44
|
+
# :query_mapping List of keypath mapped.
|
|
45
|
+
# <keypath> = <keypath mapped>
|
|
46
|
+
# :lambdas:
|
|
47
|
+
# :create_e: function to call at 'Create' task
|
|
48
|
+
# :delete_e: function to call at 'Delete' task
|
|
49
|
+
# :update_e: function to call at 'Update' task
|
|
50
|
+
# :get_e: function to call at 'Get' task
|
|
51
|
+
# :query_e: function to call at 'Query' task
|
|
52
|
+
# :value_mapping: Define list of Object's key values mapping.
|
|
53
|
+
# <keypath> key value mapping lists
|
|
54
|
+
# <value> = <map> Define the value mapping.
|
|
55
|
+
# :returns
|
|
56
|
+
# <keypath> key value to extract from controller object.
|
|
57
|
+
# :params: Defines CloudData (:data) or object (:CloudObj)
|
|
58
|
+
# needs by the <Object>
|
|
59
|
+
# :keys: Contains keys in a tree of hash.
|
|
60
|
+
# <keypath>: String. One element (string with : and /) of
|
|
61
|
+
# :list defining the key
|
|
62
|
+
# :type: :data or :CloudObj
|
|
63
|
+
# :for: Array of events which requires the data or
|
|
64
|
+
# CloudObj to work.
|
|
65
|
+
# :mapping: To automatically create a provider hash data
|
|
66
|
+
# mapped (hdata).
|
|
67
|
+
# :required: True if this parameter is required.
|
|
68
|
+
# :extract_from: Array. Build the keypath value from another
|
|
69
|
+
# params value.
|
|
70
|
+
# Ex: This example will extract :id from
|
|
71
|
+
# :security_groups object
|
|
72
|
+
# :extract_from => [:security_groups, :id]
|
|
73
|
+
#
|
|
74
|
+
@meta_obj = {}
|
|
75
|
+
|
|
76
|
+
# meta data are defined in defaults.yaml and loaded in Lorj::Default class
|
|
77
|
+
# definition.
|
|
78
|
+
# Cloud provider can redefine ForjData defaults and add some extra
|
|
79
|
+
# parameters.
|
|
80
|
+
# To get Application defaults, read defaults.yaml, under :sections:
|
|
81
|
+
# Those values can be updated by the controller with define_data
|
|
82
|
+
# <Section>:
|
|
83
|
+
# <Data>: Required. Symbol/String. default: nil
|
|
84
|
+
# => Data name. This symbol must be unique, across
|
|
85
|
+
# sections.
|
|
86
|
+
# :desc: Required. String. default: nil
|
|
87
|
+
# => Description
|
|
88
|
+
# :explanation: |- Print a multiline explanation before ask the key
|
|
89
|
+
# value.
|
|
90
|
+
# ERB template enable. To get config data,
|
|
91
|
+
# type <%= config[...] %>
|
|
92
|
+
# :readonly: Optional. true/false. Default: false
|
|
93
|
+
# => oForjConfig.set() will fail if readonly is
|
|
94
|
+
# true. It can be set, only thanks to:
|
|
95
|
+
# - oForjConfig.setup()
|
|
96
|
+
# or using private
|
|
97
|
+
# - oForjConfig._set()
|
|
98
|
+
# :account_exclusive: Optional. true/false. Default: false
|
|
99
|
+
# => Only oConfig.account_get/set() can handle the
|
|
100
|
+
# value
|
|
101
|
+
# oConfig.set/get cannot.
|
|
102
|
+
# :account: Optional. default: False
|
|
103
|
+
# => setup will configure the account with this
|
|
104
|
+
# <Data>
|
|
105
|
+
# :ask_sort: Number which represents the ask order in the
|
|
106
|
+
# step group. (See /:setup/:ask_step for details)
|
|
107
|
+
# :after: <Data> Name of the previous <Data> to ask before the
|
|
108
|
+
# current one.
|
|
109
|
+
# :depends_on:
|
|
110
|
+
# => Identify :data type required to be set before
|
|
111
|
+
# the current one.
|
|
112
|
+
# :default_value: Default value at setup time. This is not
|
|
113
|
+
# necessarily the Application default value
|
|
114
|
+
# (See /:default)
|
|
115
|
+
# :validate: Regular expression to validate end user input
|
|
116
|
+
# during setup.
|
|
117
|
+
# :value_mapping: list of values to map as defined by the
|
|
118
|
+
# controller
|
|
119
|
+
# :controller: mapping for get controller value from process
|
|
120
|
+
# values
|
|
121
|
+
# <value> : <map> value map equivalence. See data_value_mapping
|
|
122
|
+
# function
|
|
123
|
+
# :process: mapping for get process value from controller
|
|
124
|
+
# values
|
|
125
|
+
# <value> : <map> value map equivalence. See data_value_mapping
|
|
126
|
+
# function
|
|
127
|
+
# :default: Default value. Replace /:default/<data>
|
|
128
|
+
# :list_values: Defines a list of valid values for the current
|
|
129
|
+
# data.
|
|
130
|
+
# :query_type :controller_call to execute a function defined
|
|
131
|
+
# in the controller object.
|
|
132
|
+
# :process_call to execute a function defined in
|
|
133
|
+
# the process object.
|
|
134
|
+
# :values to get list of values from :values.
|
|
135
|
+
# :object Object to load before calling the function.
|
|
136
|
+
# Only :query_type = :*_call
|
|
137
|
+
# :query_call Symbol. function name to call.
|
|
138
|
+
# Only :query_type = :*_call
|
|
139
|
+
# function must return an Array.
|
|
140
|
+
# :query_params Hash. Controler function parameters.
|
|
141
|
+
# Only :query_type = :*_call
|
|
142
|
+
# :validate :list_strict. valid only if value is one of
|
|
143
|
+
# thoselisted.
|
|
144
|
+
# :values: to retrieve from.
|
|
145
|
+
# otherwise define simply a list of possible
|
|
146
|
+
# values.
|
|
147
|
+
# :ask_step: Step number. By default, setup will determine
|
|
148
|
+
# the step, thanks to meta lorj object
|
|
149
|
+
# dependencies tree.
|
|
150
|
+
# This number start at 0. Each step can be defined
|
|
151
|
+
# by /:setup/:ask_step/<steps> list.
|
|
152
|
+
# :pre_step_function: Process called before asking the data.
|
|
153
|
+
# if it returns true, user interaction is
|
|
154
|
+
# cancelled.
|
|
155
|
+
# :post_step_function:Process called after asking the data.
|
|
156
|
+
# if it returns false, the user is requested to
|
|
157
|
+
# re-enter a new value.
|
|
158
|
+
#
|
|
159
|
+
# :setup: This section describes group of fields to ask,
|
|
160
|
+
# step by step.
|
|
161
|
+
# :ask_step: Define an Array of setup steps to ask to the
|
|
162
|
+
# end user. The step order is respected, and
|
|
163
|
+
# start at 0
|
|
164
|
+
# - :desc: Define the step description. ERB template
|
|
165
|
+
# enable. To get config data, type config[...]
|
|
166
|
+
# :explanation: |- Define a multiline explanation. This is printed
|
|
167
|
+
# out in brown color.
|
|
168
|
+
# ERB template enable. To get config data, type
|
|
169
|
+
# <%= config[...] %>
|
|
170
|
+
# :add: Define a list of additionnal fields to ask.
|
|
171
|
+
# - <Data> Data to ask.
|
|
172
|
+
#
|
|
173
|
+
# :default: List of <Data> application default values.
|
|
174
|
+
# <Data> : Value to use at the config application level.
|
|
175
|
+
@meta_data = {}
|
|
176
|
+
|
|
177
|
+
# The Generic Process can pre-define some data and value
|
|
178
|
+
# (function predefine_data)
|
|
179
|
+
# The Generic Process (and external framework call) only knows about
|
|
180
|
+
# Generic data.
|
|
181
|
+
# information used:
|
|
182
|
+
#
|
|
183
|
+
@meta_predefined_values = {}
|
|
184
|
+
|
|
185
|
+
# <Data>: Data name
|
|
186
|
+
# :values: List of possible values
|
|
187
|
+
# <Value>: Value Name attached to the data
|
|
188
|
+
# options: Options
|
|
189
|
+
# :desc: Description of that predefine value.
|
|
190
|
+
|
|
191
|
+
@context = {
|
|
192
|
+
:oCurrentObj => nil, # Defines the Current Object to manipulate
|
|
193
|
+
:needs_optional => nil, # set optional to true for any next needs
|
|
194
|
+
# declaration
|
|
195
|
+
:ClassProcess => nil, # Current Process Class declaration
|
|
196
|
+
:oCurrentData => nil, # Current data model declaration
|
|
197
|
+
:oCurrentObj => nil, # Current object declaration
|
|
198
|
+
:oCurrentKey => nil # Current attribute declaration
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
# Available functions for:
|
|
202
|
+
# - BaseDefinition class declaration
|
|
203
|
+
# - Controler (derived from BaseDefinition) class declaration
|
|
204
|
+
|
|
205
|
+
@query_auto_map = false
|
|
206
|
+
|
|
207
|
+
# Model options
|
|
208
|
+
@options = {}
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Model options (get/set)
|
|
212
|
+
# Uses Hash merge to set model options.
|
|
213
|
+
def options(options = nil)
|
|
214
|
+
@options.merge!(options) unless options.nil?
|
|
215
|
+
@options
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def [](option)
|
|
219
|
+
return nil if option.nil?
|
|
220
|
+
@options[option] if @options.key?(option)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def []=(option, value)
|
|
224
|
+
return nil if option.nil?
|
|
225
|
+
@options[option] = value
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# Current Attribute identifier
|
|
229
|
+
#
|
|
230
|
+
# parameters: (Hash)
|
|
231
|
+
# - +option+ : optional. KeyPath or a string.
|
|
232
|
+
# - if option is a KeyPath, save the keypath.
|
|
233
|
+
# - if option is a Symbol, consider it as function_name for error report.
|
|
234
|
+
#
|
|
235
|
+
# return:
|
|
236
|
+
# - string : KeyPath attribute
|
|
237
|
+
def attribute_context(options = nil)
|
|
238
|
+
function_name = nil
|
|
239
|
+
function_name = options.to_s if options.is_a?(Symbol)
|
|
240
|
+
@context[:oCurrentKey] = options if options.is_a?(KeyPath)
|
|
241
|
+
|
|
242
|
+
msg = ''
|
|
243
|
+
msg += '-' + function_name unless function_name.nil?
|
|
244
|
+
msg += ': No model object attribute context defined. '\
|
|
245
|
+
'Missing attr_mapping or obj_needs?'
|
|
246
|
+
|
|
247
|
+
PrcLib.dcl_fail('%s%s', self.class, msg) if @context[:oCurrentKey].nil?
|
|
248
|
+
|
|
249
|
+
@context[:oCurrentKey]
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# Internal Current Data identifier
|
|
253
|
+
#
|
|
254
|
+
# parameters: (Hash)
|
|
255
|
+
# - +:data+ : optional. Data name to keep in context.
|
|
256
|
+
#
|
|
257
|
+
# return:
|
|
258
|
+
# - string : Data name
|
|
259
|
+
def data_context(data = nil)
|
|
260
|
+
@context[:oCurrentData] = data unless data.nil?
|
|
261
|
+
|
|
262
|
+
data = @context[:oCurrentData]
|
|
263
|
+
|
|
264
|
+
PrcLib.dcl_fail('Config data context not set. at least, you '\
|
|
265
|
+
'need to call define_data before.') if data.nil?
|
|
266
|
+
|
|
267
|
+
data
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
# Internal Current Process identifier
|
|
271
|
+
# parameters: (Hash)
|
|
272
|
+
# - +:process+ : optional. Process name to keep in context.
|
|
273
|
+
# return:
|
|
274
|
+
# - string : Process name
|
|
275
|
+
def process_context(process = nil)
|
|
276
|
+
@context[:ClassProcess] = process unless process.nil?
|
|
277
|
+
process = @context[:ClassProcess]
|
|
278
|
+
|
|
279
|
+
_lorj_dcl_error('Config process context not set. at least, you '\
|
|
280
|
+
'need to call define_data before.') if process.nil?
|
|
281
|
+
|
|
282
|
+
process
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def needs_optional(value = nil)
|
|
286
|
+
@context[:needs_optional] = value unless value.nil? || !value.boolean?
|
|
287
|
+
@context[:needs_optional]
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
def needs_setup(value = nil)
|
|
291
|
+
@context[:needs_setup] = value unless value.nil? || !value.boolean?
|
|
292
|
+
@context[:needs_setup]
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# Object Context identifier (get/set)
|
|
296
|
+
# parameters: (Hash)
|
|
297
|
+
# - +:object+ : optional. Object to keep in context.
|
|
298
|
+
# - +:function_name+ : optional. Symbol. Call function name for error report
|
|
299
|
+
# return:
|
|
300
|
+
# - string : Object name
|
|
301
|
+
def object_context(options = nil)
|
|
302
|
+
if options.is_a?(Hash) && options.key?(:object)
|
|
303
|
+
@context[:oCurrentObj] = options[:object]
|
|
304
|
+
needs_optional false
|
|
305
|
+
needs_setup false
|
|
306
|
+
end
|
|
307
|
+
|
|
308
|
+
msg = ''
|
|
309
|
+
if options.rh_exist?(:function_name) &&
|
|
310
|
+
options[:function_name].is_a?(Symbol)
|
|
311
|
+
msg += '-' + options[:function_name].to_s
|
|
312
|
+
end
|
|
313
|
+
msg += ': No model object context defined. Missing define_obj?'
|
|
314
|
+
|
|
315
|
+
PrcLib.dcl_fail('%s%s', self.class, msg) if @context[:oCurrentObj].nil?
|
|
316
|
+
@context[:oCurrentObj]
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def heap(value = nil)
|
|
320
|
+
@context[:heap] = caller[1..-1] if value.is_a?(TrueClass)
|
|
321
|
+
@context[:heap]
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
def clear_heap
|
|
325
|
+
@context[:heap] = nil
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
end
|
|
@@ -0,0 +1,330 @@
|
|
|
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
|
+
#
|
|
21
|
+
# Those classes describes :
|
|
22
|
+
# - processes (BaseProcess) : How to create/delete/edit/query object.
|
|
23
|
+
# - controler (BaseControler) : If a provider is defined, define how will do
|
|
24
|
+
# object creation/etc...
|
|
25
|
+
# - definition(BaseDefinition): Functions to declare objects, query/data mapping
|
|
26
|
+
# and setup
|
|
27
|
+
# this task to make it to work.
|
|
28
|
+
module Lorj
|
|
29
|
+
# Represents a list of key/value pairs
|
|
30
|
+
# if the value is a Lorj::Data(data or list), the key will be the Lorj::Data
|
|
31
|
+
# type.
|
|
32
|
+
#
|
|
33
|
+
# This class is used in 3 different contexts:
|
|
34
|
+
# - Process context:
|
|
35
|
+
# create/query/update/delete/get handler uses it to build the handler
|
|
36
|
+
# parameters and is passed to each process handler as 2nd parameter.
|
|
37
|
+
# ex: If a connection object is created as follow:
|
|
38
|
+
# define_obj(:connection,
|
|
39
|
+
# :create_e => :connection_create)
|
|
40
|
+
# obj_needs(:data, :uri)
|
|
41
|
+
#
|
|
42
|
+
# then at runtime:
|
|
43
|
+
# lorj_object.create(:connection, :uri => 'http://example.org')
|
|
44
|
+
# will call 'connection_create' (params)
|
|
45
|
+
# def connection_creat(object_type, params)
|
|
46
|
+
# where object_type is ':connection' and
|
|
47
|
+
# params is a 'Lorj::ObjectData' containing :uri value.
|
|
48
|
+
#
|
|
49
|
+
# The object behavior is adapted to the process usage.
|
|
50
|
+
# By default for Lorj::Data(:object), params[aKey] will get or set object
|
|
51
|
+
# attributes.
|
|
52
|
+
# ex: params[:uri] # => 'http://example.org'
|
|
53
|
+
# params[:test] # => nil
|
|
54
|
+
#
|
|
55
|
+
# - Controller context:
|
|
56
|
+
# create/query/update/delete/get handler uses it to build controller
|
|
57
|
+
# parameters like hdata.
|
|
58
|
+
# The object behavior is adapted to the controller usage
|
|
59
|
+
# By default for Lorj::Data(:object), hParams[aKey] will get or set
|
|
60
|
+
# controller object
|
|
61
|
+
#
|
|
62
|
+
# - Internally by BaseDefinition. to get a Lorj::Data cache.
|
|
63
|
+
#
|
|
64
|
+
class ObjectData
|
|
65
|
+
# Initialize the object. By default, usage is for controller context.
|
|
66
|
+
#
|
|
67
|
+
# * *Args* :
|
|
68
|
+
# - +internal+ : Context
|
|
69
|
+
# - true if process context
|
|
70
|
+
# - false if controller context. This is the default value.
|
|
71
|
+
#
|
|
72
|
+
# * *Returns* :
|
|
73
|
+
# - nothing
|
|
74
|
+
#
|
|
75
|
+
# * *Raises* :
|
|
76
|
+
# No exceptions
|
|
77
|
+
def initialize(internal = false)
|
|
78
|
+
@params = {}
|
|
79
|
+
@params[:hdata] = {} unless internal
|
|
80
|
+
@internal = internal
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Get function
|
|
84
|
+
#
|
|
85
|
+
# key can be an array, a string (converted to a symbol) or a symbol.
|
|
86
|
+
#
|
|
87
|
+
# * *Args* :
|
|
88
|
+
# - +key+ : key tree (list of keys)
|
|
89
|
+
# If key[1] == :attrs, get will forcelly use the Lorj::Data object
|
|
90
|
+
# attributes
|
|
91
|
+
# If key[1] == :ObjectData, get will forcelly return the controller
|
|
92
|
+
# object
|
|
93
|
+
# otherwise, get will depends on the context:
|
|
94
|
+
# - controller context: will return the controller object
|
|
95
|
+
# - Process context: will return the Lorj::Data object attributes
|
|
96
|
+
# * *Returns* :
|
|
97
|
+
# value found or nil.
|
|
98
|
+
# * *Raises* :
|
|
99
|
+
# nothing
|
|
100
|
+
def [](*key)
|
|
101
|
+
key = key.flatten
|
|
102
|
+
|
|
103
|
+
return @params if key.length == 0
|
|
104
|
+
|
|
105
|
+
object = @params.rh_get(key[0])
|
|
106
|
+
|
|
107
|
+
# Return ObjectData, attributes if asked. or depends on context.
|
|
108
|
+
value = object_data_get(object, key)
|
|
109
|
+
# otherwise, simply return what is found in keys hierarchy.
|
|
110
|
+
value = @params.rh_get(key) if value.nil?
|
|
111
|
+
|
|
112
|
+
value
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Functions used to set simple data/Object for controller/process function
|
|
116
|
+
# call.
|
|
117
|
+
# TODO: to revisit this function, as we may consider simple data, as
|
|
118
|
+
# Lorj::Data object
|
|
119
|
+
def []=(*key, value)
|
|
120
|
+
return nil if [:object, :query].include?(key[0])
|
|
121
|
+
@params.rh_set(value, key)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Add function. Add a Lorj::Data (data or list) to the ObjectData list.
|
|
125
|
+
#
|
|
126
|
+
# key can be an array, a string (converted to a symbol) or a symbol.
|
|
127
|
+
#
|
|
128
|
+
# * *Args* :
|
|
129
|
+
# - +oDataObject+ : Lorj::Data object
|
|
130
|
+
# * *Returns* :
|
|
131
|
+
# Nothing
|
|
132
|
+
# * *Raises* :
|
|
133
|
+
# nothing
|
|
134
|
+
def add(oDataObject)
|
|
135
|
+
# Requires to be a valid framework object.
|
|
136
|
+
fail Lorj::PrcError.new,
|
|
137
|
+
format("Invalid Framework object type '%s'.",
|
|
138
|
+
oDataObject.class) unless oDataObject.is_a?(Lorj::Data)
|
|
139
|
+
|
|
140
|
+
object_data_add(oDataObject)
|
|
141
|
+
oDataObject.register
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# delete function. delete a Lorj::Data (data or list) from the ObjectData
|
|
145
|
+
# cache.
|
|
146
|
+
#
|
|
147
|
+
# * *Args* :
|
|
148
|
+
# - +object+ : Lorj::Data or Symbol representing a Lorj::Data cached.
|
|
149
|
+
# * *Returns* :
|
|
150
|
+
# Nothing
|
|
151
|
+
# * *Raises* :
|
|
152
|
+
# nothing
|
|
153
|
+
def delete(obj)
|
|
154
|
+
if obj.is_a?(Symbol)
|
|
155
|
+
object_type = obj
|
|
156
|
+
obj = @params[object_type]
|
|
157
|
+
@params[object_type] = nil
|
|
158
|
+
else
|
|
159
|
+
object_data_delete(obj)
|
|
160
|
+
end
|
|
161
|
+
obj.unregister unless obj.nil?
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Merge 2 ObjectData.
|
|
165
|
+
#
|
|
166
|
+
# * *Args* :
|
|
167
|
+
# - +hHash+ : Hash of Lorj::Data. But it is possible to have different
|
|
168
|
+
# object type (not Lorj::Data)
|
|
169
|
+
# * *Returns* :
|
|
170
|
+
# hash merged
|
|
171
|
+
# * *Raises* :
|
|
172
|
+
# nothing
|
|
173
|
+
def <<(hHash)
|
|
174
|
+
@params.merge!(hHash) unless hHash.nil?
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# check Lorj::Data attributes or object exists. Or check key/value pair
|
|
178
|
+
# existence.
|
|
179
|
+
#
|
|
180
|
+
# * *Args* :
|
|
181
|
+
# - +hHash+ : Hash of Lorj::Data. But it is possible to have different
|
|
182
|
+
# object type (not Lorj::Data)
|
|
183
|
+
# * *Returns* :
|
|
184
|
+
# true/false
|
|
185
|
+
# * *Raises* :
|
|
186
|
+
# PrcError
|
|
187
|
+
def exist?(*key) # rubocop: disable Metrics/MethodLength
|
|
188
|
+
fail Lorj::PrcError.new, 'ObjectData: key is not list of values '\
|
|
189
|
+
'(string/symbol or array)' unless [Array, String,
|
|
190
|
+
Symbol].include?(key.class)
|
|
191
|
+
|
|
192
|
+
key = [key] if key.is_a?(Symbol) || key.is_a?(String)
|
|
193
|
+
|
|
194
|
+
key = key.flatten
|
|
195
|
+
|
|
196
|
+
object = @params.rh_get(key[0])
|
|
197
|
+
return false if object.nil?
|
|
198
|
+
|
|
199
|
+
if object.is_a?(Lorj::Data)
|
|
200
|
+
object_data_exist?(object, key)
|
|
201
|
+
else
|
|
202
|
+
# By default true if found key hierarchy
|
|
203
|
+
@params.rh_exist?(key)
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# Determine the type of object identified by a key. Lorj::Data attributes or
|
|
208
|
+
# object exists. Or check key/value pair existence.
|
|
209
|
+
#
|
|
210
|
+
# * *Args* :
|
|
211
|
+
# - +key+ : Key to check in ObjectData list.
|
|
212
|
+
# * *Returns* :
|
|
213
|
+
# - nil if not found
|
|
214
|
+
# - :data if the key value is simply a data
|
|
215
|
+
# - :DataObject if the key value is a Lorj::Data
|
|
216
|
+
# * *Raises* :
|
|
217
|
+
# PrcError
|
|
218
|
+
|
|
219
|
+
def type?(key)
|
|
220
|
+
return nil unless @params.rh_exist?(key)
|
|
221
|
+
:data
|
|
222
|
+
:DataObject if @params[key].type == :object
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# cObj was an old way to get the internal data :object.
|
|
226
|
+
# Replaced by get(:object)
|
|
227
|
+
# def cobj(*key)
|
|
228
|
+
# @params.rh_get(key, :object) if @params.rh_exist?(key, :object)
|
|
229
|
+
# end
|
|
230
|
+
|
|
231
|
+
private
|
|
232
|
+
|
|
233
|
+
# Get function
|
|
234
|
+
#
|
|
235
|
+
# key can be an array of symbol or string (converted to a symbol).
|
|
236
|
+
#
|
|
237
|
+
# * *Args* :
|
|
238
|
+
# - +object+: Lorj::Data object to get data. Must exist.
|
|
239
|
+
# - +key+ : key tree (list of keys)
|
|
240
|
+
# If key[1] == :attrs, get will forcelly use the Lorj::Data object
|
|
241
|
+
# attributes
|
|
242
|
+
# If key[1] == :ObjectData, get will forcelly return the controller
|
|
243
|
+
# object
|
|
244
|
+
# otherwise, get will depends on the context:
|
|
245
|
+
# - controller context: will return the controller object
|
|
246
|
+
# - Process context: will return the Lorj::Data object attributes
|
|
247
|
+
# * *Returns* :
|
|
248
|
+
# value found or nil.
|
|
249
|
+
# * *Raises* :
|
|
250
|
+
# nothing
|
|
251
|
+
def object_data_get(object, *key)
|
|
252
|
+
key = key.flatten
|
|
253
|
+
|
|
254
|
+
return nil unless object.is_a?(Lorj::Data)
|
|
255
|
+
|
|
256
|
+
# Return ObjectData Element if asked. Ignore additional keys.
|
|
257
|
+
return @params[key[0]] if key[1] == :ObjectData
|
|
258
|
+
|
|
259
|
+
# Return attributes if asked
|
|
260
|
+
return object[:attrs, key[2..-1]] if key[1] == :attrs
|
|
261
|
+
|
|
262
|
+
# params are retrieved in process context
|
|
263
|
+
# By default, if key is detected as a framework object, return its
|
|
264
|
+
# data.
|
|
265
|
+
return object[:attrs, key[1..-1]] if @internal
|
|
266
|
+
|
|
267
|
+
# params are retrieved in controller context
|
|
268
|
+
# By default, if key is detected as a controller object, return its
|
|
269
|
+
# data.
|
|
270
|
+
return object[:object, key[1..-1]] unless @internal
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
# Add function. Add a Lorj::Data (data or list) to the ObjectData list.
|
|
274
|
+
#
|
|
275
|
+
# key can be an array, a string (converted to a symbol) or a symbol.
|
|
276
|
+
#
|
|
277
|
+
# * *Args* :
|
|
278
|
+
# - +oDataObject+ : Lorj::Data object
|
|
279
|
+
# * *Returns* :
|
|
280
|
+
# Nothing
|
|
281
|
+
# * *Raises* :
|
|
282
|
+
# nothing
|
|
283
|
+
def object_data_add(oDataObject)
|
|
284
|
+
object_type = oDataObject.object_type?
|
|
285
|
+
|
|
286
|
+
if oDataObject.type == :list
|
|
287
|
+
old_data_object = @params.rh_get(:query, object_type)
|
|
288
|
+
old_data_object.unregister if old_data_object
|
|
289
|
+
@params.rh_set(oDataObject, :query, object_type)
|
|
290
|
+
else
|
|
291
|
+
old_data_object = @params.rh_get(object_type)
|
|
292
|
+
old_data_object.unregister if old_data_object
|
|
293
|
+
@params[object_type] = oDataObject
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
# delete function. delete a Lorj::Data (data or list) from the ObjectData
|
|
298
|
+
# list.
|
|
299
|
+
#
|
|
300
|
+
# * *Args* :
|
|
301
|
+
# - +oDataObject+ : Lorj::Data object
|
|
302
|
+
# * *Returns* :
|
|
303
|
+
# Nothing
|
|
304
|
+
# * *Raises* :
|
|
305
|
+
# nothing
|
|
306
|
+
def object_data_delete(obj)
|
|
307
|
+
fail Lorj::PrcError.new,
|
|
308
|
+
format('ObjectData: delete error. obj is not a'\
|
|
309
|
+
" framework data Object. Is a '%s'",
|
|
310
|
+
obj.class) unless obj.is_a?(Lorj::Data)
|
|
311
|
+
if obj.type == :list
|
|
312
|
+
@params.rh_set(nil, :query, obj.object_type?)
|
|
313
|
+
else
|
|
314
|
+
object_type = obj.object_type?
|
|
315
|
+
@params[object_type] = nil
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def object_data_exist?(object, key)
|
|
320
|
+
# Return true if ObjectData Element is found when asked.
|
|
321
|
+
return true if key[1] == :ObjectData && object.type?(key[0]) == :object
|
|
322
|
+
|
|
323
|
+
# Return true if attritutes or controller object attributes found when
|
|
324
|
+
# asked.
|
|
325
|
+
return object.exist?(key[2..-1]) if key[1] == :attrs
|
|
326
|
+
return object.exist?(key[1..-1]) if key.length > 1
|
|
327
|
+
true
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
end
|