Class: ZeroRailsAdapter::Relationship

Inherits:
Object
  • Object
show all
Defined in:
lib/zero_rails_adapter/relationship.rb

Defined Under Namespace

Classes: Hop

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, name:, kind:, hops:, published_schema:) ⇒ Relationship

Returns a new instance of Relationship.



39
40
41
42
43
44
45
46
47
# File 'lib/zero_rails_adapter/relationship.rb', line 39

def initialize(source:, name:, kind:, hops:, published_schema:)
  @source = source
  @name = name.to_s
  @kind = kind.to_s
  @published_schema = published_schema

  validate_header!
  @hops = validate_hops!(hops).freeze
end

Instance Attribute Details

#hopsObject (readonly)

Returns the value of attribute hops.



12
13
14
# File 'lib/zero_rails_adapter/relationship.rb', line 12

def hops
  @hops
end

#kindObject (readonly)

Returns the value of attribute kind.



12
13
14
# File 'lib/zero_rails_adapter/relationship.rb', line 12

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/zero_rails_adapter/relationship.rb', line 12

def name
  @name
end

#sourceObject (readonly)

Returns the value of attribute source.



12
13
14
# File 'lib/zero_rails_adapter/relationship.rb', line 12

def source
  @source
end

Class Method Details

.from_definition(definition, published_schema:) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/zero_rails_adapter/relationship.rb', line 14

def self.from_definition(definition, published_schema:)
  unless definition.respond_to?(:to_h)
    raise InvalidRelationshipError, "Relationship definition must be an object"
  end

  attributes = definition.to_h.symbolize_keys
  hop_definitions = if attributes.key?(:through)
    Array(attributes[:through])
  else
    [{
      source_fields: attributes[:source_fields],
      destination: attributes[:destination],
      destination_fields: attributes[:destination_fields]
    }]
  end

  new(
    source: attributes[:source],
    name: attributes[:name],
    kind: attributes[:kind],
    hops: hop_definitions,
    published_schema:
  )
end