Class: Anchor::TypeScript::Resource

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

Defined Under Namespace

Classes: Definition

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Resource

Returns a new instance of Resource.



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

def initialize(klass)
  @klass = klass
end

Instance Method Details

#definitionObject



17
18
19
20
# File 'lib/anchor/type_script/resource.rb', line 17

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

#expressObject



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

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

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



22
23
24
25
26
27
28
# File 'lib/anchor/type_script/resource.rb', line 22

def object(context: {}, include_all_fields:)
  t = Anchor::Inference::JSONAPI::ReadType.infer(@klass, context:, include_all_fields:)
  return t if t["relationships"].type.properties.count > 0
  return t.omit(["relationships"]) unless Anchor.config.empty_relationship_type

  t.overwrite(Anchor::Types::Object.new([empty_relationships_property]))
end