Class: Interfacets::Shared::GeneratedStore

Inherits:
Object
  • Object
show all
Defined in:
lib/interfacets/shared/generated_store.rb

Defined Under Namespace

Classes: Constructor, Handler, Spec

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ GeneratedStore

Returns a new instance of GeneratedStore.



125
126
127
128
129
# File 'lib/interfacets/shared/generated_store.rb', line 125

def initialize
  @attributes = {
    "internal_entity_id" => SecureRandom.uuid
  }
end

Instance Attribute Details

#attributes ⇒ Object

Returns the value of attribute attributes.



124
125
126
# File 'lib/interfacets/shared/generated_store.rb', line 124

def attributes
  @attributes
end

Class Method Details

.accessor(name) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/interfacets/shared/generated_store.rb', line 85

def accessor(name)
  name = name.to_s

  define_method(name) do
    @attributes[name]
  end

  define_method("#{name}=") do |val|
    @attributes[name] = val
  end
end

.association(name, klass:, type: :reference) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/interfacets/shared/generated_store.rb', line 97

def association(name, klass:, type: :reference)
  name = name.to_s

  associations[name] = Spec.new(
    name:,
    klass:,
    type:,
  )

  define_method(name) do
    @attributes[name]
  end

  define_method("#{name}=") do |val|
    @attributes[name] = val
  end
end

.associations ⇒ Object



119
120
121
# File 'lib/interfacets/shared/generated_store.rb', line 119

def associations
  @associations ||= {}
end

.collection(*args, **params) ⇒ Object



115
116
117
# File 'lib/interfacets/shared/generated_store.rb', line 115

def collection(*args, **params)
  association(*args, **params, type: :collection)
end

.construct ⇒ Object



68
69
70
# File 'lib/interfacets/shared/generated_store.rb', line 68

def construct(...)
  Constructor.call(...)
end

Instance Method Details

#==(other) ⇒ Object



131
132
133
# File 'lib/interfacets/shared/generated_store.rb', line 131

def ==(other)
  attributes == other.attributes
end

#association(name) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/interfacets/shared/generated_store.rb', line 135

def association(name)
  @association_handlers ||= (
    self
      .class
      .associations
      .map { |name, spec| [name, Handler.new(store: self, spec:)] }
      .to_h
  )

  @association_handlers.fetch(name)
end