lorj 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +39 -579
- data/Rakefile +7 -0
- data/example/students_1/student_v1.md +115 -0
- data/example/students_1/students.rb +8 -2
- data/example/students_2/process/students.rb +5 -5
- data/example/students_2/student_v2.md +90 -0
- data/example/students_2/students.rb +6 -6
- data/example/students_3/controller/yaml_students.rb +23 -73
- data/example/students_3/controller/yaml_students_code.rb +106 -0
- data/example/students_3/controller/yaml_students_def.rb +69 -0
- data/example/students_3/process/students.rb +23 -157
- data/example/students_3/student_v3.md +283 -0
- data/example/students_3/students.rb +26 -83
- data/example/students_4/controller/yaml_students.rb +51 -0
- data/example/students_4/controller/yaml_students_code.rb +109 -0
- data/example/students_4/controller/yaml_students_def.rb +72 -0
- data/example/students_4/process/students/code/students.rb +103 -0
- data/example/students_4/process/students/definition/students.rb +60 -0
- data/example/students_4/process/students.rb +29 -0
- data/example/students_4/student_v4.md +191 -0
- data/example/students_4/students.rb +65 -0
- data/example/students_5/controller/yaml_students.rb +106 -0
- data/example/{students_3 → students_5}/controller/yaml_students_controller.rb +5 -3
- data/example/students_5/process/students.rb +182 -0
- data/example/students_5/student_v5.md +382 -0
- data/example/students_5/students.rb +119 -0
- data/example/yaml_students/students.rb +1 -1
- data/example/yaml_students/yaml_students.rb +102 -23
- data/lib/concept.md +3 -3
- data/lib/core/core.rb +15 -15
- data/lib/core/core_controller.rb +49 -24
- data/lib/core/core_internal.rb +2 -2
- data/lib/core/core_model.rb +13 -7
- data/lib/core/core_object_data.rb +18 -18
- data/lib/core/core_object_params.rb +75 -34
- data/lib/core/core_process.rb +104 -59
- data/lib/core/core_process_setup.rb +11 -11
- data/lib/core/core_setup_ask.rb +24 -14
- data/lib/core/core_setup_encrypt.rb +17 -15
- data/lib/core/core_setup_init.rb +19 -15
- data/lib/core/core_setup_list.rb +12 -12
- data/lib/core/definition.rb +20 -20
- data/lib/core/definition_internal.rb +20 -10
- data/lib/core/lorj_basecontroller.rb +8 -8
- data/lib/core/lorj_basedefinition.rb +47 -126
- data/lib/core/lorj_baseprocess.rb +81 -57
- data/lib/core/lorj_data.rb +28 -27
- data/lib/core/lorj_keypath.rb +1 -1
- data/lib/core_process/cloud/process/flavor.rb +3 -2
- data/lib/core_process/cloud/process/keypairs.rb +5 -4
- data/lib/core_process/cloud/process/network.rb +4 -3
- data/lib/core_process/cloud/process/public_ip.rb +3 -2
- data/lib/core_process/cloud/process/rules.rb +7 -6
- data/lib/core_process/cloud/process/security_groups.rb +1 -1
- data/lib/core_process/cloud/process/server.rb +1 -1
- data/lib/core_process/cloud/process/server_log.rb +1 -1
- data/lib/core_process/cloud/process/subnetwork.rb +4 -1
- data/lib/core_process/cloud_process.rb +1 -1
- data/lib/logging.rb +41 -48
- data/lib/lorj/version.rb +1 -1
- data/lib/lorj.rb +7 -0
- data/lib/lorj_account.rb +3 -3
- data/lib/lorj_config.rb +1 -1
- data/lib/lorj_defaults.rb +222 -26
- data/lib/overview.md +120 -0
- data/lib/prc.rb +97 -24
- data/lib/prc_core_config.rb +134 -52
- data/lib/providers/hpcloud/compute.rb +3 -3
- data/lib/providers/hpcloud/hpcloud.rb +14 -14
- data/lib/providers/hpcloud/network.rb +4 -4
- data/lib/providers/hpcloud/security_groups.rb +1 -1
- data/lib/providers/mock/mock.rb +3 -3
- data/lib/providers/openstack/openstack.rb +12 -12
- data/lib/providers/templates/compute.rb +6 -6
- data/lib/rh.rb +7 -5
- data/spec/04_prc_core_config_spec.rb +52 -0
- data/spec/11_lorj_config_spec.rb +1 -1
- metadata +21 -3
@@ -14,169 +14,35 @@
|
|
14
14
|
# See the License for the specific language governing permissions and
|
15
15
|
# limitations under the License.
|
16
16
|
|
17
|
-
# Students process
|
17
|
+
# Students process
|
18
18
|
class StudentsProcess
|
19
|
-
# create_student process handler to get a student.
|
20
|
-
#
|
21
|
-
# * If not found, create it.
|
22
|
-
# * If multiple found, remove duplicates
|
23
|
-
# * otherwise return the one found.
|
24
|
-
# rubocop:disable Metrics/MethodLength
|
25
19
|
def create_student(sObjectType, hParams)
|
26
20
|
PrcLib.state(format("Running creation process for object '%s' = '%s'",
|
27
21
|
sObjectType, hParams[:student_name]))
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
found_one_student(list[0], hParams[:student_name])
|
35
|
-
else
|
36
|
-
found_multiple_students(list, hParams[:student_name])
|
37
|
-
end
|
38
|
-
end
|
39
|
-
# rubocop:enable Metrics/MethodLength
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
# Create a single new student
|
44
|
-
def create_new_student(student_name)
|
45
|
-
object = controller_create(:student)
|
46
|
-
fail format("Student '%s' not created.", student_name) if object.nil?
|
47
|
-
|
48
|
-
PrcLib.info(format("'student': '%s' created with id %s",
|
49
|
-
student_name,
|
50
|
-
object[:id]))
|
51
|
-
object
|
52
|
-
end
|
53
|
-
|
54
|
-
# Identified 1 student
|
55
|
-
def found_one_student(object, student_name)
|
56
|
-
PrcLib.info(format("'student': '%s' loaded with id %s",
|
57
|
-
student_name, object[:id]))
|
22
|
+
# byebug if ENV['BYEBUG'] # rubocop: disable Debugger
|
23
|
+
object = controller_create(sObjectType)
|
24
|
+
PrcLib.runtime_fail "Student '%s' not created.",
|
25
|
+
hParams[:student_name] if object.nil?
|
26
|
+
PrcLib.info("'%s': '%s' created with id %s",
|
27
|
+
sObjectType, hParams[:student_name], object[:id])
|
58
28
|
object
|
59
29
|
end
|
60
|
-
|
61
|
-
# Identified multiple identical students
|
62
|
-
# It will remove duplicated.
|
63
|
-
def found_multiple_students(list, student_name)
|
64
|
-
object = list[0]
|
65
|
-
PrcLib.warning(format("More than one student named '%s' is found: %s "\
|
66
|
-
'records. Selecting the first one and removing '\
|
67
|
-
'duplicates.',
|
68
|
-
student_name, list.length))
|
69
|
-
remove_multiple_students(list[1..-1], student_name)
|
70
|
-
object
|
71
|
-
end
|
72
|
-
|
73
|
-
# Remove list of identical students
|
74
|
-
def remove_multiple_students(list, student_name)
|
75
|
-
return false unless list.is_a?(Array)
|
76
|
-
return false if list.length == 0
|
77
|
-
|
78
|
-
count = 0
|
79
|
-
list.each { | elem | count += remove_student_object(elem) }
|
80
|
-
PrcLib.info(format("'student': %s duplicated '%s' removed. "\
|
81
|
-
'First loaded with id %s',
|
82
|
-
count, student_name, object[:id]))
|
83
|
-
end
|
84
|
-
|
85
|
-
def remove_student_object(object)
|
86
|
-
register(object)
|
87
|
-
controller_delete(:student)
|
88
|
-
end
|
89
|
-
|
90
|
-
# The following handler is inactive.
|
91
|
-
# It provides a simple print-out code.
|
92
|
-
# If you want to activate it:
|
93
|
-
# * uncomment query_student function
|
94
|
-
# * update the :student data model
|
95
|
-
# on query_e, replace controller_query by query_student
|
96
|
-
|
97
|
-
# def query_student(sObjectType, sQuery, hParams)
|
98
|
-
# PrcLib::state (format("Running query process for object '%s' "\
|
99
|
-
# "with query '%s'",
|
100
|
-
# sObjectType,
|
101
|
-
# sQuery))
|
102
|
-
#
|
103
|
-
# objects = controller_query(sObjectType, sQuery)
|
104
|
-
# raise "Query error." if objects.nil?
|
105
|
-
#
|
106
|
-
# PrcLib::info (format("'%s': Queried. %s records found.",
|
107
|
-
# sObjectType,
|
108
|
-
# objects.length))
|
109
|
-
# objects
|
110
|
-
# end
|
111
|
-
|
112
|
-
# This handler is inactive.
|
113
|
-
# It provides a simple print-out code.
|
114
|
-
# If you want to activate it:
|
115
|
-
# * uncomment get_student function
|
116
|
-
# * update the :student data model
|
117
|
-
# on get_e, replace controller_get by get_student
|
118
|
-
|
119
|
-
# def delete_student(sObjectType, hParams)
|
120
|
-
# controller_delete(:student)
|
121
|
-
# PrcLib::info (format("'%s:%s' student removed",
|
122
|
-
# hParams[:student, :id],
|
123
|
-
# hParams[:student, :name]))
|
124
|
-
# end
|
125
30
|
end
|
126
31
|
|
127
|
-
|
128
|
-
|
129
|
-
#
|
130
|
-
#
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
# handlers.
|
144
|
-
|
145
|
-
define_obj(:student,
|
146
|
-
# The function to call in the class Students
|
147
|
-
:create_e => :create_student,
|
148
|
-
# We use predefined call to the controller query
|
149
|
-
:query_e => :controller_query,
|
150
|
-
# We use predefined call to the controller delete
|
151
|
-
:delete_e => :controller_delete
|
152
|
-
)
|
153
|
-
|
154
|
-
# obj_needs is used to declare parameters to pass to handlers.
|
155
|
-
# :for indicates those parameters to be passed to create_e handler only.
|
156
|
-
# Those data (or objects) will be collected and passed to the process
|
157
|
-
# handler as hParams.
|
158
|
-
|
159
|
-
obj_needs :data, :student_name, :for => [:create_e]
|
160
|
-
|
161
|
-
# By default, all data are required.
|
162
|
-
# You can set it as optional. Your process will need to deal with this
|
163
|
-
# optional data.
|
164
|
-
obj_needs_optional
|
165
|
-
obj_needs :data, :first_name, :for => [:create_e]
|
166
|
-
obj_needs :data, :last_name, :for => [:create_e]
|
167
|
-
obj_needs :data, :course
|
168
|
-
# Note that in this model, the training is renamed as course.
|
169
|
-
|
170
|
-
# the controller will need to map it to 'training'.
|
171
|
-
|
172
|
-
# def_attribute defines the data model.
|
173
|
-
# The process will be able to access those data
|
174
|
-
def_attribute :course
|
175
|
-
def_attribute :student_name
|
176
|
-
def_attribute :first_name
|
177
|
-
def_attribute :last_name
|
178
|
-
def_attribute :status
|
179
|
-
|
180
|
-
undefine_attribute :name
|
181
|
-
end
|
32
|
+
# Declaring your data model and handlers.
|
33
|
+
class Lorj::BaseDefinition # rubocop: disable Style/ClassAndModuleChildren
|
34
|
+
# We need to define the student object and the handler to use while we need to
|
35
|
+
# create it.
|
36
|
+
define_obj(:student,
|
37
|
+
# The function to call in the class Students
|
38
|
+
:create_e => :create_student,
|
39
|
+
# We use predefined call to the controller query
|
40
|
+
:query_e => :controller_query,
|
41
|
+
# We use predefined call to the controller get
|
42
|
+
:get_e => :controller_get,
|
43
|
+
# We use predefined call to the controller delete
|
44
|
+
:delete_e => :controller_delete
|
45
|
+
)
|
46
|
+
|
47
|
+
obj_needs :data, :student_name, :for => [:create_e], :mapping => :name
|
182
48
|
end
|
@@ -0,0 +1,283 @@
|
|
1
|
+
# Writing student version 3
|
2
|
+
|
3
|
+
The power of lorj is to keep the GENERIC process as is, and be able to create a
|
4
|
+
controller replacing simply mock by another one.
|
5
|
+
In this example, we will use a yaml_students API in the new controller.
|
6
|
+
|
7
|
+
This API is written in `examples/yaml_students/yaml_students.rb`.
|
8
|
+
|
9
|
+
A controller is like a wrapper to an API which offer what we would like to use
|
10
|
+
to save our yaml file. And that's globally, what we are going to do.
|
11
|
+
|
12
|
+
First of all, we would like to inform lorj that we are going to use a different
|
13
|
+
controller, to be written in controller/yaml_students.rb
|
14
|
+
|
15
|
+
Let's change the student_core object creation.
|
16
|
+
|
17
|
+
## File `students.rb`
|
18
|
+
``` ruby
|
19
|
+
processes = [File.join(app_path, 'process', 'students.rb')]
|
20
|
+
# now we changed from mock to our own controller, located in
|
21
|
+
controller = File.join(app_path, 'controller', 'yaml_students.rb')
|
22
|
+
|
23
|
+
# controller/yaml_students.rb
|
24
|
+
# student_core = Lorj::Core.new(nil, processes, :mock)
|
25
|
+
student_core = Lorj::Core.new(nil, processes, controller)
|
26
|
+
```
|
27
|
+
|
28
|
+
Now, let's write the controller file.
|
29
|
+
|
30
|
+
The controller contains 2 elements:
|
31
|
+
* controller definition
|
32
|
+
* controller code
|
33
|
+
|
34
|
+
lorj will load automatically 'controller/yaml_students.rb' and expect to find
|
35
|
+
the controller definition and code.
|
36
|
+
|
37
|
+
* The Controller definition have to be written in a class named: YamlStudents
|
38
|
+
lorj automatically creates a class derived from Lorj::BaseDefinition, and
|
39
|
+
expect the file to define this class.
|
40
|
+
|
41
|
+
So, you will need to write something like:
|
42
|
+
|
43
|
+
class YamlStudents
|
44
|
+
...
|
45
|
+
end
|
46
|
+
|
47
|
+
Parent class (< BaseDefinition) is not required, as made by lorj itself.
|
48
|
+
|
49
|
+
All declaration function we can use are defined in BaseDefinition.
|
50
|
+
|
51
|
+
We will use the following:
|
52
|
+
* define_obj : Declare a meta object, and attach handlers
|
53
|
+
* obj_needs : Assign some data as parameters to handlers.
|
54
|
+
* def_attr_mapping : Define meta object attributes mapping.
|
55
|
+
* undefine_attribute : Remove some predefined meta object attribute
|
56
|
+
|
57
|
+
We use define_obj to define another meta object, to create the connection
|
58
|
+
And we need to update the existing meta object 'student', to say that in the
|
59
|
+
context of this controller we need a connection.
|
60
|
+
This controller definition is executed on top of the process data model.
|
61
|
+
|
62
|
+
Then, we need to map meta object attributes with the controller data attributes
|
63
|
+
|
64
|
+
When the controller object has been created, lorj will need to get back data
|
65
|
+
that was defined by the model. The Process uses this data to complete his task.
|
66
|
+
So, the controller will need to help lorj to extract data from the controller
|
67
|
+
object.
|
68
|
+
|
69
|
+
This the role of a get_attr function.
|
70
|
+
|
71
|
+
The usage of hdata (def_hdata) is a framework facility to build a Hash with
|
72
|
+
all data attributes and values required by the controller object to work.
|
73
|
+
|
74
|
+
Ok, let's write this controller, now:
|
75
|
+
|
76
|
+
To simplify, we split this controller in 3 files.
|
77
|
+
controller/yaml_students.rb declare, files and API to use
|
78
|
+
controller/yaml_students_def.rb contains the controller definition
|
79
|
+
controller/yaml_students_code.rb contains the controller code.
|
80
|
+
|
81
|
+
## controller/yaml_students.rb
|
82
|
+
``` ruby
|
83
|
+
# when you declare this controller with a file name, lorj expect this file
|
84
|
+
# to contains the controller code and the controller definition.
|
85
|
+
# The code have to be declared in a class which is named as follow from the file
|
86
|
+
# name:
|
87
|
+
# 1st letter is capitalized
|
88
|
+
# A '_' followed by a letterif replaced by the capicatl letter.
|
89
|
+
# Ex: my_code.rb => assume to declare MyCode class
|
90
|
+
#
|
91
|
+
# The controller code class is built from the source file name as explained
|
92
|
+
# below + 'Controller'
|
93
|
+
# Ex: my_code.rb => assume to use MyCodeController for controller handlers and
|
94
|
+
# functions.
|
95
|
+
#
|
96
|
+
# The controller definition class is build from the file name only.
|
97
|
+
# Ex: my_code.rb => assume to use MyCode for controller definition.
|
98
|
+
|
99
|
+
|
100
|
+
# This class describes how to process some actions, and will do everything prior
|
101
|
+
# this task to make it to work.
|
102
|
+
|
103
|
+
# declare yaml student API to the controller
|
104
|
+
cur_path = File.expand_path(File.dirname(__FILE__))
|
105
|
+
api_file = File.join(cur_path, '..', '..', 'yaml_students', 'yaml_students.rb'))
|
106
|
+
|
107
|
+
require api_file
|
108
|
+
|
109
|
+
# The controller is a combination of 2 elements:
|
110
|
+
# - Controller class
|
111
|
+
# Code which will interfere with the external API.
|
112
|
+
#
|
113
|
+
controller_file = File.join(cur_path, 'yaml_students_code.rb')
|
114
|
+
require controller_file # Load controller mapping
|
115
|
+
|
116
|
+
# - Definition class
|
117
|
+
# This class declare any kind of mapping or additional attributes to consider.
|
118
|
+
require File.join(cur_path, 'yaml_students_def.rb')
|
119
|
+
```
|
120
|
+
|
121
|
+
## controller/yaml_students_def.rb
|
122
|
+
``` ruby
|
123
|
+
class YamlStudents
|
124
|
+
# This is a new object which is known by the controller only.
|
125
|
+
# Used to open the yaml file. Generically, I named it :connection.
|
126
|
+
# But this can be any name you want. Only the controller will deal with it.
|
127
|
+
define_obj(:connection,
|
128
|
+
# Nothing complex to do. So, simply call the controller create.
|
129
|
+
:create_e => :controller_create
|
130
|
+
)
|
131
|
+
|
132
|
+
obj_needs :data, :connection_string, :mapping => :file_name
|
133
|
+
undefine_attribute :id # Do not return any predefined ID
|
134
|
+
undefine_attribute :name # Do not return any predefined NAME
|
135
|
+
|
136
|
+
# The student meta object have to be expanded.
|
137
|
+
define_obj(:student)
|
138
|
+
# It requires to create a connection to the data, ie opening the yaml file.
|
139
|
+
# So, before working with the :student object, the controller requires a
|
140
|
+
# connection
|
141
|
+
# This connection will be loaded in the memory and provided to the controller
|
142
|
+
# when needed.
|
143
|
+
# obj_needs :CloudObject update the :student object to requires a connection
|
144
|
+
# before.
|
145
|
+
obj_needs :CloudObject, :connection
|
146
|
+
|
147
|
+
# instead of 'student_name', the yaml API uses 'name' as key
|
148
|
+
def_attr_mapping :student_name, :name
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
##### controller/yaml_students_code.rb
|
153
|
+
This file is code of the controller which will deal with the API to wrap and
|
154
|
+
adapt the Generic process object data model to become compatible with
|
155
|
+
the controller data model.
|
156
|
+
|
157
|
+
For example:
|
158
|
+
YamlStudent requires a first and last name at student creation time.
|
159
|
+
As the GENERIC process only know the student name, the controller will
|
160
|
+
need to determine how to get the first & last name from the student name.
|
161
|
+
|
162
|
+
The generic Get a student from a unique ID doesn't exist. So, we need to adapt this
|
163
|
+
to use a query :id => id instead.
|
164
|
+
|
165
|
+
Of course, simple case, like when lorj ask directly the controller to query, it will
|
166
|
+
simply call the query API function.
|
167
|
+
|
168
|
+
Ok, let's see the code, now:
|
169
|
+
|
170
|
+
``` ruby
|
171
|
+
# This file describe the controller code.
|
172
|
+
# The class name is determined by lorj.
|
173
|
+
# See controller/yaml_students.rb for details
|
174
|
+
class YamlStudentsController
|
175
|
+
# controller wrapper
|
176
|
+
def create(sObjectType, hParams)
|
177
|
+
case sObjectType
|
178
|
+
when :connection
|
179
|
+
required?(hParams, :hdata, :file_name)
|
180
|
+
YamlSchool.new(hParams[:hdata, :file_name])
|
181
|
+
when :student
|
182
|
+
required?(hParams, :connection)
|
183
|
+
required?(hParams, :student_name)
|
184
|
+
|
185
|
+
# Here, we adapt the lorj student data model with the YamlStudent data model
|
186
|
+
fields = hParams[:student_name].split(' ')
|
187
|
+
fields.insert(0, 'first_name unknown') if fields.length == 1
|
188
|
+
|
189
|
+
options = {:first_name => fields[0..-2].join(' '),
|
190
|
+
:last_name => fields[-1]}
|
191
|
+
hParams[:connection].create_student(hParams[:student_name], options)
|
192
|
+
else
|
193
|
+
controller_error "'%s' is not a valid object for 'create'", sObjectType
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
# This function return one element identified by an ID.
|
198
|
+
# But get is not a YamlStudent API functions. But query help to use :id
|
199
|
+
#
|
200
|
+
# so we will do a query
|
201
|
+
def get(sObjectType, id, hParams)
|
202
|
+
case sObjectType
|
203
|
+
when :student
|
204
|
+
result = query(sObjectType, {:id => id}, hParams)
|
205
|
+
return nil if result.length == 0
|
206
|
+
result[0]
|
207
|
+
else
|
208
|
+
controller_error "'%s' is not a valid object for 'create'", sObjectType
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
# This function return a collection which have to provide:
|
213
|
+
# functions: [], length, each
|
214
|
+
def query(sObjectType, sQuery, hParams)
|
215
|
+
case sObjectType
|
216
|
+
when :student
|
217
|
+
required?(hParams, :connection)
|
218
|
+
|
219
|
+
hParams[:connection].query_student(sQuery)
|
220
|
+
else
|
221
|
+
controller_error "'%s' is not a valid object for 'create'", sObjectType
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
# This controller function read the data and
|
226
|
+
# extract the information requested by the framework.
|
227
|
+
# Those data will be mapped to the process data model.
|
228
|
+
# The key is an array, to get data from a level tree.
|
229
|
+
# [data_l1, data_l2, data_l3] => should retrieve data from structure like
|
230
|
+
# data[ data_l2[ data_l3 ] ]
|
231
|
+
def get_attr(oControlerObject, key)
|
232
|
+
attributes = oControlerObject
|
233
|
+
|
234
|
+
controller_error("get_attr: attribute '%s' is unknown in '%s'. "\
|
235
|
+
"Valid one are : '%s'",
|
236
|
+
key[0], oControlerObject.class,
|
237
|
+
valid_attributes) unless valid_attributes.include?(key[0])
|
238
|
+
attributes.rh_get(key)
|
239
|
+
rescue => e
|
240
|
+
controller_error "get_attr: Unable to map '%s'. %s\n See %s",
|
241
|
+
key, e.message, e.backtrace[0]
|
242
|
+
end
|
243
|
+
|
244
|
+
private
|
245
|
+
|
246
|
+
# These are the valid controller fields.
|
247
|
+
def valid_attributes
|
248
|
+
[:name, :first_name, :last_name, :id, :status, :training]
|
249
|
+
end
|
250
|
+
end
|
251
|
+
```
|
252
|
+
|
253
|
+
If you execute the code, right now, runtime/connection_string will be missing...
|
254
|
+
One of possible way should be use save this an Account file, driven by the Account
|
255
|
+
Object.
|
256
|
+
|
257
|
+
In this example, we will simply set this data manually from the main.
|
258
|
+
|
259
|
+
## students.rb
|
260
|
+
We simply add a setting
|
261
|
+
``` ruby
|
262
|
+
[...]
|
263
|
+
# now we changed from mock to our own controller, located in
|
264
|
+
# controller/yaml_students.rb
|
265
|
+
# student_core = Lorj::Core.new(nil, processes, :mock)
|
266
|
+
config = Lorj::Config.new
|
267
|
+
config[:connection_string] = '/tmp/students.yaml'
|
268
|
+
student_core = Lorj::Core.new(config, processes, controller)
|
269
|
+
[...]
|
270
|
+
```
|
271
|
+
|
272
|
+
# Playing around
|
273
|
+
|
274
|
+
If you want to move back to the mock controller, do it! You will still see that its keeps working.
|
275
|
+
|
276
|
+
You can enhance your main by selecting between mock or yaml_students
|
277
|
+
|
278
|
+
# next?
|
279
|
+
|
280
|
+
In next version, we will enhance the process to remove duplicates, and ensure
|
281
|
+
a student is created if it does not exist.
|
282
|
+
|
283
|
+
[Details is explained here](example/students_4/student_v4_md.html)
|
@@ -17,106 +17,49 @@
|
|
17
17
|
|
18
18
|
app_path = File.dirname(__FILE__)
|
19
19
|
|
20
|
-
if ENV['
|
20
|
+
if ENV['BYEBUG']
|
21
21
|
require 'byebug'
|
22
22
|
lib_path = File.expand_path(File.join(app_path, '..', '..', 'lib'))
|
23
23
|
$LOAD_PATH << lib_path
|
24
24
|
end
|
25
|
-
require 'lorj'
|
26
|
-
require 'ansi'
|
27
|
-
|
28
|
-
# Load global Config
|
29
|
-
|
30
|
-
# This object is used to provide configuration data to lorj
|
31
|
-
|
32
|
-
# The config search is:
|
33
|
-
# 1- Application defaults (defaults.yaml) - Not defined by default. update the
|
34
|
-
# following line and create defaults.yaml
|
35
|
-
# PrcLib.app_defaults = $APP_PATH
|
36
|
-
# 2- Local defaults (~/.<Application Name>/config.yaml) - <Application Name> is
|
37
|
-
# 'Lorj' by default. Can be updated with following line.
|
38
|
-
# PrcLib.app_name = 'myapp'
|
39
|
-
# 3 - runtime. Those variables are set, with config[key] = value
|
40
|
-
|
41
|
-
config = Lorj::Config.new # Use Simple Config Object
|
42
25
|
|
43
|
-
|
44
|
-
# between runtime and config.yaml/app default
|
45
|
-
# config = Lorj::Account.new('MyAccount') #
|
26
|
+
require 'lorj'
|
46
27
|
|
47
|
-
# If you want to see what is happening in the framework, uncomment
|
48
|
-
#
|
28
|
+
# If you want to see what is happening in the framework, uncomment debug
|
29
|
+
# settings.
|
49
30
|
PrcLib.level = Logger::DEBUG # Printed out to your console.
|
50
|
-
PrcLib.core_level =
|
31
|
+
PrcLib.core_level = 3 # framework debug levels.
|
51
32
|
|
52
33
|
# Initialize the framework
|
53
34
|
processes = [File.join(app_path, 'process', 'students.rb')]
|
35
|
+
controller = File.join(app_path, 'controller', 'yaml_students.rb')
|
54
36
|
|
55
|
-
#
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
student_core.create(:connection, :connection_string => '/tmp/students.yaml')
|
63
|
-
|
64
|
-
puts ANSI.bold('Create 1st student:')
|
65
|
-
|
66
|
-
# Set the student name to use
|
67
|
-
# There is different way to set them...
|
68
|
-
# Those lines do the same using config object. Choose what you want.
|
69
|
-
config.set(:first_name, 'Robert')
|
70
|
-
config[:last_name] = 'Redford'
|
71
|
-
config[:student_name] = 'Robert Redford'
|
72
|
-
config[:course] = 'Art Comedy'
|
37
|
+
# now we changed from mock to our own controller, located in
|
38
|
+
# controller/yaml_students.rb
|
39
|
+
# student_core = Lorj::Core.new(nil, processes, :mock)
|
40
|
+
config = Lorj::Config.new
|
41
|
+
config[:connection_string] = '/tmp/students.yaml'
|
42
|
+
student_core = Lorj::Core.new(config, processes, controller)
|
73
43
|
|
74
44
|
# Ask the framework to create the object student 'Robert Redford'
|
75
|
-
student_core.create(:student)
|
45
|
+
student_core.create(:student, :student_name => 'Robert Redford')
|
76
46
|
|
77
|
-
|
78
|
-
|
79
|
-
# The
|
80
|
-
student_core.create(:student,
|
81
|
-
:student_name => 'Anthony Hopkins',
|
82
|
-
:first_name => 'Anthony',
|
83
|
-
:last_name => 'Hopkins',
|
84
|
-
:course => 'Art Drama'
|
85
|
-
)
|
86
|
-
# config[:student_name] = "Anthony Hopkins"
|
87
|
-
# config[:course] = "Art Drama"
|
88
|
-
# student_core.Create(:student)
|
47
|
+
# Want to create a duplicated student 'Robert Redford'?
|
48
|
+
student_core.create(:student, :student_name => 'Robert Redford')
|
49
|
+
# no problem. The key is the key in the Mock controller array.
|
89
50
|
|
90
|
-
|
91
|
-
student_core.create(:student,
|
92
|
-
:student_name => 'Marilyn Monroe',
|
93
|
-
:first_name => 'Marilyn',
|
94
|
-
:last_name => 'Monroe',
|
95
|
-
:course => 'Art Drama'
|
96
|
-
)
|
97
|
-
# replaced the following :
|
98
|
-
# config[:student_name] = "Anthony Hopkins"
|
99
|
-
# student_core.Create(:student)
|
51
|
+
student_core.create(:student, :student_name => 'Anthony Hopkins')
|
100
52
|
|
101
|
-
|
102
|
-
|
103
|
-
:student_name => 'Anthony Mistake',
|
104
|
-
:first_name => 'Anthony',
|
105
|
-
:last_name => 'Mistake',
|
106
|
-
:course => 'what ever you want!!!'
|
107
|
-
)
|
53
|
+
# Let's create a third different student.
|
54
|
+
students = student_core.query(:student, :name => 'Robert Redford')
|
108
55
|
|
109
|
-
puts format(
|
56
|
+
puts format('%s students found', students.length)
|
110
57
|
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
student_core.delete(:student)
|
115
|
-
puts format('Wrong student to remove: %s = %s',
|
116
|
-
student[:id], student[:student_name])
|
58
|
+
students.each do |a_student|
|
59
|
+
puts format('%s: %s', a_student[:id], a_student[:name])
|
60
|
+
end
|
117
61
|
|
118
|
-
|
119
|
-
|
62
|
+
# let's check the get function, who is the ID 2?
|
63
|
+
student = student_core.get(:student, 2)
|
120
64
|
|
121
|
-
puts
|
122
|
-
puts student_core.query(:student, :status => :removed).to_a
|
65
|
+
puts format('The student ID 2 is %s', student[:name])
|
@@ -0,0 +1,51 @@
|
|
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
|
+
# when you declare this controller with a file name, lorj expect this file
|
18
|
+
# to contains the controller code and the controller definition.
|
19
|
+
# The code have to be declared in a class which is named as follow from the file
|
20
|
+
# name:
|
21
|
+
# 1st letter is capitalized
|
22
|
+
# A '_' followed by a letterif replaced by the capicatl letter.
|
23
|
+
# Ex: my_code.rb => assume to declare MyCode class
|
24
|
+
#
|
25
|
+
# The controller code class is built from the source file name as explained
|
26
|
+
# below + 'Controller'
|
27
|
+
# Ex: my_code.rb => assume to use MyCodeController for controller handlers and
|
28
|
+
# functions.
|
29
|
+
#
|
30
|
+
# The controller definition class is build from the file name only.
|
31
|
+
# Ex: my_code.rb => assume to use MyCode for controller definition.
|
32
|
+
|
33
|
+
# This class describes how to process some actions, and will do everything prior
|
34
|
+
# this task to make it to work.
|
35
|
+
|
36
|
+
# declare yaml student API to the controller
|
37
|
+
cur_path = File.expand_path(File.dirname(__FILE__))
|
38
|
+
api_file = File.join(cur_path, '..', '..', 'yaml_students', 'yaml_students.rb')
|
39
|
+
|
40
|
+
require api_file
|
41
|
+
|
42
|
+
# The controller is a combination of 2 elements:
|
43
|
+
# - Controller class
|
44
|
+
# Code which will interfere with the external API.
|
45
|
+
#
|
46
|
+
controller_file = File.join(cur_path, 'yaml_students_code.rb')
|
47
|
+
require controller_file # Load controller mapping
|
48
|
+
|
49
|
+
# - Definition class
|
50
|
+
# This class declare any kind of mapping or additional attributes to consider.
|
51
|
+
require File.join(cur_path, 'yaml_students_def.rb')
|