Module: Foobara::Types::Type::Concerns::Reflection

Includes:
Concern
Included in:
Foobara::Types::Type
Defined in:
foobara-0.0.110/projects/types/src/type/concerns/reflection.rb

Instance Method Summary collapse

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Instance Method Details

#deep_types_depended_onObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'foobara-0.0.110/projects/types/src/type/concerns/reflection.rb', line 75

def deep_types_depended_on
  result = Set.new
  to_process = types_depended_on

  until to_process.empty?
    type = to_process.first
    to_process.delete(type)

    next if result.include?(type)

    result << type

    to_process |= type.types_depended_on
  end

  result.select(&:registered?)
end

#inspectObject



164
165
166
167
168
169
170
171
172
173
174
# File 'foobara-0.0.110/projects/types/src/type/concerns/reflection.rb', line 164

def inspect
  # :nocov:
  name = if scoped_path_set?
           scoped_full_name
         else
           "Anonymous #{base_type.type_symbol}"
         end

  "#<Type:#{name}:0x#{object_id.to_s(16)} #{declaration_data}>"
  # :nocov:
end

#type_at_path(data_path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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
# File 'foobara-0.0.110/projects/types/src/type/concerns/reflection.rb', line 93

def type_at_path(data_path)
  path_parts = DataPath.for(data_path).path

  path_part, *path_parts = path_parts

  next_type = case path_part
              when :"#"
                unless element_type
                  # :nocov:
                  raise "Expected element_type to be set but is not"
                  # :nocov:
                end

                element_type
              when Symbol
                case element_types
                when ::Hash
                  unless element_types.key?(path_part)
                    # :nocov:
                    raise "Expected element type to have key #{path_part} but does not"
                    # :nocov:
                  end

                  element_types[path_part]
                when Types::Type
                  unless element_types.extends?(BuiltinTypes[:attributes])
                    # :nocov:
                    raise "Expected element type to be a Type but is not"
                    # :nocov:
                  end

                  # TODO: We assume it's attributes here but maybe we should assert that
                  element_types.element_types[path_part]
                when nil
                  # :nocov:
                  raise "Expected element_types to be set but is not"
                  # :nocov:
                else
                  # :nocov:
                  raise "Unsure how to handle path part #{path_part}"
                  # :nocov:
                end
              when Integer
                if extends?(BuiltinTypes[:tuple])
                  element_types[path_part]
                elsif extends?(BuiltinTypes[:array])
                  element_type
                else
                  # :nocov:
                  raise "Unsure how to handle path part #{path_part}"
                  # :nocov:
                end
              else
                # :nocov:
                raise "Bad path part #{path_part}"
                # :nocov:
              end

  unless next_type
    # :nocov:
    raise "Expected to find a type at #{path_part}"
    # :nocov:
  end

  if path_parts.empty?
    next_type
  else
    next_type.type_at_path(path_parts)
  end
end

#types_depended_on(result = nil) ⇒ Object

as soon as we hit a registered type, don’t go further down that path



9
10
11
12
13
14
15
16
17
18
19
20
21
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
# File 'foobara-0.0.110/projects/types/src/type/concerns/reflection.rb', line 9

def types_depended_on(result = nil)
  remove_sensitive = TypeDeclarations.foobara_manifest_context_remove_sensitive?

  start = result.nil?
  result ||= Set.new
  return if result.include?(self)

  result << self

  return if !start && registered?

  to_process = types_to_add_to_manifest

  if element_types
    to_process += case element_types
                  when Type
                    if remove_sensitive && element_types.sensitive?
                      # TODO: test this code path
                      # :nocov:
                      []
                      # :nocov:
                    else
                      [element_types]
                    end
                  when ::Hash
                    element_types.values.select do |value|
                      if !remove_sensitive || !value.sensitive?
                        value.is_a?(Type)
                      end
                    end
                  when ::Array
                    if remove_sensitive
                      element_types.reject(&:sensitive?)
                    else
                      element_types
                    end
                  else
                    # :nocov:
                    raise "Not sure how to find dependent types for #{element_types}"
                    # :nocov:
                  end
  end

  to_process.each do |type|
    type.types_depended_on(result)
  end

  if start
    result = result.select { |type| type.registered? && type != self }.to_set
  end

  result
end

#types_to_add_to_manifestObject



63
64
65
66
67
68
69
70
71
72
73
# File 'foobara-0.0.110/projects/types/src/type/concerns/reflection.rb', line 63

def types_to_add_to_manifest
  remove_sensitive = TypeDeclarations.foobara_manifest_context_remove_sensitive?

  types = [*base_type, *possible_errors.map(&:error_class)]

  if element_type && (!remove_sensitive || !element_type.sensitive?)
    types << element_type
  end

  types
end