Class: Tapioca::Compilers::DryStruct::DryAstCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/tapioca/dsl/compilers/dry_struct.rb

Overview

Convert Dry AST to type informations

“‘ compiler = DryAstCompiler.new DryStructClass.schema.each do |s|

attribute_info = compiler.visit(s.to_ast)

# => { name: 'attr_name01', required: true, type: String }
# => { name: 'attr_name02', required: true, type: Integer }
# => { name: 'attr_name03', required: true, type: [String] }
# => { name: 'attr_name04', required: true, type: [[String]] }
# => { name: 'attr_name05', required: true, type: Hash }
# => { name: 'attr_name06', required: true, type: Date }
# => { name: 'attr_name07', required: true, type: DateTime }
# => { name: 'attr_name08', required: true, type: Time }
# => { name: 'attr_name09', required: true, type: Set }
# => { name: 'attr_name10', required: true, type: Range }
# => { name: 'attr_name11', required: true, type: YourKlass }
# => { name: 'attr_name12', required: true, type: #<DryAstCompiler::Undefined> }
# => { name: 'attr_name13', required: true, type: #<DryAstCompiler::Sum> }
# => { name: 'attr_name14', required: true, type: #<DryAstCompiler::Map> }
# => { name: 'attr_name15', required: true, type: #<DryAstCompiler::Schema> }

do “‘

Defined Under Namespace

Classes: Map, Schema, Sum, Undefined

Instance Method Summary collapse

Instance Method Details

#visit(node) ⇒ Object



132
133
134
135
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 132

def visit(node)
  meth, rest = node
  public_send(:"visit_#{meth}", rest)
end

#visit_and(node) ⇒ Object



165
166
167
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 165

def visit_and(node)
  # NOP
end

#visit_any(_node) ⇒ Object



204
205
206
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 204

def visit_any(_node)
  Undefined.new
end

#visit_array(node) ⇒ Object



169
170
171
172
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 169

def visit_array(node)
  type = visit(node[0])
  [type]
end

#visit_constrained(node) ⇒ Object



174
175
176
177
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 174

def visit_constrained(node)
  types = node.map { |r| visit(r) }.reject(&:nil?)
  types.size == 1 ? types[0] : types
end

#visit_constructor(node) ⇒ Object



146
147
148
149
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 146

def visit_constructor(node)
  a, _ = node
  visit(a)
end

#visit_enum(node) ⇒ Object



200
201
202
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 200

def visit_enum(node)
  visit(node[0][1][0])
end

#visit_hash(_node) ⇒ Object



192
193
194
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 192

def visit_hash(_node)
  ::Hash
end

#visit_key(node) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 137

def visit_key(node)
  name, required, rest = node
  {
    name: name,
    required: required,
    type: visit(rest)
  }
end

#visit_map(node) ⇒ Object



196
197
198
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 196

def visit_map(node)
  Map.new(visit(node[0]), visit(node[1]))
end

#visit_nominal(node) ⇒ Object



179
180
181
182
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 179

def visit_nominal(node)
  type, _option = node
  type
end

#visit_predicate(_node) ⇒ Object



184
185
186
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 184

def visit_predicate(_node)
  # NOP
end

#visit_schema(node) ⇒ Object



188
189
190
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 188

def visit_schema(node)
  Schema.new(node[0].map { |n| visit(n) })
end

#visit_struct(node) ⇒ Object



151
152
153
154
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 151

def visit_struct(node)
  type, _ = node
  type
end

#visit_sum(node) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/tapioca/dsl/compilers/dry_struct.rb', line 156

def visit_sum(node)
  type = Sum.new
  node.each do |n|
    next if n.is_a?(::Hash)
    type << visit(n)
  end
  type
end