Class: Spout::Models::CoverageResult

Inherits:
Object
  • Object
show all
Defined in:
lib/spout/models/coverage_result.rb

Overview

Contains the coverage of a specific variable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, csv_values) ⇒ CoverageResult

Returns a new instance of CoverageResult.



13
14
15
16
17
18
19
20
21
# File 'lib/spout/models/coverage_result.rb', line 13

def initialize(column, csv_values)
  load_json(column)
  load_valid_values

  @csv_values = csv_values
  @values_test = check_values
  @variable_type_test = check_variable_type
  @domain_test = check_domain_specified
end

Instance Attribute Details

#csv_valuesObject

Returns the value of attribute csv_values.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def csv_values
  @csv_values
end

#domain_testObject

Returns the value of attribute domain_test.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def domain_test
  @domain_test
end

#errorObject

Returns the value of attribute error.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def error
  @error
end

#error_messageObject

Returns the value of attribute error_message.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def error_message
  @error_message
end

#file_name_testObject

Returns the value of attribute file_name_test.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def file_name_test
  @file_name_test
end

#jsonObject

Returns the value of attribute json.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def json
  @json
end

#json_id_testObject

Returns the value of attribute json_id_test.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def json_id_test
  @json_id_test
end

#valid_valuesObject

Returns the value of attribute valid_values.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def valid_values
  @valid_values
end

#values_testObject

Returns the value of attribute values_test.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def values_test
  @values_test
end

#variable_type_testObject

Returns the value of attribute variable_type_test.



9
10
11
# File 'lib/spout/models/coverage_result.rb', line 9

def variable_type_test
  @variable_type_test
end

Instance Method Details

#check_domain_specifiedObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/spout/models/coverage_result.rb', line 53

def check_domain_specified
  if @json["type"] != "choices" && domain_name == ""
    true
  else
    domain_file = Dir.glob("domains/**/#{@json['domain'].to_s.downcase}.json", File::FNM_CASEFOLD).first
    if domain_json = JSON.parse(File.read(domain_file, encoding: "utf-8")) rescue false
      return domain_json.is_a?(Array)
    end
    false
  end
end

#check_valuesObject



45
46
47
# File 'lib/spout/models/coverage_result.rb', line 45

def check_values
  @json["type"] != "choices" || (@valid_values | @csv_values.compact).size == @valid_values.size
end

#check_variable_typeObject



49
50
51
# File 'lib/spout/models/coverage_result.rb', line 49

def check_variable_type
  Spout::Tests::VariableTypeValidation::VALID_VARIABLE_TYPES.include?(@json["type"])
end

#domain_nameObject



69
70
71
# File 'lib/spout/models/coverage_result.rb', line 69

def domain_name
  @json["domain"].to_s.downcase.strip
end

#errored?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/spout/models/coverage_result.rb', line 65

def errored?
  error == true
end

#load_json(column) ⇒ Object



23
24
25
26
27
28
# File 'lib/spout/models/coverage_result.rb', line 23

def load_json(column)
  file = Dir.glob("variables/**/#{column.to_s.downcase}.json", File::FNM_CASEFOLD).first
  @file_name_test = !file.nil?
  @json = JSON.parse(File.read(file, encoding: "utf-8")) rescue @json = {}
  @json_id_test = (@json["id"].to_s.downcase == column)
end

#load_valid_valuesObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/spout/models/coverage_result.rb', line 30

def load_valid_values
  valid_values = []
  if @json["type"] == "choices" || domain_name != ""
    file = Dir.glob("domains/**/#{@json['domain'].to_s.downcase}.json", File::FNM_CASEFOLD).first
    if json = JSON.parse(File.read(file, encoding: "utf-8")) rescue false
      valid_values = json.collect { |hash| hash["value"] }
    end
  end
  @valid_values = valid_values
end

#number_of_errorsObject



41
42
43
# File 'lib/spout/models/coverage_result.rb', line 41

def number_of_errors
  @file_name_test && @json_id_test && @values_test && @variable_type_test && @domain_test ? 0 : 1
end