Class: Interfacets::Shared::Entity

Inherits:
Object
  • Object
show all
Includes:
EntityDsl, Validations
Defined in:
lib/interfacets/shared/entity.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

#append_features, #class_methods, extended, #included, #prepend_features, #prepended

Constructor Details

#initialize(store:, nesting:, parent:) ⇒ Entity

Returns a new instance of Entity.



36
37
38
39
40
# File 'lib/interfacets/shared/entity.rb', line 36

def initialize(store:, nesting:, parent:)
  @store = store
  @parent = parent
  @nesting = nesting
end

Instance Attribute Details

#parent ⇒ Object (readonly)

Returns the value of attribute parent.



35
36
37
# File 'lib/interfacets/shared/entity.rb', line 35

def parent
  @parent
end

#store ⇒ Object (readonly)

Returns the value of attribute store.



35
36
37
# File 'lib/interfacets/shared/entity.rb', line 35

def store
  @store
end

Class Method Details

.inherited(subclass) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/interfacets/shared/entity.rb', line 10

def inherited(subclass)
  subclass.inherit_attributes(self)

  subclass.action(:after_load, accepted_by: :client)

  subclass.accessor(
    :internal_entity_id,
    getter: -> {
      if store.respond_to?(:internal_entity_id)
        store.internal_entity_id
      else
        (@interfacets_id ||= SecureRandom.uuid)
      end
    },
    setter: ->(val) {
      if store.respond_to?(:internal_entity_id=)
        store.internal_entity_id = val
      else
        @interfacets_id = val
      end
    }
  )
end

Instance Method Details

#==(other) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/interfacets/shared/entity.rb', line 46

def ==(other)
  if other.is_a?(Entity)
    store == other.store
  else
    store == other
  end
end

#association(name) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/interfacets/shared/entity.rb', line 88

def association(name)
  # inlined to avoid polluting API
  @association_handlers ||= (
    self
      .class
      .associations
      .map { |name, spec| [name, spec.handler(self) ] }
      .to_h
  )

  @association_handlers.fetch(name.to_s)
end

#entity_at(nesting) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/interfacets/shared/entity.rb', line 70

def entity_at(nesting)
  # ignore first entry, cause it's self
  real_nesting = nesting[1..-1]

  if real_nesting.count == 0
    self
  else
    assoc, internal_entity_id = real_nesting[0]

    if association(assoc).collection?
      association(assoc).get.find { _1.internal_entity_id == internal_entity_id }
    else
      value = association(assoc).get
      value.internal_entity_id == internal_entity_id ? value : nil
    end
  end
end

#entity_nesting ⇒ Object



66
67
68
# File 'lib/interfacets/shared/entity.rb', line 66

def entity_nesting
  @entity_nesting ||= (parent&.entity_nesting || []) + [[@nesting, internal_entity_id]]
end

#record ⇒ Object



42
43
44
# File 'lib/interfacets/shared/entity.rb', line 42

def record
  store
end

#role ⇒ Object



54
55
56
57
58
59
60
# File 'lib/interfacets/shared/entity.rb', line 54

def role
  if parent.nil?
    self.class.role
  else
    parent.role
  end
end

#uid ⇒ Object



62
63
64
# File 'lib/interfacets/shared/entity.rb', line 62

def uid
  [self.class.name, internal_entity_id].join("__")
end