Class: TSJSON::Intersection

Inherits:
Merge show all
Defined in:
lib/types/intersection.rb

Instance Attribute Summary

Attributes inherited from Merge

#is_normal, #types

Instance Method Summary collapse

Methods inherited from Merge

#initialize

Methods inherited from Base

#index, #property, #valid?

Constructor Details

This class inherits a constructor from TSJSON::Merge

Instance Method Details

#compileObject



53
54
55
56
57
58
59
# File 'lib/types/intersection.rb', line 53

def compile
  return if @compiled
  @compiled = true

  return normal_form.compile unless @is_normal
  merged_object
end

#fieldsObject



45
46
47
# File 'lib/types/intersection.rb', line 45

def fields
  merged_object.fields
end

#intersect_normal(other) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/types/intersection.rb', line 19

def intersect_normal(other)
  #   raise_if_not_composite(other)

  if other.is_a?(Intersection)
    Intersection.new(@types.dup.concat(other.types), true)
  elsif other.is_a?(Union)
    other.types.reduce(Union.new) do |u, t|
      u.union_normal(intersect_normal(t))
    end
  else
    Intersection.new(@types.dup.push(other), true)
  end
end

#normal_formObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/types/intersection.rb', line 5

def normal_form
  unless @normalized
    @normalized =
      @types.reduce(Intersection.new) do |buffer, type|
        if type.is_a?(Merge)
          buffer.intersect_normal(type.normal_form)
        else
          buffer.intersect_normal(type)
        end
      end
  end
  @normalized
end

#to_sObject



49
50
51
# File 'lib/types/intersection.rb', line 49

def to_s
  @types.map(&:to_s).join(' & ')
end

#union_normal(other) ⇒ Object



33
34
35
36
37
# File 'lib/types/intersection.rb', line 33

def union_normal(other)
  #   raise_if_not_composite(other)

  Union.new([self, other], true)
end

#validate(object) ⇒ Object



39
40
41
42
43
# File 'lib/types/intersection.rb', line 39

def validate(object)
  return normal_form.validate(object) unless @is_normal

  merged_object.validate(object)
end