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.



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

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.



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

def json_schema
  @json_schema
end

Instance Method Details

#bodiesObject



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

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



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

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

#coverObject



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

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