@@ -49,7 +49,7 @@ class DataFlowSchema
49
49
50
50
include DataShift ::Logging
51
51
52
- attr_reader :configuration , :nodes
52
+ attr_reader :nodes , :raw_data , :yaml_data
53
53
54
54
def initialize
55
55
@nodes = DataShift ::NodeCollection . new
@@ -59,16 +59,19 @@ def sources
59
59
@nodes . collect ( &:method_binding ) . collect ( &:source )
60
60
end
61
61
62
- def prepare_from_klass ( klass )
62
+ # Build the node collection from a Class, that is for each operator in scope
63
+ # create a method binding and a node context, and add to collection.
64
+ #
65
+ def prepare_from_klass ( klass , doc_context = nil )
66
+
67
+ context = doc_context || DocContext . new ( klass )
63
68
64
69
@nodes = DataShift ::NodeCollection . new
65
70
66
71
klass_to_model_methods ( klass ) . each_with_index do |mm , i |
67
- puts "DataFlowSchema - #{ mm . operator } "
68
-
69
72
binding = MethodBinding . new ( mm . operator , i , mm )
70
73
71
- @nodes << DataShift ::NodeContext . new ( DocContext . new ( klass ) , binding , i , nil )
74
+ @nodes << DataShift ::NodeContext . new ( context , binding , i , nil )
72
75
end
73
76
74
77
@nodes
@@ -97,32 +100,44 @@ def klass_to_model_methods(klass)
97
100
end
98
101
99
102
model_methods
100
-
101
103
end
102
104
103
105
def prepare_from_file ( file_name , locale_key = 'data_flow_schema' )
104
- yaml = YAML . load ( File . read ( file_name ) )
106
+ @raw_data = File . read ( file_name )
107
+ yaml = YAML . load ( raw_data )
105
108
106
109
prepare_from_yaml ( yaml , locale_key )
107
110
end
108
111
109
112
def prepare_from_string ( text , locale_key = 'data_flow_schema' )
113
+ @raw_data = text
110
114
yaml = YAML . load ( text )
111
115
112
116
prepare_from_yaml ( yaml , locale_key )
113
117
end
114
118
115
119
def prepare_from_yaml ( yaml , locale_key = 'data_flow_schema' )
116
120
121
+ @yaml_data = yaml
122
+
117
123
@nodes = NodeCollection . new
118
124
119
125
raise RuntimeError . new ( "Bad YAML syntax - No key #{ locale_key } found in #{ yaml } " ) unless yaml [ locale_key ]
120
126
121
- klass_section = yaml [ locale_key ]
127
+ locale_section = yaml [ locale_key ]
122
128
123
- klass = klass_section . keys . first
129
+ klass = locale_section . keys . first
130
+
131
+ klass_section = locale_section [ klass ]
132
+
133
+ # The over all doc context
134
+ doc = DocContext . new ( MapperUtils . class_from_string_or_raise ( klass ) )
124
135
125
- yaml_nodes = klass_section [ klass ] [ 'nodes' ]
136
+ nodes . doc_context = doc
137
+
138
+ DataShift ::Transformer . factory { |f | f . configure_from_yaml ( klass , klass_section ) }
139
+
140
+ yaml_nodes = klass_section [ 'nodes' ]
126
141
127
142
logger . info ( "Read Data Schema Nodes: #{ yaml_nodes . inspect } " )
128
143
@@ -131,11 +146,6 @@ def prepare_from_yaml(yaml, locale_key = 'data_flow_schema')
131
146
raise 'Bad syntax in flow schema YAML - Nodes should be a sequence'
132
147
end
133
148
134
- nodes = [ ]
135
-
136
- # The over all doc context
137
- doc = DocContext . new ( MapperUtils . class_from_string_or_raise ( klass ) )
138
-
139
149
yaml_nodes . each_with_index do |keyed_node , i |
140
150
141
151
unless ( keyed_node . keys . size == 1 )
@@ -149,28 +159,23 @@ def prepare_from_yaml(yaml, locale_key = 'data_flow_schema')
149
159
# - project:
150
160
# heading:
151
161
# source: "title"
152
- # destination : "Title"
162
+ # presentation : "Title"
153
163
# operator: title
154
164
# operator_type: has_many
155
- #
156
- # - project_owner_budget:
157
- # heading:
158
- # destination: "Budget"
159
- # operator: owner.budget
160
- #
161
165
logger . info ( "Node Data: #{ keyed_node . inspect } " )
162
166
163
167
# type one of ModelMethod.supported_types_enum
164
-
165
168
section = keyed_node . values . first
166
169
167
170
operator = Operator . new ( section [ 'operator' ] , :method ) if ( section [ 'operator' ] )
168
171
169
172
model_method = ModelMethod . new ( klass , operator , section [ 'operator_type' ] ) if ( section [ 'operator_type' ] )
170
173
171
- method_binding = MethodBinding . new ( section [ 'heading' ] [ 'source' ] , i , model_method )
174
+ source = section . fetch ( 'heading' , { } ) . fetch ( 'source' , nil )
172
175
173
- doc . headers . add ( section [ 'heading' ] [ 'source' ] )
176
+ method_binding = MethodBinding . new ( source , i , model_method )
177
+
178
+ doc . headers . add ( source )
174
179
175
180
node = DataShift ::NodeContext . new ( doc , method_binding , i , nil )
176
181
@@ -180,50 +185,5 @@ def prepare_from_yaml(yaml, locale_key = 'data_flow_schema')
180
185
nodes
181
186
end
182
187
183
- private
184
-
185
- attr_accessor :current_review_object
186
-
187
- attr_accessor :model_object
188
-
189
- def row_to_node_collection ( review_object , row )
190
- # The section name, can be used as the state, for linking whole section, rather than at field level
191
- link_state = row [ :link_state ] || current_section
192
- link_title = row [ :link_title ]
193
-
194
- @current_review_object = review_object
195
-
196
- # The review partial can support whole objects, or low level data from method call defined in the DSL
197
- if ( row [ :method ] . blank? )
198
- node_collection . add ( row [ :title ] , review_object , link_state . to_s , link_title )
199
- else
200
- # rubocop:disable Style/IfInsideElse
201
- if ( review_object . respond_to? ( :each ) )
202
- review_object . each do |o |
203
- @current_review_object = o
204
- node_collection . add ( row [ :title ] , send_chain ( row [ :method ] ) , link_state . to_s , link_title )
205
- end
206
- else
207
- node_collection . add ( row [ :title ] , send_chain ( row [ :method ] ) , link_state . to_s , link_title )
208
- end
209
-
210
- end
211
- end
212
-
213
- def find_association ( method_chain )
214
- arr = method_chain . to_s . split ( '.' )
215
-
216
- arr . inject ( model_object ) { |o , a | o . send ( a ) }
217
- end
218
-
219
- def send_chain ( method_chain )
220
- arr = method_chain . to_s . split ( '.' )
221
- begin
222
- arr . inject ( current_review_object ) { |o , a | o . send ( a ) }
223
- rescue => e
224
- Rails . logger . error ( "Failed to process method chain #{ method_chain } : #{ e . message } " )
225
- return I18n . t ( '.enrollment_review.missing_data' )
226
- end
227
- end
228
188
end
229
189
end
0 commit comments