lorj 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.gitreview +4 -0
  4. data/Gemfile +25 -0
  5. data/Gemfile.lock +34 -0
  6. data/LICENSE.txt +14 -0
  7. data/README.md +652 -0
  8. data/Rakefile +24 -0
  9. data/bin/cloud_test.rb +81 -0
  10. data/example/students_1/process/Students.rb +20 -0
  11. data/example/students_1/students.rb +16 -0
  12. data/example/students_2/process/Students.rb +27 -0
  13. data/example/students_2/students.rb +36 -0
  14. data/example/students_3/controller/yaml_students.rb +94 -0
  15. data/example/students_3/controller/yaml_students_controller.rb +123 -0
  16. data/example/students_3/process/students.rb +118 -0
  17. data/example/students_3/students.rb +93 -0
  18. data/example/students_4/controller/yaml_students.rb +82 -0
  19. data/example/students_4/controller/yaml_students_controller.rb +141 -0
  20. data/example/students_4/process/students.rb +112 -0
  21. data/example/students_4/students.rb +103 -0
  22. data/example/yaml_students/students.rb +78 -0
  23. data/example/yaml_students/yaml_students.rb +115 -0
  24. data/lib/concept.md +111 -0
  25. data/lib/core/core.rb +723 -0
  26. data/lib/core/definition.rb +505 -0
  27. data/lib/core/definition_internal.rb +338 -0
  28. data/lib/core/lorj-basecontroller.rb +90 -0
  29. data/lib/core/lorj-basedefinition.rb +1079 -0
  30. data/lib/core/lorj-baseprocess.rb +231 -0
  31. data/lib/core/lorj-data.rb +567 -0
  32. data/lib/core/lorj-keypath.rb +115 -0
  33. data/lib/core_process/CloudProcess.rb +334 -0
  34. data/lib/core_process/global_process.rb +406 -0
  35. data/lib/core_process/network_process.rb +603 -0
  36. data/lib/img/.directory +4 -0
  37. data/lib/img/account_data_access.png +0 -0
  38. data/lib/img/config_data_access.png +0 -0
  39. data/lib/img/forj-lib-concept.png +0 -0
  40. data/lib/lorj/version.rb +3 -0
  41. data/lib/lorj.rb +51 -0
  42. data/lib/prc-account.rb +339 -0
  43. data/lib/prc-config.rb +1023 -0
  44. data/lib/prc-logging.rb +183 -0
  45. data/lib/prc.rb +108 -0
  46. data/lib/providers/hpcloud/Hpcloud.rb +419 -0
  47. data/lib/providers/hpcloud/compute.rb +108 -0
  48. data/lib/providers/hpcloud/network.rb +117 -0
  49. data/lib/providers/hpcloud/security_groups.rb +67 -0
  50. data/lib/providers/mock/Mock.rb +141 -0
  51. data/lib/providers/openstack/Openstack.rb +47 -0
  52. data/lib/providers/templates/compute.rb +42 -0
  53. data/lib/providers/templates/core.rb +61 -0
  54. data/lib/providers/templates/network.rb +33 -0
  55. data/lorj-spec/defaults.yaml +26 -0
  56. data/lorj.gemspec +39 -0
  57. data/spec/forj-account_spec.rb +75 -0
  58. data/spec/forj-config_spec.rb +196 -0
  59. metadata +164 -0
data/bin/cloud_test.rb ADDED
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #require 'byebug'
4
+
5
+ $APP_PATH = File.dirname(__FILE__)
6
+ $LIB_PATH = File.expand_path(File.join(File.dirname($APP_PATH),'lib'))
7
+
8
+ $LOAD_PATH << $LIB_PATH
9
+
10
+ $LOAD_PATH << File.join($LIB_PATH, 'lib-forj', 'lib')
11
+
12
+ require 'appinit.rb'
13
+
14
+ # Initialize forj paths
15
+ AppInit::forj_initialize()
16
+
17
+ # Initialize global Log object
18
+ $FORJ_LOGGER=LorjLog.new()
19
+
20
+ require 'lib-forj.rb'
21
+
22
+ Logging.set_level(Logger::DEBUG)
23
+
24
+ # Load global Config
25
+ oConfig = ForjConfig.new()
26
+
27
+ aProcesses = []
28
+
29
+ # Defines how to manage Maestro and forges
30
+ # create a maestro box. Identify a forge instance, delete it,...
31
+ aProcesses << File.join($LIB_PATH, 'forj', 'ForjCore.rb')
32
+
33
+ # Defines how cli will control FORJ features
34
+ # boot/down/ssh/...
35
+ aProcesses << File.join($LIB_PATH, 'forj', 'ForjCli.rb')
36
+
37
+ $LIB_FORJ_DEBUG = 3 # verbose
38
+
39
+ infra_dir = File.expand_path(oConfig.get(:infra_repo))
40
+
41
+ # Ask information if needed.
42
+ if not Dir.exist?(File.expand_path(infra_dir))
43
+ Logging.warning(<<-END
44
+ Your infra workspace directory is missing.
45
+
46
+ Forj uses an infra workspace directory to store any kind of data that are private to you.
47
+ We provides ways to send those data securily to your new Forge instance, as metadata.
48
+ In production case, we suggest you to keep it safe in your SCM preferred database.
49
+
50
+ If you already have an existing infra workspace, use 'forj set infra_repo=<PathToYourRepo>' to set it and restart.
51
+
52
+ Otherwise, we will build a new one with some predefined data, you can review and update later.
53
+ END
54
+ )
55
+ sAsk = "Do you want to create a new one from Maestro (yes/no)?" % [infra_dir]
56
+ bBuildInfra=agree(sAsk)
57
+ if not bBuildInfra
58
+ puts 'Process aborted on your demand.'
59
+ exit 0
60
+ end
61
+ end
62
+
63
+ oCloud = ForjCloud.new(oConfig, 'hpcloud', aProcesses)
64
+
65
+ #oConfig.set(:instance_name, "test")
66
+ #oCloud.Create(:metadata)
67
+ #oCloud.Create(:infra_repository)
68
+ #oCloud.Create(:userdata)
69
+
70
+
71
+ #oCloud.Setup(:server, 'hpcloud')
72
+ #oCloud.Setup(:forge, 'hpcloud')
73
+
74
+ #oCloud.Create(:forge)
75
+
76
+ oConfig.set(:box_name, '183')
77
+ oConfig.set(:box, 'maestro')
78
+ #oConfig.Create(:server)
79
+ oCloud.Create(:ssh)
80
+
81
+ #oCloud.Query(:server, 'maestro')
@@ -0,0 +1,20 @@
1
+ # Students process
2
+ class StudentsProcess
3
+ def create_student(sObjectType, hParams)
4
+ puts "Running creation process for object '%s' = '%s'" % [sObjectType, hParams[:student_name] ]
5
+ # If you prefer to print out to the log system instead:
6
+ # PrcLib::debug("Running creation process for object '%s' = '%s'" % [sObjectType, hParams[:student_name] ])
7
+ end
8
+ end
9
+
10
+ # Declaring your data model and handlers.
11
+ class Lorj::BaseDefinition
12
+
13
+ # We need to define the student object and the handler to use while we need to create it.
14
+ define_obj(:student,
15
+ {
16
+ :create_e => :create_student # The function to call in the class Students
17
+ })
18
+
19
+ obj_needs :data, :student_name, { :for => [:create_e] }
20
+ end
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $APP_PATH = File.dirname(__FILE__)
4
+ require 'lorj'
5
+
6
+ # If you want to see what is happening in the framework, uncomment debug settings.
7
+ # PrcLib.level = Logger::DEBUG # Printed out to your console.
8
+ # PrcLib.core_level = 3 # framework debug levels.
9
+
10
+ # Initialize the framework
11
+ hProcesses = [ File.join($APP_PATH, 'process', 'Students.rb')]
12
+
13
+ oStudentCore = Lorj::Core.new( nil, hProcesses)
14
+
15
+ # Ask the framework to create the object student 'Robert Redford'
16
+ oStudentCore.Create(:student, :student_name => "Robert Redford")
@@ -0,0 +1,27 @@
1
+ # Students process
2
+ class StudentsProcess
3
+ def create_student(sObjectType, hParams)
4
+ PrcLib::state ("Running creation process for object '%s' = '%s'" % [sObjectType, hParams[:student_name] ])
5
+
6
+ oObject = controller_create(sObjectType)
7
+ raise "Student '%s' not created." % hParams[:student_name] if oObject.nil?
8
+ PrcLib::info ("'%s': '%s' created with id %s" % [sObjectType, hParams[:student_name], oObject[:id]])
9
+ oObject
10
+ end
11
+
12
+ end
13
+
14
+ # Declaring your data model and handlers.
15
+ class Lorj::BaseDefinition
16
+
17
+ # We need to define the student object and the handler to use while we need to create it.
18
+ define_obj(:student,
19
+ {
20
+ :create_e => :create_student, # The function to call in the class Students
21
+ :query_e => :controller_query, # We use predefined call to the controller query
22
+ :get_e => :controller_get, # We use predefined call to the controller get
23
+ :delete_e => :controller_delete # We use predefined call to the controller delete
24
+ })
25
+
26
+ obj_needs :data, :student_name, { :for => [:create_e], :mapping => :name }
27
+ end
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $APP_PATH = File.dirname(__FILE__)
4
+ require 'lorj'
5
+
6
+ # If you want to see what is happening in the framework, uncomment debug settings.
7
+ # PrcLib.level = Logger::DEBUG # Printed out to your console.
8
+ # PrcLib.core_level = 3 # framework debug levels.
9
+
10
+ # Initialize the framework
11
+ hProcesses = [ File.join($APP_PATH, 'process', 'Students.rb')]
12
+
13
+ oStudentCore = Lorj::Core.new( nil, hProcesses, :mock)
14
+
15
+ # Ask the framework to create the object student 'Robert Redford'
16
+ oStudentCore.Create(:student, :student_name => "Robert Redford")
17
+
18
+ # Want to create a duplicated student 'Robert Redford'?
19
+ oStudentCore.Create(:student)
20
+ # no problem. The key is the key in the Mock controller array.
21
+
22
+ oStudentCore.Create(:student, :student_name => "Anthony Hopkins")
23
+
24
+ # Let's create a third different student.
25
+ oStudents = oStudentCore.Query(:student, { :name => "Robert Redford" } )
26
+
27
+ puts "%s students found" % oStudents.length
28
+
29
+ oStudents.each { | oStudent |
30
+ puts "%s: %s" % [oStudent[:id], oStudent[:name]]
31
+ }
32
+
33
+ # let's check the get function, who is the ID 2?
34
+ oStudent = oStudentCore.Get(:student, 2)
35
+
36
+ puts "The student ID 2 is %s" % oStudent[:name]
@@ -0,0 +1,94 @@
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
+
18
+ # This class describes how to process some actions, and will do everything prior
19
+ # this task to make it to work.
20
+
21
+
22
+ # declare yaml student API to the controller
23
+ cur_path = File.dirname(__FILE__)
24
+ api_file = File.expand_path(File.join(cur_path, "..", "..", "yaml_students", 'yaml_students.rb'))
25
+ require api_file
26
+
27
+ # The controller is a combination of 2 elements:
28
+ # - Controller class
29
+ # Code which will interfere with the external API.
30
+ #
31
+ # The file name must respect the name of the class. 1st letter already capitalized and letter after _ is capitalized.
32
+ # file: my_code.rb => needs to create MyCodeController class
33
+ #
34
+ # - Definition class
35
+ # This class declare any kind of mapping or additional fields to consider.
36
+ # Additionnal fields are unknow by the process. So, those fields will needs to be setup before.
37
+ #
38
+ # file name convention is identical than controller class.
39
+ # file: my_code.rb => needs to create MyCode class
40
+
41
+ controller_file = File.expand_path(File.join(cur_path,'yaml_students_controller.rb'))
42
+ require controller_file # Load controller mapping
43
+
44
+ # Declare
45
+ # - additional objects and their specific process (:connection using basic predefined :controller_create process)
46
+ # - data_mapping :
47
+ # :connection_string => :file_name
48
+ # :course => :training
49
+ #
50
+ # If some data has been added by the controller, the main and process, won't take care and the framework will fails.
51
+ # To eliminate this errors, there is 2 cases:
52
+ # - detect this change in the process or the main.
53
+ # - set it up, using lorj setup function, if the data is declared askable by the controller.
54
+ # The controller can define how the setup have to ask values, and even can get data
55
+ # from itself.
56
+
57
+ class YamlStudents
58
+
59
+ # This is a new object which is known by the controller only.
60
+ # Used to open the yaml file. Generically, I named it :connection.
61
+ # But this can be any name you want. Only the controller will deal with it.
62
+ define_obj(:connection,{
63
+ :create_e => :controller_create # Nothing complex to do. So, simply call the controller create.
64
+ })
65
+
66
+ obj_needs :data, :connection_string, :mapping => :file_name
67
+ undefine_attribute :id # Do not return any predefined ID
68
+ undefine_attribute :name # Do not return any predefined NAME
69
+
70
+ # The student model have to be expanded.
71
+ define_obj(:student)
72
+ # It requires to create a connection to the data, ie opening the yaml file.
73
+ # So, before working with the :student object, the controller requires a connection
74
+ # This connection will be loaded in the memory and provided to the controller
75
+ # when needed.
76
+ # obj_needs :CloudObject update the :student object to requires a connection before.
77
+ obj_needs :CloudObject, :connection
78
+
79
+ # To simplify controller wrapper, we use hdata built by lorj, and passed to the API
80
+ # This hdata is a hash containing mapped data, thanks to set_hdata.
81
+ set_hdata :first_name
82
+ set_hdata :last_name
83
+ # Instead of 'course', the yaml API uses 'training'
84
+ set_hdata :course, :mapping => :training
85
+
86
+ get_attr_mapping :course, :training
87
+ # instead of 'student_name', the yaml API uses 'name'
88
+ get_attr_mapping :student_name, :name
89
+
90
+ # This controller will know how to manage a student file with those data.
91
+ # But note that the file can have a lot of more data than what the process
92
+ # usually manage. It is up to you to increase your process to manage more data.
93
+ # Then each controller may need to define mapping fields.
94
+ end
@@ -0,0 +1,123 @@
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
+
18
+ # This class describes how to process some actions, and will do everything prior
19
+ # this task to make it to work.
20
+
21
+ # This Mock controller keep the data in memory in hash/Array data.
22
+
23
+ # declare yaml student API to the controller
24
+ cur_file = File.expand_path(File.join(File.dirname(File.dirname(__FILE__)), "..", "yaml_students", 'yaml_students.rb'))
25
+ require cur_file
26
+
27
+ # The controller is a combination of 2 elements:
28
+ # - Controller class
29
+ # Code which will interfere with the external API.
30
+ #
31
+ # The file name must respect the name of the class. 1st letter already capitalized and letter after _ is capitalized.
32
+ # file: my_code.rb => needs to create MyCodeController class
33
+ #
34
+ # - Definition class
35
+ # This class declare any kind of mapping or additional fields to consider.
36
+ # Additionnal fields are unknow by the process. So, those fields will needs to be setup before.
37
+ #
38
+ # file name convention is identical than controller class.
39
+ # file: my_code.rb => needs to create MyCode class
40
+
41
+ class YamlStudentsController
42
+ def initialize()
43
+ @@valid_attributes = [:name, :first_name, :last_name, :id, :status, :training]
44
+ end
45
+
46
+ def create(sObjectType, hParams)
47
+ case sObjectType
48
+ when :connection
49
+ required?(hParams, :hdata, :file_name)
50
+ YamlSchool.new(hParams[:hdata, :file_name])
51
+ when :student
52
+ required?(hParams, :connection)
53
+ required?(hParams, :student_name)
54
+
55
+ hParams[:connection].create_student(hParams[:student_name], hParams[:hdata])
56
+ else
57
+ Error "'%s' is not a valid object for 'create'" % sObjectType
58
+ end
59
+ end
60
+
61
+ # This function return a collection which have to provide:
62
+ # functions: [], length, each
63
+ # Used by network process.
64
+ def query(sObjectType, sQuery, hParams)
65
+ case sObjectType
66
+ when :student
67
+ required?(hParams, :connection)
68
+
69
+ hParams[:connection].query_student(sQuery)
70
+ else
71
+ Error "'%s' is not a valid object for 'create'" % sObjectType
72
+ end
73
+
74
+ end
75
+
76
+ def delete(sObjectType, hParams)
77
+ case sObjectType
78
+ when :student
79
+ required?(hParams, :connection)
80
+
81
+ hParams[:connection].delete_student(hParams[sObjectType][:id])
82
+ else
83
+ Error "'%s' is not a valid object for 'create'" % sObjectType
84
+ end
85
+ end
86
+
87
+ def get_attr(oControlerObject, key)
88
+ # This controller function read the data and
89
+ # extract the information requested by the framework.
90
+ # Those data will be mapped to the process data model.
91
+ # The key is an array, to get data from a level tree.
92
+ # [data_l1, data_l2, data_l3] => should retrieve data from structure like data[ data_l2[ data_l3 ] ]
93
+ begin
94
+ attributes = oControlerObject
95
+ raise "get_attr: attribute '%s' is unknown in '%s'. Valid one are : '%s'" % [key[0], oControlerObject.class, @@valid_attributes ] unless @@valid_attributes.include?(key[0])
96
+ Lorj::rhGet(attributes, key)
97
+ rescue => e
98
+ Error "get_attr: Unable to map '%s'. %s" % [key, e.message]
99
+ end
100
+ end
101
+
102
+ def set_attr(oControlerObject, key, value)
103
+ begin
104
+ attributes = oControlerObject
105
+ raise "set_attr: attribute '%s' is unknown in '%s'. Valid one are : '%s'" % [key[0], oControlerObject.class, @@valid_attributes ] unless @@valid_attributes.include?(key[0])
106
+ Lorj::rhSet(attributes, value, key)
107
+ rescue => e
108
+ Error "set_attr: Unable to map '%s' on '%s'" % [key, sObjectType]
109
+ end
110
+ end
111
+
112
+ def update(sObjectType, oObject, hParams)
113
+ case sObjectType
114
+ when :student
115
+ required?(hParams, :connection)
116
+
117
+ hParams[:connection].update_student(oObject)
118
+ else
119
+ Error "'%s' is not a valid object for 'create'" % sObjectType
120
+ end
121
+ end
122
+
123
+ end
@@ -0,0 +1,118 @@
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
+ # Students process - Define specific handlers
18
+ class StudentsProcess
19
+ def create_student(sObjectType, hParams)
20
+ PrcLib::state ("Running creation process for object '%s' = '%s'" % [sObjectType, hParams[:student_name] ])
21
+
22
+ # config object is a reference to runtime/config data.
23
+ oList = Query(sObjectType, {:student_name => hParams[:student_name]})
24
+ case oList.length
25
+ when 0
26
+ oObject = controller_create(sObjectType)
27
+ raise "Student '%s' not created." % hParams[:student_name] if oObject.nil?
28
+ PrcLib::info ("'%s': '%s' created with id %s" % [sObjectType, hParams[:student_name], oObject[:id]])
29
+ when 1
30
+ oObject = oList[0]
31
+ PrcLib::info ("'%s': '%s' loaded with id %s" % [sObjectType, hParams[:student_name], oObject[:id]])
32
+ else
33
+ oObject = oList[0]
34
+ PrcLib::warning("More than one student named '%s' is found: %s records. Selecting the first one and removing duplicates." % [hParams[:student_name], oList.length])
35
+ iCount = 0
36
+ oList[1..-1].each { | elem |
37
+ register(elem)
38
+ iCount += controller_delete(sObjectType)
39
+ }
40
+ PrcLib::info ("'%s': %s duplicated '%s' removed. First loaded with id %s" % [sObjectType, iCount, hParams[:student_name], oObject[:id]])
41
+ end
42
+ oObject
43
+ end
44
+
45
+ # The following handler is inactive.
46
+ # It provides a simple print-out code.
47
+ # If you want to activate it:
48
+ # * uncomment query_student function
49
+ # * update the :student data model
50
+ # on query_e, replace controller_query by query_student
51
+
52
+ # def query_student(sObjectType, sQuery, hParams)
53
+ # PrcLib::state ("Running query process for object '%s' with query '%s'" % [sObjectType, sQuery])
54
+ #
55
+ # oObjects = controller_query(sObjectType, sQuery)
56
+ # raise "Query error." if oObjects.nil?
57
+ #
58
+ # PrcLib::info ("'%s': Queried. %s records found." % [sObjectType, oObjects.length])
59
+ # oObjects
60
+ # end
61
+
62
+ # This handler is inactive.
63
+ # It provides a simple print-out code.
64
+ # If you want to activate it:
65
+ # * uncomment get_student function
66
+ # * update the :student data model
67
+ # on get_e, replace controller_get by get_student
68
+
69
+ # def delete_student(sObjectType, hParams)
70
+ # controller_delete(:student)
71
+ # PrcLib::info ("'%s:%s' student removed" % [hParams[:student, :id], hParams[:student, :name]])
72
+ # end
73
+ end
74
+
75
+ # Declaring your data model and handlers.
76
+ # Process Handlers functions have to be declared before, as lorj check their existence during data model definition.
77
+
78
+ class Lorj::BaseDefinition
79
+
80
+ # We need to define the student object data model and process handlers to use.
81
+ # Process handlers must manipulate data defined here.
82
+ #
83
+ # The controller can redefine object for it needs, but should NEVER impact the main process.
84
+ # The controller can add specific process to deal with internal controller objects.
85
+ # But this should never influence the original process model.
86
+
87
+ # Use define_obj, to declare the new object managed by lorj with process handlers.
88
+ define_obj(:student,
89
+ {
90
+ :create_e => :create_student, # The function to call in the class Students
91
+ :query_e => :controller_query, # We use predefined call to the controller query
92
+ :delete_e => :controller_delete # We use predefined call to the controller delete
93
+ })
94
+
95
+ # obj_needs is used to declare parameters to pass to handlers.
96
+ # :for indicates those parameters to be passed to create_e handler only.
97
+ # Those data (or objects) will be collected and passed to the process handler as hParams.
98
+
99
+ obj_needs :data, :student_name, { :for => [:create_e] }
100
+
101
+ # By default, all data are required.
102
+ # You can set it as optional. Your process will need to deal with this optional data.
103
+ obj_needs_optional
104
+ obj_needs :data, :first_name, { :for => [:create_e] }
105
+ obj_needs :data, :last_name, { :for => [:create_e] }
106
+ obj_needs :data, :course # Note that in this model, the training is renamed as course.
107
+ # the controller will need to map it to 'training'.
108
+
109
+ # def_attribute defines the data model.
110
+ # The process will be able to access those data
111
+ def_attribute :course
112
+ def_attribute :student_name
113
+ def_attribute :first_name
114
+ def_attribute :last_name
115
+ def_attribute :status
116
+
117
+ undefine_attribute :name
118
+ end
@@ -0,0 +1,93 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $APP_PATH = File.dirname(__FILE__)
4
+ require 'lorj'
5
+ require 'ansi'
6
+
7
+ # Load global Config
8
+
9
+ # This object is used to provide configuration data to lorj
10
+
11
+ # The config search is:
12
+ # 1- Application defaults (defaults.yaml) - Not defined by default. update the following line and create defaults.yaml
13
+ # PrcLib.app_defaults = $APP_PATH
14
+ # 2- Local defaults (~/.<Application Name>/config.yaml) - <Application Name> is 'Lorj' by default. Can be updated with following line.
15
+ # PrcLib.app_name = 'myapp'
16
+ # 3 - runtime. Those variables are set, with oConfig[key] = value
17
+
18
+ oConfig = Lorj::Config.new() # Use Simple Config Object
19
+
20
+ # You can use an account object, which add an extra account level
21
+ # between runtime and config.yaml/app default
22
+ # oConfig = Lorj::Account.new('MyAccount') #
23
+
24
+
25
+ # If you want to see what is happening in the framework, uncomment debug settings.
26
+ # PrcLib.level = Logger::DEBUG # Printed out to your console.
27
+ # PrcLib.core_level = 3 # framework debug levels.
28
+
29
+ # Initialize the framework
30
+ hProcesses = [ File.join($APP_PATH, 'process', 'students.rb')]
31
+
32
+ #~ oStudentCore = Lorj::Core.new( oConfig, hProcesses, :mock)
33
+ oStudentCore = Lorj::Core.new( oConfig, hProcesses, File.join($APP_PATH, 'controller', 'yaml_students.rb'))
34
+
35
+ oStudentCore.Create(:connection, :connection_string => "/tmp/students.yaml")
36
+
37
+ puts ANSI.bold("Create 1st student:")
38
+
39
+ # Set the student name to use
40
+ # There is different way to set them... Those lines do the same using config object. Choose what you want.
41
+ oConfig.set(:first_name, 'Robert')
42
+ oConfig[:last_name] = "Redford"
43
+ oConfig[:student_name] = "Robert Redford"
44
+ oConfig[:course] = 'Art Comedy'
45
+
46
+ # Ask the framework to create the object student 'Robert Redford'
47
+ oStudentCore.Create(:student)
48
+
49
+ puts ANSI.bold("Create 2nd student:")
50
+ # We can set runtime configuration instantly from the Create call
51
+ # The following line :
52
+ oStudentCore.Create(:student, {
53
+ student_name: 'Anthony Hopkins',
54
+ first_name: 'Anthony',
55
+ last_name: 'Hopkins',
56
+ course: 'Art Drama'
57
+ })
58
+ # oConfig[:student_name] = "Anthony Hopkins"
59
+ # oConfig[:course] = "Art Drama"
60
+ # oStudentCore.Create(:student)
61
+
62
+ puts ANSI.bold("Create 3rd student:")
63
+ oStudentCore.Create(:student, {
64
+ student_name: "Marilyn Monroe",
65
+ first_name: 'Marilyn',
66
+ last_name: 'Monroe',
67
+ course: 'Art Drama'
68
+ })
69
+ # replaced the following :
70
+ # oConfig[:student_name] = "Anthony Hopkins"
71
+ # oStudentCore.Create(:student)
72
+
73
+ puts ANSI.bold("Create mistake")
74
+ oStudent = oStudentCore.Create(:student, {
75
+ :student_name => "Anthony Mistake",
76
+ :first_name => 'Anthony',
77
+ :last_name => 'Mistake',
78
+ :course => 'what ever you want!!!'
79
+ })
80
+
81
+ puts "Student created '%s'" % oStudent[:attrs]
82
+
83
+ # Because the last student was the mistake one, we can directly delete it.
84
+ # Usually, we use get instead.
85
+ puts ANSI.bold("Remove mistake")
86
+ oStudentCore.Delete(:student)
87
+ puts "Wrong student to remove: %s = %s" % [oStudent[:id], oStudent[:student_name]]
88
+
89
+ puts ANSI.bold("List of students for 'Art Drama':")
90
+ puts oStudentCore.Query(:student, { :course => "Art Drama"}).to_a
91
+
92
+ puts ANSI.bold("Deleted students:")
93
+ puts oStudentCore.Query(:student,{ :status => :removed}).to_a