Class: Discerner::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/discerner/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



6
7
8
9
10
# File 'lib/discerner/parser.rb', line 6

def initialize(options={})
  self.options = options
  self.errors = []
  reset_counts
end

Instance Attribute Details

#abandoned_dictionariesObject

Returns the value of attribute abandoned_dictionaries.



3
4
5
# File 'lib/discerner/parser.rb', line 3

def abandoned_dictionaries
  @abandoned_dictionaries
end

#blank_parameter_valuesObject

Returns the value of attribute blank_parameter_values.



3
4
5
# File 'lib/discerner/parser.rb', line 3

def blank_parameter_values
  @blank_parameter_values
end

#errorsObject

Returns the value of attribute errors.



3
4
5
# File 'lib/discerner/parser.rb', line 3

def errors
  @errors
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/discerner/parser.rb', line 3

def options
  @options
end

#updated_categoriesObject

Returns the value of attribute updated_categories.



3
4
5
# File 'lib/discerner/parser.rb', line 3

def updated_categories
  @updated_categories
end

#updated_dictionariesObject

Returns the value of attribute updated_dictionaries.



3
4
5
# File 'lib/discerner/parser.rb', line 3

def updated_dictionaries
  @updated_dictionaries
end

#updated_parameter_value_categoriesObject

Returns the value of attribute updated_parameter_value_categories.



3
4
5
# File 'lib/discerner/parser.rb', line 3

def updated_parameter_value_categories
  @updated_parameter_value_categories
end

#updated_parameter_valuesObject

Returns the value of attribute updated_parameter_values.



3
4
5
# File 'lib/discerner/parser.rb', line 3

def updated_parameter_values
  @updated_parameter_values
end

#updated_parametersObject

Returns the value of attribute updated_parameters.



3
4
5
# File 'lib/discerner/parser.rb', line 3

def updated_parameters
  @updated_parameters
end

Instance Method Details

#deleted?(param) ⇒ Boolean

Returns:

  • (Boolean)


402
403
404
405
# File 'lib/discerner/parser.rb', line 402

def deleted?(param)
  return false if param.blank?
  to_bool(param)
end

#error_message(str, target = nil) ⇒ Object

Raises:

  • (ActiveRecord::Rollback)


385
386
387
388
389
390
# File 'lib/discerner/parser.rb', line 385

def error_message(str, target=nil)
  errors << "#{target}: #{str}"
  puts "ERROR: #{str}"
  reset_counts
  raise ActiveRecord::Rollback
end

#find_or_create_parameter_value(parameter, search_value, name = nil, parameter_value_category_identifier = nil, silent = nil) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/discerner/parser.rb', line 328

def find_or_create_parameter_value(parameter, search_value, name=nil, parameter_value_category_identifier=nil, silent=nil)
  error_message "search value was not provided" if search_value.nil?
  search_value = search_value.to_s
  notification_message "processing parameter value '#{search_value}'"

  parameter_value = Discerner::ParameterValue.where(search_value: search_value, parameter_id: parameter.id).first_or_initialize
  if parameter_value.new_record?
    notification_message "creating parameter value ..."
    parameter_value.created_at = Time.now
  else
    notification_message "updating parameter value ..."
    parameter_value.updated_at = Time.now
  end

  unless parameter_value_category_identifier.blank?
    parameter_value_category = Discerner::ParameterValueCategory.where(unique_identifier: parameter_value_category_identifier, parameter_id: parameter.id).first_or_initialize
    if parameter_value_category.blank?
      error_message "parameter value category with unique identifier #{parameter_value_category_identifier} is not found for parameter #{parameter.name}"
    else
      parameter_value.parameter_value_category = parameter_value_category
    end
  end

  parameter_value.name = name || search_value
  parameter_value.deleted_at = nil
  error_message "parameter value #{search_value} could not be saved: #{parameter_value.errors.full_messages}" unless parameter_value.save
  notification_message 'parameter value saved'
  updated_parameter_values << parameter_value
  parameter_value
end

#find_or_create_parameter_value_category(parameter, unique_identifier, name, display_order = 0, collapse = nil) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/discerner/parser.rb', line 359

def find_or_create_parameter_value_category(parameter, unique_identifier, name, display_order=0, collapse=nil)
  error_message "unique_identifier was not provided" if unique_identifier.nil?
  error_message "name was not provided" if name.nil?

  unique_identifier = unique_identifier.to_s
  notification_message "processing parameter value category '#{unique_identifier}'"

  parameter_value_category = Discerner::ParameterValueCategory.where(unique_identifier: unique_identifier, parameter_id: parameter.id).first_or_initialize
  if parameter_value_category.new_record?
    notification_message "creating parameter value category..."
    parameter_value_category.created_at = Time.now
  else
    notification_message "updating parameter value category ..."
    parameter_value_category.updated_at = Time.now
  end

  parameter_value_category.name = name
  parameter_value_category.display_order = display_order.to_i
  parameter_value_category.collapse = collapse
  parameter_value_category.deleted_at = nil
  error_message "parameter value category #{unique_identifier} could not be saved: #{parameter_value_category.errors.full_messages}" unless parameter_value_category.save
  notification_message 'parameter value category saved'
  updated_parameter_value_categories << parameter_value_category
  parameter_value_category
end

#find_or_initialize_parameter_type(name) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/discerner/parser.rb', line 312

def find_or_initialize_parameter_type(name)
  error_message "Parameter type name has to be provided" if name.blank?
  error_message "'integer' parameter type has been replaced with 'numeric', please update your dictionary definition" if /integer/.match(name.downcase)

  ## find or initialize parameter type
  parameter_type = Discerner::ParameterType.find_or_initialize_by(name: name.downcase)
  if parameter_type.new_record?
    notification_message "Creating parameter type '#{name}'"
    parameter_type.created_at = Time.now
  else
    notification_message "Parameter type '#{name}' already exists"
  end
  error_message "Parameter type #{name} could not be saved: #{parameter_type.errors.full_messages}" unless parameter_type.save
  return parameter_type
end

#load_parameter_value_categories_from_source(parameter, hash) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/discerner/parser.rb', line 239

def load_parameter_value_categories_from_source(parameter, hash)
  error_message 'parameter value category  definition was not provided' if hash.blank?

  model_name  = hash[:model]
  method_name = hash[:method]
  error_message "model and method must be defined for parameter value category source" if model_name.blank? || method_name.blank?

  source_model = model_name.safe_constantize
  error_message "model '#{model_name}' could not be found" if source_model.blank?

  if source_model.respond_to?(method_name)
    notification_message "method '#{method_name}' recognized as a class method"

    parameter_value_categories = source_model.send(method_name)

    error_message "method '#{method_name}' did not return an array of values" if parameter_value_categories.blank? || !parameter_value_categories.kind_of?(Array)

    if parameter_value_categories.select { |parameter_value_category| !parameter_value_category.has_key?(:name) || !parameter_value_category.has_key?(:unique_identifier)}.any?
      error_message "method '#{method_name}' does not adhere to the interface"
    end

    parameter_value_categories.map{|parameter_value_category| find_or_create_parameter_value_category(parameter, parameter_value_category[:unique_identifier], parameter_value_category[:name], parameter_value_category[:display_order], parameter_value_category[:collapse])}
  else
    notification_message "method '#{method_name}' is not recognized as a class method, will try it on instance.."
    error_message "model '#{model_name}' does no respond to :all method" if !source_model.respond_to?(:all)

    parameter_value_category_sources = source_model.send(:all)
    error_message "model '#{method_name}' did not return an array of instances" if parameter_value_category_sources.blank?

    parameter_value_category_sources.each do |row|
      error_message "model '#{model_name}' instance does no respond to #{method_name} method" if !row.respond_to?(method_name)
      find_or_create_parameter_value_category(parameter, row.send(method_name))
    end
  end
end

#load_parameter_value_from_source(parameter, hash) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/discerner/parser.rb', line 203

def load_parameter_value_from_source(parameter, hash)
  error_message 'parameter value definition was not provided' if hash.blank?

  model_name  = hash[:model]
  method_name = hash[:method]
  error_message "model and method must be defined for parameter source" if model_name.blank? || method_name.blank?

  source_model = model_name.safe_constantize
  error_message "model '#{model_name}' could not be found" if source_model.blank?

  if source_model.respond_to?(method_name)
    notification_message "method '#{method_name}' recognized as a class method"

    parameter_values = source_model.send(method_name)

    error_message "method '#{method_name}' did not return an array of values" if parameter_values.blank? || !parameter_values.kind_of?(Array)

    if parameter_values.select { |parameter_value| !parameter_value.has_key?(:name) || !parameter_value.has_key?(:search_value) }.any?
      error_message "method '#{method_name}' does not adhere to the interface"
    end

    parameter_values.map{|parameter_value| find_or_create_parameter_value(parameter, parameter_value[:search_value], parameter_value[:name], parameter_value[:parameter_value_category]) }
  else
    notification_message "method '#{method_name}' is not recognized as a class method, will try it on instance.."
    error_message "model '#{model_name}' does no respond to :all method" if !source_model.respond_to?(:all)

    search_value_sources = source_model.send(:all)
    error_message "model '#{method_name}' did not return an array of instances" if search_value_sources.blank?

    search_value_sources.each do |row|
      error_message "model '#{model_name}' instance does no respond to #{method_name} method" if !row.respond_to?(method_name)
      find_or_create_parameter_value(parameter, row.send(method_name))
    end
  end
end

#notification_message(str) ⇒ Object



392
393
394
# File 'lib/discerner/parser.rb', line 392

def notification_message(str)
  puts str unless self.options[:trace].blank?
end

#parse_dictionaries(str) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/discerner/parser.rb', line 22

def parse_dictionaries(str)
  reset_counts
  hash_from_file = YAML.load(str)

  # find or initialize dictionaries
  dictionaries_from_file = hash_from_file[:dictionaries]
  error_message 'No dictionaries detected.' if dictionaries_from_file.blank?

  Discerner::Dictionary.transaction do
    dictionaries_from_file.each do |dictionary_from_file|
      dictionary = parse_dictionary(dictionary_from_file)

      ## find or initialize parameter categories
      parameter_categories_from_file = dictionary_from_file[:parameter_categories]
      error_message 'no parameter categories detected' if parameter_categories_from_file.blank?

      parameter_categories_from_file.each do |parameter_category_from_file|
        parameter_category = parse_parameter_category(dictionary, parameter_category_from_file)

        ## find or initialize parameters
        parameters_from_file = parameter_category_from_file[:parameters]
        error_message 'no parameters detected' if parameters_from_file.blank?

        parameters_from_file.each do |parameter_from_file|
          parameter = parse_parameter(parameter_category, parameter_from_file)

          search_identifiers = parameter_from_file[:search]
          unless search_identifiers.blank?
            ## find or initialize parameter value categories
            unless search_identifiers[:parameter_value_categories].blank?
              search_identifiers[:parameter_value_categories].each do |parameter_value_category_from_file|
                parse_parameter_value_category(parameter, parameter_value_category_from_file)
              end
            end

            unless search_identifiers[:parameter_value_categories_source].blank?
              load_parameter_value_categories_from_source(parameter, search_identifiers[:parameter_value_categories_source])
            end

            ## find or initialize parameter values
            unless search_identifiers[:parameter_values].blank?
              search_identifiers[:parameter_values].each do |parameter_value_from_file|
                parse_parameter_value(parameter, parameter_value_from_file)
              end
            end

            unless search_identifiers[:source].blank?
              load_parameter_value_from_source(parameter, search_identifiers[:source])
            end

            unless search_identifiers[:allow_empty_values] == false
              blank_parameter_values << find_or_create_parameter_value(parameter, '', 'None', nil, true)
            end
          end
        end
      end
    end
  end
  notification_message "cleaing up ..."
  cleanup unless errors.any?
end

#parse_dictionary(hash) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/discerner/parser.rb', line 84

def parse_dictionary(hash)
  error_message 'dictionary definition was not provided' if hash.blank?

  dictionary_name = hash[:name]
  error_message 'dictionary name cannot be blank' if dictionary_name.blank?
  notification_message "processing dictionary '#{dictionary_name}'"

  dictionary = Discerner::Dictionary.find_or_initialize_by(name: dictionary_name)
  dictionary.deleted_at     = nil

  if dictionary.new_record?
    notification_message "creating dictionary ..."
    dictionary.created_at = Time.now
  else
    notification_message "updating dictionary ..."
    dictionary.updated_at = Time.now
  end
  error_message "dictionary could not be saved: #{dictionary.errors.full_messages}", dictionary_name unless dictionary.save
  notification_message 'dictionary saved'
  updated_dictionaries << dictionary
  dictionary
end

#parse_operators(str) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/discerner/parser.rb', line 275

def parse_operators(str)
   hash_from_file = YAML.load(str)

   operators_from_file = hash_from_file[:operators]
   error_message 'No operators detected in the file.' if operators_from_file.blank?

   Discerner::Operator.transaction do
     operators_from_file.each do |operator_from_file|
       error_message 'unique identifier has to be defined' if operator_from_file[:unique_identifier].blank?

       operator = Discerner::Operator.find_or_initialize_by(unique_identifier: operator_from_file[:unique_identifier])
       if operator.new_record?
         notification_message "creating operator '#{operator_from_file[:unique_identifier]}'"
         operator.created_at = Time.now
       else
         notification_message "operator '#{operator_from_file[:unique_identifier]}' already exists and will be updated"
         operator.updated_at = Time.now
       end
       operator.deleted_at = operator_from_file[:deleted].blank? ? nil : Time.now

       unless operator_from_file[:parameter_types].blank?
         operator.parameter_types.destroy_all

         operator_from_file[:parameter_types].each do |parameter_type_from_file|
           parameter_type = find_or_initialize_parameter_type(parameter_type_from_file)
           operator.parameter_types << parameter_type
         end
       end
       operator.symbol        = operator_from_file[:symbol]
       operator.text          = operator_from_file[:text]
       operator.operator_type = operator_from_file[:type]
       #operator.deleted_at    = operator_from_file[:deleted].blank? ? nil : Time.now
       error_message "Operator could not be saved: #{operator.errors.full_messages}" unless operator.save
     end
  end
end

#parse_parameter(parameter_category, hash) ⇒ Object



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
# File 'lib/discerner/parser.rb', line 130

def parse_parameter(parameter_category, hash)
  error_message 'parameter definition was not provided' if hash.blank?

  parameter_name = hash[:name]
  error_message 'parameter name cannot be blank' if parameter_name.blank?

  notification_message "processing parameter '#{parameter_name}'"
  unique_identifier = hash[:unique_identifier]
  error_message "unique_identifier cannot be blank", parameter_name if unique_identifier.blank?

  existing_parameter    = Discerner::Parameter.
                          includes({parameter_category: :dictionary}).
                          where('discerner_parameters.unique_identifier = ? and discerner_dictionaries.id = ?', unique_identifier, parameter_category.dictionary.id).
                          references(:discerner_parameters, :discerner_dictionaries).first
  parameter             = existing_parameter || Discerner::Parameter.new(unique_identifier: unique_identifier, parameter_category: parameter_category)

  parameter.name        = parameter_name
  parameter.deleted_at  = nil
  parameter.exclusive   = hash[:exclusive].nil? ? true : to_bool(hash[:exclusive])
  parameter.parameter_category  = parameter_category

  search_identifiers = hash[:search]
  unless search_identifiers.blank?
    error_message "Searchable parameter should search model, search method and parameter_type defined." if
      search_identifiers[:model].blank? || search_identifiers[:method].blank? || search_identifiers[:parameter_type].blank?

    parameter.search_model      = search_identifiers[:model].to_s
    parameter.search_method     = search_identifiers[:method].to_s
    parameter.hidden_in_search  = search_identifiers[:hidden].blank? ? false : search_identifiers[:hidden]
    parameter.parameter_type    = find_or_initialize_parameter_type(search_identifiers[:parameter_type])
  end

  export_identifiers = hash[:export]
  unless export_identifiers.blank?
    error_message "Exportable parameter should export model and export method defined." if
      export_identifiers[:model].blank? || export_identifiers[:method].blank?

    parameter.export_model      = export_identifiers[:model].to_s
    parameter.export_method     = export_identifiers[:method].to_s
    parameter.hidden_in_export  = export_identifiers[:hidden].blank? ? false : export_identifiers[:hidden]
  end

  if parameter.new_record?
    notification_message "creating parameter ..."
    parameter.created_at = Time.now
  else
    notification_message "updating parameter ..."
    parameter.updated_at = Time.now
  end

  error_message "parameter could not be saved: #{parameter.errors.full_messages}", parameter_name unless parameter.save
  notification_message 'parameter saved'
  updated_parameters << parameter
  parameter
end

#parse_parameter_category(dictionary, hash) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/discerner/parser.rb', line 107

def parse_parameter_category(dictionary, hash)
  error_message 'parameter category definition was not provided' if hash.blank?

  parameter_category_name = hash[:name]
  error_message 'parameter category name cannot be blank' if parameter_category_name.blank?
  notification_message "processing parameter category  '#{parameter_category_name}'"

  parameter_category = Discerner::ParameterCategory.where(name: parameter_category_name, dictionary_id: dictionary.id).first_or_initialize
  parameter_category.deleted_at = nil

  if parameter_category.new_record?
    notification_message "creating parameter category ..."
    parameter_category.created_at = Time.now
  else
    notification_message "updating parameter category ..."
    parameter_category.updated_at = Time.now
  end
  error_message "parameter category could not be saved: #{parameter_category.errors.full_messages}", parameter_category_name unless parameter_category.save
  notification_message 'parameter category saved'
  updated_categories << parameter_category
  parameter_category
end

#parse_parameter_value(parameter, hash) ⇒ Object



186
187
188
189
190
191
# File 'lib/discerner/parser.rb', line 186

def parse_parameter_value(parameter, hash)
  error_message 'parameter value definition was not provided' if hash.blank?
  search_value = hash[:search_value]
  error_message 'parameter value search_value cannot be blank' if search_value.nil?
  find_or_create_parameter_value(parameter, search_value, hash[:name], hash[:parameter_value_category], false)
end

#parse_parameter_value_category(parameter, hash) ⇒ Object



193
194
195
196
197
198
199
200
201
# File 'lib/discerner/parser.rb', line 193

def parse_parameter_value_category(parameter, hash)
  error_message 'parameter value category definition was not provided' if hash.blank?
  unique_identifier = hash[:unique_identifier]
  name = hash[:name]

  error_message 'parameter value category unique_identifier cannot be blank' if unique_identifier.nil?
  error_message 'parameter value category name cannot be blank' if name.nil?
  find_or_create_parameter_value_category(parameter, unique_identifier, name, hash[:display_order], hash[:collapse])
end

#reset_countsObject



12
13
14
15
16
17
18
19
20
# File 'lib/discerner/parser.rb', line 12

def reset_counts
  self.updated_dictionaries = []
  self.updated_categories = []
  self.updated_parameters = []
  self.updated_parameter_values = []
  self.updated_parameter_value_categories = []
  self.blank_parameter_values = []
  self.abandoned_dictionaries = []
end

#to_bool(s) ⇒ Object



396
397
398
399
400
# File 'lib/discerner/parser.rb', line 396

def to_bool(s)
  return true if s == true || s =~ (/^(true|t|yes|y|1)$/i)
  return false if s == false || s.blank? || s =~ (/^(false|f|no|n|0)$/i)
  error_message("invalid value for Boolean: \"#{s}\"")
end