Class: Anchor::TypeScript::Resource

Inherits:
Resource
  • Object
show all
Defined in:
lib/anchor/type_script/resource.rb

Defined Under Namespace

Classes: Definition

Instance Attribute Summary

Attributes inherited from Resource

#resource_klass

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Anchor::Resource

Instance Method Details

#definitionObject


11
12
13
14
# File 'lib/anchor/type_script/resource.rb', line 11

def definition(...)
  @object = object(...)
  Definition.new(name: anchor_schema_name, object: @object)
end

#expressObject


5
6
7
8
9
# File 'lib/anchor/type_script/resource.rb', line 5

def express(...)
  @object = object(...)
  expression = Anchor::TypeScript::Serializer.type_string(@object)
  "export type #{anchor_schema_name} = " + expression + ";"
end

#object(context: {}, include_all_fields:, exclude_fields:) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/anchor/type_script/resource.rb', line 16

def object(context: {}, include_all_fields:, exclude_fields:)
  included_fields = schema_fetchable_fields(context:, include_all_fields:)
  included_fields -= exclude_fields if exclude_fields

  relationships_property = anchor_relationships_property(included_fields:)
  if relationships_property.nil? && Anchor.config.empty_relationship_type
    relationships_property = Anchor::Types::Property.new(:relationships, Anchor.config.empty_relationship_type.call)
  end

  properties = [id_property, type_property] +
    Array.wrap(anchor_attributes_properties(included_fields:)) +
    Array.wrap(relationships_property) +
    [anchor_meta_property].compact + [anchor_links_property].compact

  Anchor::Types::Object.new(properties)
end