Class: Fitting::Records::Unit::JsonSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/fitting/records/unit/json_schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_schema, tested_bodies) ⇒ JsonSchema

Returns a new instance of JsonSchema.



12
13
14
15
# File 'lib/fitting/records/unit/json_schema.rb', line 12

def initialize(json_schema, tested_bodies)
  @json_schema = json_schema
  @tested_bodies = tested_bodies
end

Instance Attribute Details

#json_schemaObject (readonly)

Returns the value of attribute json_schema.



10
11
12
# File 'lib/fitting/records/unit/json_schema.rb', line 10

def json_schema
  @json_schema
end

Instance Method Details

#bodiesObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/fitting/records/unit/json_schema.rb', line 17

def bodies
  @bodies ||= @tested_bodies.inject([]) do |res, tested_body|
    begin
      next res unless JSON::Validator.validate(@json_schema, tested_body)
      res.push(tested_body)
    rescue JSON::Schema::UriError
      res.push(tested_body)
    end
  end
end

#combinationsObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fitting/records/unit/json_schema.rb', line 28

def combinations
  return @combinations if @combinations
  @combinations = []
  cover_json_schema = Fitting::Cover::JSONSchema.new(@json_schema)
  cover_json_schema.combi.map do |comb|
    @combinations.push(Fitting::Records::Unit::Combination.new(
      comb,
      bodies
    ))
  end
  @combinations
end

#combinations_with_enumObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fitting/records/unit/json_schema.rb', line 41

def combinations_with_enum
  return @combinations_with_enum if @combinations_with_enum
  @combinations_with_enum = []
  qwe = Fitting::Cover::JSONSchema.new(@json_schema).combi + Fitting::Cover::JSONSchemaEnum.new(@json_schema).combi
  qwe.map do |comb|
    @combinations_with_enum.push(Fitting::Records::Unit::Combination.new(
      comb,
      bodies
    ))
  end
  @combinations_with_enum
end

#coverObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fitting/records/unit/json_schema.rb', line 54

def cover
  @cover ||= if bodies == []
               0
             else
               count = 0
               combinations.map do |combination|
                 count += 1 unless combination.valid_bodies == []
               end
               (count + 1) * 100 / (combinations.size + 1)
             end
end

#cover_enumObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fitting/records/unit/json_schema.rb', line 66

def cover_enum
  @cover_enum ||= if bodies == []
    0
  else
    count = 0
    combinations_with_enum.map do |combination|
      count += 1 unless combination.valid_bodies == []
    end
    (count + 1) * 100 / (combinations_with_enum.size + 1)
  end
end