Class: DataShift::DataFlowSchema

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/datashift/mapping/data_flow_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logdir, #logdir=, #logger, #verbose

Constructor Details

#initializeDataFlowSchema

Returns a new instance of DataFlowSchema.



54
55
56
# File 'lib/datashift/mapping/data_flow_schema.rb', line 54

def initialize
  @nodes = DataShift::NodeCollection.new
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



52
53
54
# File 'lib/datashift/mapping/data_flow_schema.rb', line 52

def nodes
  @nodes
end

#raw_dataObject (readonly)

Returns the value of attribute raw_data.



52
53
54
# File 'lib/datashift/mapping/data_flow_schema.rb', line 52

def raw_data
  @raw_data
end

#yaml_dataObject (readonly)

Returns the value of attribute yaml_data.



52
53
54
# File 'lib/datashift/mapping/data_flow_schema.rb', line 52

def yaml_data
  @yaml_data
end

Instance Method Details

#klass_to_model_methods(klass) ⇒ Object

Helpers for dealing with Active Record models and collections Catalogs the supplied Klass and builds set of expected/valid Headers for Klass



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/datashift/mapping/data_flow_schema.rb', line 83

def klass_to_model_methods(klass)

  op_types_in_scope = DataShift::Configuration.call.op_types_in_scope

  collection = ModelMethods::Manager.catalog_class(klass)

  model_methods = []

  if collection

    collection.each { |mm| model_methods << mm if(op_types_in_scope.include? mm.operator_type) }

    remove = DataShift::Transformation::Remove.new

    remove.unwanted_model_methods model_methods
  end

  model_methods
end

#prepare_from_file(file_name, locale_key = 'data_flow_schema') ⇒ Object



103
104
105
106
107
108
# File 'lib/datashift/mapping/data_flow_schema.rb', line 103

def prepare_from_file(file_name, locale_key = 'data_flow_schema')
  @raw_data = File.read(file_name)
  yaml = YAML.load(raw_data)

  prepare_from_yaml(yaml, locale_key)
end

#prepare_from_klass(klass, doc_context = nil) ⇒ Object

Build the node collection from a Class, that is for each operator in scope create a method binding and a node context, and add to collection.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/datashift/mapping/data_flow_schema.rb', line 65

def prepare_from_klass( klass, doc_context = nil )

  context = doc_context || DocContext.new(klass)

  @nodes = DataShift::NodeCollection.new

  klass_to_model_methods( klass ).each_with_index do |mm, i|
    binding = MethodBinding.new(mm.operator, i, mm)

    @nodes << DataShift::NodeContext.new(context, binding, i, nil)
  end

  @nodes
end

#prepare_from_string(text, locale_key = 'data_flow_schema') ⇒ Object



110
111
112
113
114
115
# File 'lib/datashift/mapping/data_flow_schema.rb', line 110

def prepare_from_string(text, locale_key = 'data_flow_schema')
  @raw_data = text
  yaml = YAML.load(text)

  prepare_from_yaml(yaml, locale_key)
end

#prepare_from_yaml(yaml, locale_key = 'data_flow_schema') ⇒ Object

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/datashift/mapping/data_flow_schema.rb', line 117

def prepare_from_yaml(yaml, locale_key = 'data_flow_schema')

  @yaml_data = yaml

  @nodes = NodeCollection.new

  raise RuntimeError.new("Bad YAML syntax  - No key #{locale_key} found in #{yaml}") unless yaml[locale_key]

  locale_section = yaml[locale_key]

  class_name = locale_section.keys.first

  klass = MapperUtils.class_from_string_or_raise(class_name)

  # The over all doc context
  doc = DocContext.new(klass)
  nodes.doc_context = doc

  klass_section = locale_section[class_name]

  DataShift::Transformation.factory { |f| f.configure_from_yaml(class_name, klass_section) }

  yaml_nodes = klass_section['nodes']

  logger.info("Read Data Schema Nodes: #{yaml_nodes.inspect}")

  unless(yaml_nodes.is_a?(Array))
    Rails.logger.error('Bad syntax in flow schema YAML - Nodes should be a sequence')
    raise 'Bad syntax in flow schema YAML - Nodes should be a sequence'
  end

  model_method_mgr = ModelMethods::Manager.catalog_class(klass)

  yaml_nodes.each_with_index do |keyed_node, i|

    unless(keyed_node.keys.size == 1)
      Rails.logger.error('Bad syntax in flow schema YAML - Section should be keyed hash')
      raise 'Bad syntax in flow schema YAML - Section should be keyed hash'
    end

    # data_flow_schema:
    #   Project:
    #     nodes:
    #       - project:
    #           heading:
    #             source: "title"
    #             presentation: "Title"
    #           operator: title
    #           operator_type: has_many
    #
    logger.info("Node Data: #{keyed_node.inspect}")

    # type one of ModelMethod.supported_types_enum
    section = keyed_node.values.first

    if(section['operator'])
      # Find the domain model method details
      model_method = model_method_mgr.search(section['operator'])

      unless model_method
        operator_type = section['operator_type'] || :method

        model_method = model_method_mgr.insert(section['operator'], operator_type)
      end
    end

    source = section.fetch('heading', {}).fetch('source', nil)

    doc.headers.add( source )

    method_binding = MethodBinding.new(source, i, model_method)

    node = DataShift::NodeContext.new(doc, method_binding, i, nil)

    nodes << node
  end

  nodes
end

#sourcesObject



58
59
60
# File 'lib/datashift/mapping/data_flow_schema.rb', line 58

def sources
  @nodes.collect(&:method_binding).collect(&:source)
end