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.
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
@@ -15,498 +15,676 @@
15
15
  # See the License for the specific language governing permissions and
16
16
  # limitations under the License.
17
17
 
18
+ # rubocop: disable Metrics/AbcSize
19
+
18
20
  # Module Lorj which contains several classes.
19
21
  #
20
22
  # Those classes describes :
21
23
  # - processes (BaseProcess) : How to create/delete/edit/query object.
22
- # - controler (BaseControler) : If a provider is defined, define how will do object creation/etc...
23
- # - definition(BaseDefinition): Functions to declare objects, query/data mapping and setup
24
+ # - controler (BaseControler) : If a provider is defined, define how will do
25
+ # object creation/etc...
26
+ # - definition(BaseDefinition): Functions to declare objects, query/data mapping
27
+ # and setup
24
28
  # this task to make it to work.
25
29
  module Lorj
26
- class BaseDefinition
27
-
28
- private
29
-
30
- ###################################################
31
- # Class management Section
32
- ###################################################
33
-
34
- # Meta Object declaration structure
35
- # <Object>
36
- # :query_mapping List of keypath mapped.
37
- # <keypath> = <keypath mapped>
38
- # :lambdas:
39
- # :create_e: function to call at 'Create' task
40
- # :delete_e: function to call at 'Delete' task
41
- # :update_e: function to call at 'Update' task
42
- # :get_e: function to call at 'Get' task
43
- # :query_e: function to call at 'Query' task
44
- # :value_mapping: Define list of Object's key values mapping.
45
- # <keypath> key value mapping lists
46
- # <value> = <map> Define the value mapping.
47
- # :returns
48
- # <keypath> key value to extract from controller object.
49
- # :params: Defines CloudData (:data) or CloudObj (:CloudObj) needs by the <Object>
50
- # :keys: Contains keys in a tree of hash.
51
- # <keypath>: String. One element (string with : and /) of :list defining the key
52
- # :type: :data or :CloudObj
53
- # :for: Array of events which requires the data or CloudObj to work.
54
- # :mapping: To automatically create a provider hash data mapped (hdata).
55
- # :required: True if this parameter is required.
56
- # :extract_from: Array. Build the keypath value from another hParams value.
57
- # Ex: This example will extract :id from :security_groups object
58
- # :extract_from => [:security_groups, :id]
59
- #
60
- @@meta_obj = {}
61
-
62
- # meta data are defined in defaults.yaml and loaded in Lorj::Default class definition
63
- # Cloud provider can redefine ForjData defaults and add some extra parameters.
64
- # To get Application defaults, read defaults.yaml, under :sections:
65
- # <Section>:
66
- # <Data>: Required. Symbol/String. default: nil
67
- # => Data name. This symbol must be unique, across sections.
68
- # :desc: Required. String. default: nil
69
- # => Description
70
- # :explanation: |- Print a multiline explanation before ask the key value.
71
- # ERB template enable. To get config data, type <%= config[...] %>
72
- # :readonly: Optional. true/false. Default: false
73
- # => oForjConfig.set() will fail if readonly is true.
74
- # Can be set, only thanks to oForjConfig.setup()
75
- # or using private oForjConfig._set()
76
- # :account_exclusive: Optional. true/false. Default: false
77
- # => Only oConfig.account_get/set() can handle the value
78
- # oConfig.set/get cannot.
79
- # :account: Optional. default: False
80
- # => setup will configure the account with this <Data>
81
- # :ask_sort: Number which represents the ask order in the step group. (See /:setup/:ask_step for details)
82
- # :after: <Data> Name of the previous <Data> to ask before the current one.
83
- # :depends_on:
84
- # => Identify :data type required to be set before the current one.
85
- # :default_value: Default value at setup time. This is not necessarily the Application default value (See /:default)
86
- # :validate: Regular expression to validate end user input during setup.
87
- # :value_mapping: list of values to map as defined by the controller
88
- # :controller: mapping for get controller value from process values
89
- # <value> : <map> value map equivalence. See data_value_mapping function
90
- # :process: mapping for get process value from controller values
91
- # <value> : <map> value map equivalence. See data_value_mapping function
92
- # :defaut: Default value
93
- # :list_values: Defines a list of valid values for the current data.
94
- # :query_type :controller_call to execute a function defined in the controller object.
95
- # :process_call to execute a function defined in the process object.
96
- # :values to get list of values from :values.
97
- # :object Object to load before calling the function. Only :query_type = :*_call
98
- # :query_call Symbol. function name to call. Only :query_type = :*_call
99
- # function must return an Array.
100
- # :query_params Hash. Controler function parameters. Only :query_type = :*_call
101
- # :validate :list_strict. valid only if value is one of those listed.
102
- # :values:
103
- # to retrieve from.
104
- # otherwise define simply a list of possible values.
105
- # :ask_step: Step number. By default, setup will determine the step, thanks to meta lorj object dependencies tree.
106
- # This number start at 0. Each step can be defined by /:setup/:ask_step/<steps> list.
107
- #
108
- # :setup: This section describes group of fields to ask, step by step.
109
- # :ask_step: Define an Array of setup steps to ask to the end user. The step order is respected, and start at 0
110
- # - :desc: Define the step description. ERB template enable. To get config data, type config[...]
111
- # :explanation: |- Define a multiline explanation. This is printed out in brown color.
112
- # ERB template enable. To get config data, type <%= config[...] %>
113
- # :add: Define a list of additionnal fields to ask.
114
- # - <Data> Data to ask.
115
- #
116
- # :default: List of <Data> application default values.
117
- # <Data> : Value to use at the config application level.
118
- @@meta_data = {}
119
-
120
- # The Generic Process can pre-define some data and value (function predefine_data)
121
- # The Generic Process (and external framework call) only knows about Generic data.
122
- # information used
123
- #
124
- @@meta_predefined_values = {}
125
-
126
- # <Data>: Data name
127
- # :values: List of possible values
128
- # <Value>: Value Name attached to the data
129
- # options: Options
130
- # :desc: Description of that predefine value.
131
-
132
- @@Context = {
133
- :oCurrentObj => nil, # Defines the Current Object to manipulate
134
- :needs_optional => nil, # set optional to true for any next needs declaration
135
- :ClassProcess => nil # Current Process Class declaration
136
- }
137
-
138
- # Available functions for:
139
- # - BaseDefinition class declaration
140
- # - Controler (derived from BaseDefinition) class declaration
141
-
142
- @@query_auto_map = false
143
-
144
- def self.current_process (cProcessClass)
145
- @@Context[:ClassProcess] = cProcessClass
146
- end
147
-
148
- def self.obj_needs_optional
149
- @@Context[:needs_optional] = true
150
- end
151
-
152
- def self.obj_needs_requires
153
- @@Context[:needs_optional] = false
30
+ # Global Process functions
31
+ class BaseDefinition
32
+ # Process declaration
33
+ # Defines current Process context
34
+ #
35
+ # parameters:
36
+ # - +process_class+ : Process Class object.
37
+ #
38
+ def self.current_process(cProcessClass)
39
+ PrcLib.model.heap true
40
+ PrcLib.model.process_context(cProcessClass)
41
+ end
42
+
43
+ # Process declaration
44
+ # Set obj_needs requirement setting to false
45
+ #
46
+ def self.obj_needs_optional
47
+ PrcLib.model.heap true
48
+ PrcLib.model.needs_optional true
49
+ end
50
+
51
+ # Process declaration
52
+ # Set obj_needs requirement setting to True
53
+ #
54
+ def self.obj_needs_requires
55
+ PrcLib.model.heap true
56
+ PrcLib.model.needs_optional false
57
+ end
58
+
59
+ # Process declaration
60
+ # Defines default process options
61
+ #
62
+ # parameters:
63
+ # - +options+ : Supported options are:
64
+ # - use_controller : Boolean. True if the model require a controller
65
+ # False otherwise. Default is true.
66
+ #
67
+ def self.process_default(hOptions)
68
+ PrcLib.model.heap true
69
+ supported_options = [:use_controller]
70
+ unless hOptions.nil?
71
+ hOptions.each_key do | key |
72
+ case key
73
+ when :use_controller
74
+ value = hOptions.rh_get(:use_controller)
75
+ next unless value.boolean?
76
+ PrcLib.model[key] = hOptions[key]
77
+ else
78
+ PrcLib.dcl_fail("Unknown default process options '%s'. "\
79
+ "Supported are '%s'",
80
+ key, supported_options.join(','))
81
+ end
82
+ end
154
83
  end
155
-
156
- def self.process_default(hOptions)
157
- aSupportedOptions = [:use_controller]
158
- unless hOptions.nil?
159
- hOptions.each_key { | key |
160
- case key
161
- when :use_controller
162
- value = Lorj::rhGet(hOptions, :use_controller)
163
- next unless value.is_a?(TrueClass) or value.is_a?(FalseClass)
164
- Lorj::rhSet(@@Context, hOptions[key], :options, key)
165
- else
166
- raise Lorj::PrcError.new, "Unknown default process options '%s'. Supported are '%s'" % [key, aSupportedOptions.join(',')]
167
- end
168
- }
169
- end
170
-
84
+ end
85
+ end
86
+
87
+ # Base definition class for Process declaration
88
+ class BaseDefinition
89
+ # Application process or controller to defines an object.
90
+ #
91
+ # The context will be set by this definition for next declaration.
92
+ # Depending on the context, define_obj is not used identically:
93
+ #
94
+ # *Context* : Application Process
95
+ # 'define_obj' is the first object declaration. It sets the object context
96
+ # for next declaration.
97
+ # At least it needs to create an handler or define it with :nohandler: true
98
+ #
99
+ # Usually, this definition is followed by:
100
+ # - def_attribute : Object attribute list
101
+ # - obj_needs : Handler parameters needs
102
+ # - undefine_attribute : Remove predefined attributes.
103
+ # - def_query_attribute: Query attribute definition
104
+ #
105
+ # *Context*: Controller
106
+ #
107
+ # A controller uses define_obj to update an existing object.
108
+ # A controller can create a new object, only if the controller
109
+ # defines specific process.
110
+ #
111
+ # Usually, this definition is followed by:
112
+ # - query_mapping : Adapt query attribute to match controller query
113
+ # settings
114
+ # - obj_needs : Adapt needed parameters, and/or set mapping.
115
+ # - def_hdata : Define Controller Hash parameter, for handlers.
116
+ # - def_attr_mapping : Define object attribute mapping.
117
+ # - data_value_mapping: Define Data model values mapping.
118
+ #
119
+ def self.define_obj(obj_type_name, handlers = nil)
120
+ return nil unless [NilClass, String, Symbol].include?(obj_type_name.class)
121
+ PrcLib.model.heap true
122
+
123
+ obj_type_name = obj_type_name.to_sym if obj_type_name.is_a?(String)
124
+
125
+ handlers = {} unless handlers.is_a?(Hash)
126
+
127
+ lorj_object = PrcLib.model.meta_obj.rh_get(obj_type_name)
128
+
129
+ # Checking handlers_options data
130
+ handlers = _verify_handlers(obj_type_name, lorj_object, handlers)
131
+
132
+ if lorj_object.nil?
133
+ lorj_object = _define_obj_initialize(obj_type_name, handlers)
134
+ PrcLib.model.meta_obj.rh_set(lorj_object, obj_type_name)
171
135
  end
172
136
 
173
- # Defines Object and connect to functions events
174
- def self.define_obj(sCloudObj, hParam = nil)
175
- return nil if not sCloudObj
176
- return nil if not [String, Symbol].include?(sCloudObj.class)
177
-
178
- aCaller = caller
179
- aCaller.pop
180
-
181
- sCloudObj = sCloudObj.to_sym if sCloudObj.class == String
182
- @@Context[:oCurrentObj] = sCloudObj
183
- @@Context[:needs_optional] = false
184
- @@Context[:needs_setup] = false
185
- bController = Lorj::rhGet(@@Context, :options, :use_controller)
186
- bController = true if bController.nil?
187
-
188
- if not [Hash].include?(hParam.class)
189
- if Lorj::rhExist?(@@meta_obj, sCloudObj) != 1
190
- raise Lorj::PrcError.new(), "New undefined object '%s' requires at least one handler. Ex: define_obj :%s, :create_e => myhandler " % [sCloudObj, sCloudObj]
191
- end
192
- hParam = {}
193
- end
194
-
195
- oCloudObj = Lorj::rhGet(@@meta_obj, sCloudObj)
196
- if not oCloudObj
197
- oCloudObj = {
198
- :lambdas => {:create_e => nil, :delete_e => nil, :update_e => nil, :get_e => nil, :query_e => nil, :get_attr_e => nil},
199
- :params => {},
200
- :options => {:controller => bController },
201
- :query_mapping => { ":id" => ":id", ":name" => ":name"},
202
- :returns => {":id" => ":id", ":name" => ":name"}
203
- }
204
- msg = nil
205
- else
206
- msg = ""
207
- end
208
-
209
- sObjectName = "'%s.%s'" % [self.class, sCloudObj]
210
-
211
- # Checking hParam data
212
- if not Lorj::rhGet(hParam, :nohandler)
213
- hParam.each_key do | key |
214
- raise Lorj::PrcError.new(), "'%s' parameter is invalid. Use '%s'" % [key, oCloudObj[:lambdas].keys.join(', ')], aCaller if Lorj::rhExist?(oCloudObj, :lambdas, key)!= 2
215
- end
216
- msg = "%-28s object declared." % [sObjectName] if not msg
217
- else
218
- msg = "%-28s meta object declared." % [sObjectName] if not msg
219
- end
220
- Lorj.debug(2, msg) if msg != ""
221
-
222
- # Setting procs
223
- Lorj::rhGet(oCloudObj, :lambdas).each_key { |key|
224
- next if not hParam.key?(key)
225
-
226
- if not @@Context[:ClassProcess].instance_methods.include?(hParam[key])
227
- raise Lorj::PrcError.new(), "'%s' parameter requires a valid instance method '%s' in the process '%s'." % [key, hParam[key], @@Context[:ClassProcess]], aCaller
228
- end
229
- if hParam[key] == :default
230
- # By default, we use the event name as default function to call.
231
- # Those function are predefined in ForjController
232
- # The Provider needs to derive from ForjController and redefine those functions.
233
- oCloudObj[:lambdas][key] = key
234
- else
235
- # If needed, ForjProviver redefined can contains some additionnal functions
236
- # to call.
237
- oCloudObj[:lambdas][key] = hParam[key]
238
- end
239
- }
240
- Lorj::rhSet(@@meta_obj, oCloudObj, sCloudObj)
137
+ PrcLib.model.object_context(:object => obj_type_name)
138
+
139
+ _handler_settings(lorj_object, handlers)
140
+
141
+ nil
142
+ end
143
+
144
+ # Application process to defines query attributes.
145
+ def self.def_query_attribute(key)
146
+ PrcLib.model.heap true
147
+ query_mapping(key, key)
148
+ end
149
+
150
+ # Available functions exclusively for Controller (derived from
151
+ # BaseDefinition) class declaration
152
+
153
+ # Following functions are related to Object Attributes
154
+ # ----------------------------------------------------
155
+
156
+ # Function to declare handlers data/object needs.
157
+ # Used by application process declaration and controller declaration
158
+ # to defines the object data needs or sub-object dependency.
159
+ #
160
+ # The application process declare global objects/data dependency
161
+ # while the controller can complete it with any needed other object/data
162
+ # as required by the controller code.
163
+ #
164
+ # Ex: A process can define a generic connection object.
165
+ # define_obj :connection
166
+ # obj_needs :data, :user, :for => [:create_e]
167
+ # obj_needs :data, :passwd, :for => [:create_e]
168
+ #
169
+ # The controller can add several other needs, specifically
170
+ # to this controller.
171
+ #
172
+ # define_obj :connection
173
+ # obj_needs :data, :user, mapping => :username
174
+ # obj_needs :data, :uri
175
+ #
176
+ # Requires Object context
177
+ #
178
+ # parameters:
179
+ # - +type+ : :data or :object requirement
180
+ # - +name+ : Name of the data or the object.
181
+ # - +options+ : Possible options
182
+ # - :for : Array: requirement for a limited list of handler.
183
+ # By default, all handlers requires this data or object.
184
+ def self.obj_needs(type, name, options = {})
185
+ return nil unless [String, Symbol].include?(type.class)
186
+ return nil unless [String, Symbol, Array].include?(name.class)
187
+ PrcLib.model.heap true
188
+
189
+ type = type.to_sym if type.is_a?(String)
190
+
191
+ options = {} unless options.is_a?(Hash)
192
+
193
+ unless options.key?(:required)
194
+ options[:required] = !PrcLib.model.needs_optional
241
195
  end
242
196
 
243
- def self.def_query_attribute(key)
244
- self.query_mapping(key, key)
197
+ _configure_options_handlers(options)
198
+
199
+ params = PrcLib.model.meta_obj.rh_get(PrcLib.model.object_context,
200
+ :params)
201
+
202
+ _define_object_needs(params, type,
203
+ _initialize_object_needs(name), options)
204
+ end
205
+
206
+ # Function used by the Process to define Model Object attributes.
207
+ # By default, any attributes are queriable as well. No need to call
208
+ # query_mapping
209
+ #
210
+ # parameters:
211
+ # - +key+ : name of the default object attribute
212
+ # - +options+: optional.
213
+ def self.def_attribute(key, options = {})
214
+ PrcLib.model.heap true
215
+
216
+ key_path = _set_attr_mapping(key, key, options)[0]
217
+
218
+ Lorj.debug(4, "%s: Defining object attribute '%s'",
219
+ PrcLib.model.object_context, key_path)
220
+ end
221
+
222
+ # Process to undeclare default lorj object attributes
223
+ # By default, while process declares a new lorj object,
224
+ # :id and :name are predefined.
225
+ # If the model of this lorj object do not have any ID or Name
226
+ # the process will needs to undeclare it.
227
+ #
228
+ # The Controller can undeclare some attribute defined by the
229
+ # Application process model. But it requires the controller to
230
+ # re-define any object handler which can use those attributes.
231
+ #
232
+ # parameters:
233
+ # - +key+ : Attribute name to undeclare.
234
+ def self.undefine_attribute(key)
235
+ return nil unless [String, Symbol].include?(key.class)
236
+ PrcLib.model.heap true
237
+
238
+ PrcLib.dcl_fail('%s: No Object defined. Missing define_obj?',
239
+ self.class) if PrcLib.model.object_context.nil?
240
+
241
+ key_path = KeyPath.new(key)
242
+
243
+ PrcLib.model.meta_obj.rh_set(nil, PrcLib.model.object_context,
244
+ :returns, key_path.sFullPath)
245
+ PrcLib.model.attribute_context key_path
246
+ Lorj.debug(4, "%s: Undefining attribute mapping '%s'",
247
+ PrcLib.model.object_context, key_path.sFullPath)
248
+
249
+ _query_mapping(key, nil)
250
+ end
251
+
252
+ # Process or controller to defines or update data model options
253
+ # Possible data model options are defined definition under <section>/<data>
254
+ # of defaults.yaml
255
+ #
256
+ # Parameters:
257
+ # - +data+ : String/Symbol. Name of the data
258
+ # - +options+ : Hash. List of options
259
+ def self.define_data(data, options)
260
+ return nil unless [String, Symbol].include?(data.class)
261
+ return nil if options.class != Hash
262
+ PrcLib.model.heap true
263
+
264
+ data = data.to_sym if data.class == String
265
+ PrcLib.dcl_fail("%s: Config data '%s' unknown",
266
+ self.class, data) unless Lorj.defaults.meta_exist?(data)
267
+
268
+ PrcLib.model.data_context data
269
+
270
+ section = _section_from(data)
271
+
272
+ cur_options = PrcLib.model.meta_data.rh_get(section, data)
273
+ return cur_options.merge!(options) if cur_options
274
+ PrcLib.model.meta_data.rh_set(options, section, data)
275
+ end
276
+
277
+ # Controller to declare a model Data value mapping
278
+ #
279
+ # Parameters:
280
+ # - +value+ : Value to map
281
+ # - +map+ : Value mapped
282
+ def self.data_value_mapping(value, map)
283
+ return nil unless _decl_data_valid?(value, map)
284
+ PrcLib.model.heap true
285
+
286
+ data = PrcLib.model.data_context
287
+
288
+ section = _section_from(data)
289
+
290
+ Lorj.debug(2, format("%s/%s: Define config data value mapping: '%s' => "\
291
+ "'%s'", section, data, value, map))
292
+ PrcLib.model.meta_data.rh_set(map, section, data,
293
+ :value_mapping, :controller, value)
294
+ PrcLib.model.meta_data.rh_set(value, section, data,
295
+ :value_mapping, :process, map)
296
+ end
297
+
298
+ def self.defined?(objType)
299
+ PrcLib.model.heap true
300
+ @obj_type.include?(objType)
301
+ end
302
+
303
+ # Internal BaseDefinition function
304
+
305
+ def self.predefine_data_value(data, hOptions)
306
+ PrcLib.model.heap true
307
+ # Refuse to run if not a
308
+ return nil if self.class != BaseDefinition
309
+ # BaseDefinition call
310
+ return nil unless [String, Symbol].include?(value.class)
311
+ return nil unless [NilClass, Symbol, String].include?(map.class)
312
+
313
+ key_path = PrcLib.model.attribute_context
314
+
315
+ value = { data => { :options => hOptions } }
316
+
317
+ PrcLib.model.predefine_data_value.rh_set(value,
318
+ key_path.sFullPath, :values)
319
+ end
320
+
321
+ # function to interpret a template data, and use ERBConfig as data context.
322
+ # ERBConfig contains config object only.
323
+ def erb(str)
324
+ ERB.new(str).result(@erb_config.get_binding)
325
+ end
326
+ end
327
+
328
+ ##############################################################
329
+ # Completing BaseDefinition with Exclusive controller functions
330
+ class BaseDefinition
331
+ attr_writer :obj_type
332
+
333
+ # Controller declaration to map an query attribute
334
+ # By default, def_attribute configure those attributes as queriable.
335
+ # The controller can redefine the query part.
336
+ def self.query_mapping(key, map)
337
+ PrcLib.model.heap true
338
+ _query_mapping(key, map)
339
+ end
340
+
341
+ # Function used by the controler to define mapping.
342
+ # By default, any attributes are queriable as well. No need to call
343
+ # query_mapping
344
+ #
345
+ # parameters:
346
+ # - +key+ : name of the default object attribute
347
+ # - +map+ : optional.
348
+ # - +options+: optional.
349
+ def self.def_attr_mapping(key, map, options = {})
350
+ PrcLib.model.heap true
351
+ key_paths = _set_attr_mapping(key, map, options)
352
+
353
+ Lorj.debug(4, "%s: Defining object attribute mapping '%s' => '%s'",
354
+ PrcLib.model.object_context, key_paths[0], key_paths[1])
355
+ end
356
+
357
+ # Controller to declare an lorj object attribute data mapping.
358
+ #
359
+ # You need to define object and attribute context before attr_value_mapping
360
+ #
361
+ # parameters:
362
+ # - +value+ : name of the default object attribute
363
+ # - +map+ : Map a predefined object attribute value.
364
+ #
365
+ # Ex: If the application model has defined:
366
+ # :server[:status] = [:create, :boot, :active]
367
+ #
368
+ # define_obj :server # Required to set object context
369
+ # get_attr_mapping :status, :state # set attribute mapping and context.
370
+ # attr_value_mapping :create, 'BUILD'
371
+ # attr_value_mapping :boot, :boot
372
+ # attr_value_mapping :active, 'ACTIVE'
373
+ #
374
+ def self.attr_value_mapping(value, map)
375
+ PrcLib.model.heap true
376
+
377
+ return nil unless _decl_data_valid?(value, map)
378
+
379
+ object_type = PrcLib.model.object_context(fct_context)
380
+
381
+ key_path = PrcLib.model.attribute_context __callee__
382
+
383
+ keypath = key_path.sFullPath
384
+ Lorj.debug(2, "%s-%s: Attribute value mapping '%s' => '%s'",
385
+ object_type, key_path.to_s, value, map)
386
+ PrcLib.model.meta_obj.rh_set(map,
387
+ object_type, :value_mapping, keypath, value)
388
+ end
389
+
390
+ # Controller to declare predefined Hash options for controller wrapper code.
391
+ #
392
+ # When a controller wrapper code is called to execute a function,
393
+ # the controller may/should provides some options.
394
+ #
395
+ # lorj framework can simplify the way to call this function
396
+ # and provide a predefined options list, prepared by lorj.
397
+ #
398
+ # Ex: If you are calling a connection function, which requires one
399
+ # or more parameters passed as an Hash:
400
+ # wrapper code without using :hdata:
401
+ # def connect(params)
402
+ # options = { :hp_access_key => params[:account_id],
403
+ # :hp_secret_key => params[:account_key]
404
+ # :hp_auth_uri => params[:auth_uri]
405
+ # :hp_tenant_id => params[:tenant]
406
+ # :hp_avl_zone => params[:network]
407
+ # }
408
+ # Fog::HP::Network.new(options)
409
+ # end
410
+ #
411
+ # wrapper code using :hdata and def_hdata:
412
+ # def connect(params)
413
+ # Fog::HP::Network.new(params[:hdata])
414
+ # end
415
+ #
416
+ # def_hdata requires the object context.
417
+ # Ex:
418
+ # define_obj(:student)
419
+ # def_hdata :first_name
420
+ # def_hdata :last_name
421
+ # def_hdata :course, mapping: :training
422
+ #
423
+ # parameters:
424
+ # - +attr_name+ : Attribute name to add in :hdata Hash
425
+ # as hdata[attr_name] = value.
426
+ # - +options+: Possible options:
427
+ # - :mapping : map name to use mapping instead of attr_name.
428
+ # hdata[map_name] = value
429
+ #
430
+ #
431
+ def self.def_hdata(attr_name, options = {})
432
+ PrcLib.model.heap true
433
+ fct_context = { :function_name => __callee__ }
434
+ return nil unless [String, Symbol, Array].include?(attr_name.class)
435
+
436
+ options = {} unless options.is_a?(Hash)
437
+
438
+ object_type = PrcLib.model.object_context(fct_context)
439
+
440
+ key_access = KeyPath.new(attr_name).sFullPath
441
+
442
+ # PrcLib.model.meta_obj://<Object>/:params/:keys/<keypath> must exist.
443
+ object_param = PrcLib.model.meta_obj.rh_get(object_type,
444
+ :params, :keys, key_access)
445
+
446
+ object_param[:mapping] = attr_name
447
+ object_param[:mapping] = options[:mapping] unless options[:mapping].nil?
448
+
449
+ Lorj.debug(2, "%-28s: hdata set '%s' => '%s'",
450
+ _object_name(object_type), attr_name, object_param[:mapping])
451
+
452
+ # Internally, lorj stores this declaration in
453
+ # PrcLib.model.meta_obj://<Object>/:params/:keys/<keypath>/:mapping)
454
+ end
455
+ end
456
+
457
+ ##############################################################
458
+ # Completing BaseDefinition with internal functions
459
+ class BaseDefinition
460
+ # Internal function
461
+ def self._query_mapping(key, map)
462
+ return nil unless [String, Symbol].include?(key.class)
463
+ return nil unless [NilClass, Symbol, String].include?(map.class)
464
+
465
+ object_type = PrcLib.model.object_context
466
+ key_path = KeyPath.new(key)
467
+ map_path_obj = KeyPath.new(map)
468
+
469
+ PrcLib.model.attribute_context key_path
470
+
471
+ PrcLib.model.meta_obj.rh_set(map_path_obj.sFullPath, object_type,
472
+ :query_mapping, key_path.sFullPath)
473
+ end
474
+
475
+ # Internal function to store object attribute and mapping information
476
+ # parameters:
477
+ # - +key+ : KeyPath. key object
478
+ # - +map+ : KeyPath. map object
479
+ # - +options+: Hash. Options to set.
480
+ def self._set_attr_mapping(key, map, options)
481
+ return nil unless _decl_object_attr_valid?(key, map, options)
482
+
483
+ object_type = PrcLib.model.object_context
484
+ key_path_obj = KeyPath.new(key)
485
+
486
+ map_path_obj = KeyPath.new(map)
487
+
488
+ PrcLib.model.meta_obj.rh_set(map_path_obj.sFullPath, object_type,
489
+ :returns, key_path_obj.sFullPath)
490
+
491
+ PrcLib.model.attribute_context key_path_obj
492
+
493
+ return if options[:not_queriable] == true
494
+ query_mapping(key, map)
495
+ [key_path_obj.sFullPath, map_path_obj.sFullPath]
496
+ end
497
+
498
+ # Internal section detection
499
+ def self._section_from(data)
500
+ section = Lorj.defaults.get_meta_section(data)
501
+ section = :runtime if section.nil?
502
+
503
+ section
504
+ end
505
+
506
+ # Internal model data validation
507
+ # return true if valid. false otherwise.
508
+ def self._decl_object_attr_valid?(key, map, options)
509
+ return false unless [String, Symbol].include?(key.class)
510
+ return false unless options.is_a?(Hash)
511
+ return false unless [Symbol, String, Array].include?(map.class)
512
+
513
+ true
514
+ end
515
+
516
+ # Internal model data validation
517
+ # return true if valid. false otherwise.
518
+ def self._decl_data_valid?(value, map)
519
+ return false if [String, Symbol].include?(value.class)
520
+ return false if [NilClass, Symbol, String].include?(map.class)
521
+ true
522
+ end
523
+
524
+ # Internal function to get the object_name as class.object
525
+ #
526
+ # return formated string.
527
+ def self._object_name(name)
528
+ format("'%s.%s'", self.class, name)
529
+ end
530
+
531
+ # Internal function for obj_needs
532
+ # Initialize :params/:keys/
533
+ def self._initialize_object_needs(name)
534
+ top_param_obj = PrcLib.model.meta_obj.rh_get(PrcLib.model.object_context,
535
+ :params)
536
+
537
+ PrcLib.model.attribute_context KeyPath.new(name)
538
+ key_access = PrcLib.model.attribute_context.sFullPath
539
+
540
+ unless top_param_obj[:keys].key?(key_access)
541
+ top_param_obj[:keys][key_access] = {}
542
+ return 'New'
245
543
  end
246
544
 
247
- def self.query_mapping(key, map)
248
- return nil if not [String, Symbol].include?(key.class)
249
- return nil if not [NilClass, Symbol, String].include?(map.class)
250
-
251
- aCaller = caller
252
- aCaller.pop
253
-
254
- raise Lorj::PrcError.new(), "%s: No Object defined. Missing define_obj?" % [ self.class], aCaller if @@Context[:oCurrentObj].nil?
255
-
256
- sCloudObj = @@Context[:oCurrentObj]
257
- oKeyPath = KeyPath.new(key)
258
- oMapPath = KeyPath.new(map)
545
+ 'Upd'
546
+ end
259
547
 
260
- @@Context[:oCurrentKey] = oKeyPath
548
+ # Internal function
549
+ def self._define_object_needs(params, type, msg_action, options)
550
+ attribute = PrcLib.model.attribute_context
261
551
 
262
- Lorj::rhSet(@@meta_obj, oMapPath.sFullPath, sCloudObj, :query_mapping, oKeyPath.sFullPath)
552
+ case type
553
+ when :data
554
+ return _obj_needs_data(params[:keys][attribute.sFullPath],
555
+ msg_action, options)
556
+ when :CloudObject, :object
557
+ return _obj_needs_object(params[:keys][attribute.sFullPath],
558
+ options)
263
559
  end
264
-
265
- # Available functions exclusively for Controler (derived from BaseDefinition) class declaration
266
-
267
- # Following functions are related to Object Attributes
268
- # ----------------------------------------------------
269
-
270
- # Defines Object CloudData/CloudObj dependency
271
- def self.obj_needs(sType, sParam, hParams = {})
272
- return nil if not [String, Symbol].include?(sType.class)
273
- return nil if not [String, Symbol, Array].include?(sParam.class)
274
-
275
- hParams = {} if not hParams
276
-
277
- hParams[:required] = not(@@Context[:needs_optional]) if Lorj::rhExist?(hParams, :required) != 1
278
-
279
- aCaller = caller
280
- aCaller.pop
281
-
282
- raise Lorj::PrcError.new(), "%s: No Object defined. Missing define_obj?" % [ self.class], aCaller if @@Context[:oCurrentObj].nil?
283
-
284
- sCloudObj = @@Context[:oCurrentObj]
285
-
286
- aForEvents = Lorj::rhGet(@@meta_obj, sCloudObj, :lambdas).keys
287
- hParams = hParams.merge({ :for => aForEvents}) if not hParams.key?(:for)
288
- sType = sType.to_sym if sType.class == String
289
-
290
-
291
- raise Lorj::PrcError.new(), "%s: '%s' not declared. Missing define_obj(%s)?" % [ self.class, sCloudObj, sCloudObj], aCaller if Lorj::rhExist?(@@meta_obj, sCloudObj) != 1
292
-
293
- oObjTopParam = Lorj::rhGet(@@meta_obj, sCloudObj, :params)
294
- if not oObjTopParam.key?(:keys)
295
- # Initialize top structure
296
-
297
- oObjTopParam.merge!({ :keys => {} })
298
- end
299
-
300
- oKeyPath = KeyPath.new(sParam)
301
- sKeyAccess = oKeyPath.sFullPath
302
-
303
- @@Context[:oCurrentKey] = oKeyPath
304
-
305
- oCloudObjParam = Lorj::rhGet(oObjTopParam, :keys, sKeyAccess)
306
- if oCloudObjParam.nil?
307
- sMsgAction = "New"
308
- oObjTopParam[:keys][sKeyAccess] = {}
309
- oCloudObjParam = oObjTopParam[:keys][sKeyAccess]
310
- else
311
- sMsgAction = "Upd"
312
- end
313
- sObjectName = "'%s.%s'" % [self.class, sCloudObj]
314
- case sType
315
- when :data
316
- if Lorj::Default.meta_exist?(sParam)
317
- Lorj.debug(2, "%-28s: %s predefined config '%s'." % [sObjectName, sMsgAction, sParam])
318
- else
319
- Lorj.debug(2, "%-28s: %s runtime config '%s'." % [sObjectName, sMsgAction, sParam])
320
- end
321
- oCloudObjParam.merge!( hParams.merge({:type => sType}) ) # Merge from predefined params, but ensure type is never updated.
322
- when :CloudObject
323
- raise Lorj::PrcError.new(), "%s: '%s' not declared. Missing define_obj(%s)?" % [self.class, sParam, sParam], aCaller if not @@meta_obj.key?(sParam)
324
- oCloudObjParam.merge!( hParams.merge({:type => sType}) ) # Merge from predefined params, but ensure type is never updated.
325
- else
326
- raise Lorj::PrcError.new(), "%s: Object parameter type '%s' unknown." % [ self.class, sType ], aCaller
327
- end
560
+ PrcLib.dcl_fail("%s: Object parameter type '%s' unknown.",
561
+ self.class, type)
562
+ end
563
+
564
+ # Internal function
565
+ def self._configure_options_handlers(options)
566
+ for_events = PrcLib.model.meta_obj.rh_get(PrcLib.model.object_context,
567
+ :lambdas).keys
568
+ options.merge(:for => for_events) unless options.key?(:for)
569
+ end
570
+ end
571
+
572
+ ##############################################################
573
+ # Completing BaseDefinition with internal functions for obj_needs
574
+ class BaseDefinition
575
+ # Internal function
576
+ def self._obj_needs_data(object_attr, msg_action, new_params)
577
+ attr_name = PrcLib.model.attribute_context
578
+ if Lorj.defaults.meta_exist?(attr_name)
579
+ Lorj.debug(2, "%-28s: %s predefined config '%s'.",
580
+ _object_name(PrcLib.model.object_context),
581
+ msg_action, attr_name)
582
+ else
583
+ Lorj.debug(2, "%-28s: %s runtime config '%s'.",
584
+ _object_name(PrcLib.model.object_context),
585
+ msg_action, attr_name)
328
586
  end
329
-
330
- # Define the hdata values to build for the controller automatically
331
- # Input:
332
- # - sParam: Data name to add in hdata controller.
333
- # - hParams: supports following hash values:
334
- # - :mapping (merged in @@meta_obj://<Object>/:params/:keys/<keypath>/:mapping)
335
- #
336
-
337
- def self.set_hdata(sParam, hParams = {})
338
- return nil if not [String, Symbol, Array].include?(sParam.class)
339
-
340
- hParams = {} if not hParams
341
-
342
- aCaller = caller
343
- aCaller.pop
344
-
345
- raise Lorj::PrcError.new(), "%s: No Object defined. Missing define_obj?" % [ self.class], aCaller if @@Context[:oCurrentObj].nil?
346
- sCloudObj = @@Context[:oCurrentObj]
347
-
348
- raise Lorj::PrcError.new(), "%s: '%s' not declared. Missing define_obj(%s)?" % [ self.class, sCloudObj, sCloudObj], aCaller if Lorj::rhExist?(@@meta_obj, sCloudObj) != 1
349
-
350
- oKeyPath = KeyPath.new(sParam)
351
- sKeyAccess = oKeyPath.sFullPath
352
-
353
- # @@meta_obj://<Object>/:params/:keys/<keypath> must exist.
354
- oCloudObjParam = Lorj::rhGet(@@meta_obj, sCloudObj, :params, :keys, sKeyAccess)
355
-
356
- sMapping = sParam
357
- sMapping = hParams[:mapping] unless hParams[:mapping].nil?
358
- oCloudObjParam[:mapping] = sMapping
359
-
360
- sObjectName = "'%s.%s'" % [self.class, sCloudObj]
361
- Lorj.debug(2, "%-28s: hdata set '%s' => '%s'" % [sObjectName, sParam, sMapping])
587
+ # Merge from predefined params, but ensure type is never updated.
588
+ object_attr.merge!(new_params.merge(:type => :data))
589
+ end
590
+
591
+ # Internal function
592
+ def self._obj_needs_object(object_attr, new_params)
593
+ attr_name = PrcLib.model.attribute_context
594
+ unless PrcLib.model.meta_obj.key?(attr_name.key)
595
+ PrcLib.dcl_fail("%s: '%s' not declared. Missing define_obj(%s)?",
596
+ self.class,
597
+ attr_name,
598
+ attr_name)
362
599
  end
600
+ # Merge from predefined params, but ensure type is never updated.
601
+ object_attr.merge!(new_params.merge(:type => :CloudObject))
602
+ end
363
603
 
604
+ # Internal function
605
+ def self._define_obj_initialize(obj_type_name, handlers)
606
+ use_controller = PrcLib.model[:use_controller]
364
607
 
365
- def self.attr_value_mapping(value, map)
366
- return nil if not [String, Symbol].include?(value.class)
367
- return nil if not [NilClass, Symbol, String].include?(map.class)
368
-
369
- aCaller = caller
370
- aCaller.pop
371
-
372
- sCloudObj = @@Context[:oCurrentObj]
373
- raise Lorj::PrcError.new, "attr_value_mapping: mapping '%s' needs object context definition. You need to call define_obj to get the context." % value if sCloudObj.nil?
374
-
375
- oKeypath = @@Context[:oCurrentKey]
376
- raise Lorj::PrcError.new, "attr_value_mapping: mapping '%s' needs object data context definition. You need to call define_obj, then obj_needs to get the context." % value if oKeypath.nil?
377
-
378
- keypath = oKeypath.sFullPath
379
- Lorj.debug(2, "%s-%s: Value mapping definition '%s' => '%s'" % [sCloudObj, oKeypath.to_s, value, map])
380
- Lorj::rhSet(@@meta_obj, map, sCloudObj, :value_mapping, keypath, value)
381
- end
382
-
383
- def self.def_attribute(key, options = nil)
384
- self.get_attr_mapping(key, options)
385
- #~ self.def_query_attribute(key) unless options and options.key?(:not_queriable) and options[:not_queriable]== true
386
- end
387
-
388
- # Function used by the controler to define mapping.
389
- # By default, any attributes are queriable as well. No need to call
390
- # query_mapping
391
- def self.get_attr_mapping(key, map = nil, options = nil)
392
- return nil if not [String, Symbol].include?(key.class)
393
- return nil if not [NilClass, Symbol, String, Array].include?(map.class)
394
-
395
- aCaller = caller
396
- aCaller.pop
397
-
398
- raise Lorj::PrcError.new(), "%s: No Object defined. Missing define_obj?" % [ self.class], aCaller if @@Context[:oCurrentObj].nil?
399
-
400
- sCloudObj = @@Context[:oCurrentObj]
401
- oKeyPath = KeyPath.new(key)
402
-
403
- if map.nil?
404
- oMapPath = oKeyPath
405
- map = oMapPath.sKey
406
- else
407
- oMapPath = KeyPath.new(map)
408
- end
409
-
410
- Lorj::rhSet(@@meta_obj, oMapPath.sFullPath, sCloudObj, :returns, oKeyPath.sFullPath)
411
- @@Context[:oCurrentKey] = oKeyPath
412
- if oMapPath == oKeyPath
413
- Lorj::debug(4, "%s: Defining object attribute '%s'" % [sCloudObj, oKeyPath.sFullPath])
414
- else
415
- Lorj::debug(4, "%s: Defining object attribute mapping '%s' => '%s'" % [sCloudObj, oKeyPath.sFullPath, oMapPath.sFullPath])
416
- end
417
-
418
- self.query_mapping(key, map) unless options and options.key?(:not_queriable) and options[:not_queriable]== true
419
- end
420
-
421
- def self.undefine_attribute(key)
422
- return nil if not [String, Symbol].include?(key.class)
423
-
424
- aCaller = caller
425
- aCaller.pop
426
-
427
- raise Lorj::PrcError.new(), "%s: No Object defined. Missing define_obj?" % [ self.class], aCaller if @@Context[:oCurrentObj].nil?
428
-
429
- sCloudObj = @@Context[:oCurrentObj]
430
- oKeyPath = KeyPath.new(key)
431
-
432
- Lorj::rhSet(@@meta_obj, nil, sCloudObj, :returns, oKeyPath.sFullPath)
433
- @@Context[:oCurrentKey] = oKeyPath
434
- Lorj::debug(4, "%s: Undefining attribute mapping '%s'" % [sCloudObj, oKeyPath.sFullPath])
435
-
436
- self.query_mapping(key, nil)
437
- end
438
-
439
- # Defines/update CloudData parameters
440
- def self.define_data(sData, hMeta)
441
- return nil if not sData or not hMeta
442
- return nil if not [String, Symbol].include?(sData.class)
443
- return nil if hMeta.class != Hash
444
-
445
- aCaller = caller
446
- aCaller.pop
447
-
448
- sData = sData.to_sym if sData.class == String
449
- raise Lorj::PrcError.new(), "%s: Config data '%s' unknown" % [self.class, sData], aCaller if not Lorj::Default.meta_exist?(sData)
450
-
451
- @@Context[:oCurrentData] = sData
452
-
453
- section = Lorj::Default.get_meta_section(sData)
454
- section = :runtime if section.nil?
455
-
456
- if Lorj::rhExist?(@@meta_data, section, sData) == 2
457
- Lorj::rhGet(@@meta_data, section, sData).merge!(hMeta)
458
- else
459
- Lorj::rhSet(@@meta_data, hMeta, section, sData)
460
- end
461
-
462
- end
463
-
464
- def self.data_value_mapping(value, map)
465
- return nil if not [String, Symbol].include?(value.class)
466
- return nil if not [NilClass, Symbol, String].include?(map.class)
467
-
468
- aCaller = caller
469
- aCaller.pop
470
- sData = @@Context[:oCurrentData]
471
- raise Lorj::PrcError.new, "Config data context not set. at least, you need to call define_data before." if sData.nil?
472
-
473
- section = Lorj::Default.get_meta_section(sData)
474
- section = :runtime if section.nil?
475
-
476
- Lorj.debug(2, "%s/%s: Define config data value mapping: '%s' => '%s'" % [section, sData, value, map])
477
- Lorj::rhSet(@@meta_data, map, section, sData, :value_mapping, :controller, value)
478
- Lorj::rhSet(@@meta_data, value, section, sData, :value_mapping, :process, map)
608
+ if use_controller.nil?
609
+ PrcLib.model.options :use_controller => true
610
+ use_controller = true
479
611
  end
480
612
 
481
- def self.provides(aObjType)
482
- @aObjType = aObjType
613
+ # TODO: Cleanup un-used 2 levels :params/:keys by single :params
614
+ object = { :lambdas => { :create_e => nil, :delete_e => nil,
615
+ :update_e => nil, :get_e => nil,
616
+ :query_e => nil, :get_attr_e => nil },
617
+ :params => { :keys => {} },
618
+ :options => { :controller => use_controller },
619
+ :query_mapping => { ':id' => ':id', ':name' => ':name' },
620
+ :returns => { ':id' => ':id', ':name' => ':name' }
621
+ }
622
+
623
+ PrcLib.dcl_fail("A new declared object '%s' requires at "\
624
+ 'least one handler. Ex: define_obj :%s, '\
625
+ 'create_e: myhandler or nohandler: true',
626
+ obj_type_name,
627
+ obj_type_name) if handlers.length == 0
628
+
629
+ if !handlers.rh_get(:nohandler)
630
+ msg = '%-28s object declared.'
631
+ else
632
+ msg = '%-28s meta object declared.'
483
633
  end
484
-
485
- def self.defined?(objType)
486
- @aObjType.include?(objType)
634
+ Lorj.debug(2, msg, _object_name(obj_type_name))
635
+ object
636
+ end
637
+
638
+ # Internal function for define_obj
639
+ # Check handler options.
640
+ # Return the list of handlers set if handler list is ok (exist 1 at least)
641
+ # return false if no handler is set.
642
+ def self._verify_handlers(type_name, object, handlers)
643
+ return false if handlers.rh_get(:nohandler)
644
+
645
+ PrcLib.dcl_fail("A new declared object '%s' requires at "\
646
+ 'least one handler. Ex: define_obj :%s, '\
647
+ 'create_e: myhandler or nohandler: true',
648
+ type_name, type_name) if object.nil? &&
649
+ handlers.length == 0
650
+
651
+ return handlers if object.nil?
652
+
653
+ handlers_list = object[:lambdas].keys.join(', ')
654
+ handlers.each_key do | key |
655
+ next if lorj_object.rh_exist?(:lambdas, key)
656
+
657
+ PrcLib.dcl_fail("'%s' parameter is invalid. Use '%s'",
658
+ key, handlers_list)
487
659
  end
488
-
489
- # Internal BaseDefinition function
490
-
491
- def self.predefine_data_value(data, hOptions)
492
- return nil if self.class != BaseDefinition # Refuse to run if not a BaseDefinition call
493
- return nil if not [String, Symbol].include?(value.class)
494
- return nil if not [NilClass, Symbol, String].include?(map.class)
495
-
496
- aCaller = caller
497
- aCaller.pop
498
-
499
- oKeyPath = @@Context[:oCurrentKey]
500
-
501
- value = {data => {:options => hOptions} }
502
-
503
- Lorj::rhSet(@@predefine_data_value, value, oKeyPath.sFullPath, :values)
504
- end
505
-
506
- # function to interpret a template data, and use ERBConfig as data context.
507
- # ERBConfig contains config object only.
508
- def erb(str)
509
- ERB.new(str).result(@oERBConfig.get_binding())
660
+ handlers
661
+ end
662
+
663
+ # Setting procs
664
+ def self._handler_settings(object, handlers_options)
665
+ handlers_dcl = object[:lambdas]
666
+ process_context = PrcLib.model.process_context
667
+
668
+ handlers_dcl.each_key do |key|
669
+ next unless handlers_options.key?(key)
670
+
671
+ unless process_context.instance_methods.include?(handlers_options[key])
672
+ PrcLib.dcl_fail("'%s' parameter requires a valid instance method"\
673
+ " '%s' in the process '%s'.",
674
+ key, handlers_options[key], process_context)
675
+ end
676
+ if handlers_options[key] == :default
677
+ # By default, we use the event name as default function to call.
678
+ # Those function are predefined in ForjController
679
+ # The Provider needs to derive from ForjController and redefine those
680
+ # functions.
681
+ object[:lambdas][key] = key
682
+ else
683
+ # If needed, ForjProviver redefined can contains some additionnal
684
+ # functions to call.
685
+ object[:lambdas][key] = handlers_options[key]
686
+ end
510
687
  end
511
- end
688
+ end
689
+ end
512
690
  end