Class: Foobara::Model

Inherits:
Object
  • Object
show all
Includes:
Concerns::Aliases, Concerns::Classes, Concerns::Reflection, Concerns::Types
Defined in:
foobara-0.7.3/projects/typesystem/projects/model/src/model.rb,
foobara-0.7.3/projects/typesystem/projects/model/lib/foobara/model.rb,
foobara-0.7.3/projects/typesystem/projects/model/src/concerns/types.rb,
foobara-0.7.3/projects/typesystem/projects/model/src/concerns/aliases.rb,
foobara-0.7.3/projects/typesystem/projects/model/src/concerns/classes.rb,
foobara-0.7.3/projects/typesystem/projects/model/src/concerns/reflection.rb,
foobara-0.7.3/projects/typesystem/projects/model/src/sensitive_type_removers/model.rb,
foobara-0.7.3/projects/typesystem/projects/model/src/sensitive_value_removers/model.rb,
foobara-0.7.3/projects/typesystem/projects/model/src/sensitive_type_removers/extended_model.rb

Defined Under Namespace

Modules: Concerns, SensitiveTypeRemovers, SensitiveValueRemovers Classes: AttributeIsImmutableError, NoSuchAttributeError

Constant Summary collapse

ALLOWED_OPTIONS =
[:validate, :mutable, :ignore_unexpected_attributes, :skip_validations].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from Concerns::Types

#attributes_type

Constructor Details

#initialize(attributes = nil, options = {}) ⇒ Model

Returns a new instance of Model.



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'projects/typesystem/projects/model/src/model.rb', line 193

def initialize(attributes = nil, options = {})
  invalid_options = options.keys - ALLOWED_OPTIONS

  unless invalid_options.empty?
    # simplecov:disable
    raise ArgumentError, "Invalid options #{invalid_options} expected only #{ALLOWED_OPTIONS}"
    # simplecov:enable
  end

  self.skip_validations = options[:skip_validations]

  if options[:ignore_unexpected_attributes]
    Thread.with_inheritable_thread_local_var(:foobara_ignore_unexpected_attributes, true) do
      initialize(attributes, options.except(:ignore_unexpected_attributes))
      return
    end
  end

  if Thread.inheritable_thread_local_var_get(:foobara_ignore_unexpected_attributes)
    @ignore_unexpected_attributes = true
  end

  validate = options[:validate]

  if attributes.nil?
    if validate
      # simplecov:disable
      raise ArgumentError, "Cannot use validate option without attributes"
      # simplecov:enable
    end
  else
    if Thread.inheritable_thread_local_var_get(:foobara_ignore_unexpected_attributes)
      outcome = attributes_type.process_value(attributes)

      if outcome.success?
        attributes = outcome.result
      end
    end

    self.mutable = true
    attributes.each_pair do |attribute_name, value|
      write_attribute(attribute_name, value)
    end
  end

  mutable = if options.key?(:mutable)
              options[:mutable]
            elsif self.class.model_type.declaration_data.key?(:mutable)
              self.class.model_type.declaration_data[:mutable]
            else
              # why do we default to true here but false in the transformers?
              true
            end

  self.mutable = if mutable.is_a?(::Array)
                   mutable.map(&:to_sym)
                 else
                   mutable
                 end

  validate! if validate # TODO: test this code path
end

Class Attribute Details

.is_abstract ⇒ Object

Returns the value of attribute is_abstract.



14
15
16
# File 'projects/typesystem/projects/model/src/model.rb', line 14

def is_abstract
  @is_abstract
end

Instance Attribute Details

#mutable ⇒ Object

Returns the value of attribute mutable.



189
190
191
# File 'projects/typesystem/projects/model/src/model.rb', line 189

def mutable
  @mutable
end

#skip_validations ⇒ Object

Returns the value of attribute skip_validations.



189
190
191
# File 'projects/typesystem/projects/model/src/model.rb', line 189

def skip_validations
  @skip_validations
end

Class Method Details

.abstract ⇒ Object

TODO: would be nice to make this a universal concept via a concern



38
39
40
# File 'projects/typesystem/projects/model/src/model.rb', line 38

def abstract
  @is_abstract = true
end

.abstract? ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'projects/typesystem/projects/model/src/model.rb', line 42

def abstract?
  @is_abstract
end

.attribute_names ⇒ Object



96
97
98
# File 'projects/typesystem/projects/model/src/model.rb', line 96

def attribute_names
  attributes_type.element_types.keys
end

.closest_namespace_module ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'projects/typesystem/projects/model/src/model.rb', line 46

def closest_namespace_module
  # TODO: Feels like we should use the autoset_namespace helpers here
  mod = Util.module_for(self)

  while mod
    if mod.is_a?(Namespace::IsNamespace)
      namespace = mod
      break
    end

    mod = Util.module_for(mod)
  end

  if mod.nil? || mod == GlobalOrganization || mod == Foobara
    GlobalDomain
  else
    namespace
  end
end

.description(*args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'projects/typesystem/projects/model/src/model.rb', line 16

def description(*args)
  case args.size
  when 0
    @description
  when 1
    @description = args[0]
  else
    # simplecov:disable
    raise ArgumentError, "expected 0 or 1 argument, got #{args.size}"
    # simplecov:enable
  end
end

.domain ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'projects/typesystem/projects/model/src/model.rb', line 66

def domain
  if model_type
    domain = model_type.foobara_domain

    if domain == GlobalDomain
      module_name = model_type.declaration_data[:model_module]

      begin
        Domain.to_domain(module_name)
      rescue Domain::NoSuchDomain
        module_to_check = module_name

        loop do
          domain = if Object.const_defined?(module_to_check)
                     return Domain.domain_through_modules(Object.const_get(module_to_check))
                   elsif module_to_check.include?("::")
                     module_to_check = module_to_check.split("::")[..-2].join("::")
                   else
                     return GlobalDomain
                   end
        end
      end
    else
      domain
    end
  else
    Domain.domain_through_modules(self)
  end
end

.domain_name ⇒ Object



33
34
35
# File 'projects/typesystem/projects/model/src/model.rb', line 33

def domain_name
  domain.foobara_domain_name
end

.fire_reregistered!(model_class) ⇒ Object



180
181
182
183
184
# File 'projects/typesystem/projects/model/src/model.rb', line 180

def fire_reregistered!(model_class)
  @on_reregister&.each do |block|
    block.call(model_class)
  end
end

.foobara_model_name ⇒ Object



110
111
112
113
114
115
116
# File 'projects/typesystem/projects/model/src/model.rb', line 110

def foobara_model_name
  if foobara_type&.scoped_path_set?
    foobara_type.scoped_name
  else
    Util.non_full_name(self) || model_name&.split("::")&.[](-1)
  end
end

.foobara_name ⇒ Object



118
119
120
# File 'projects/typesystem/projects/model/src/model.rb', line 118

def foobara_name
  foobara_model_name
end

.full_model_name ⇒ Object



122
123
124
# File 'projects/typesystem/projects/model/src/model.rb', line 122

def full_model_name
  [*model_type&.scoped_full_name, model_name].max_by(&:size)
end

.handle_reregistered_types!(seen) ⇒ Object



166
167
168
169
170
171
172
# File 'projects/typesystem/projects/model/src/model.rb', line 166

def handle_reregistered_types!(seen)
  seen << self

  if instance_variable_defined?(:@attributes_type)
    remove_instance_variable(:@attributes_type)
  end
end

.install! ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'projects/typesystem/projects/model/lib/foobara/model.rb', line 9

def install!
  model_handler = TypeDeclarations::Handlers::ExtendModelTypeDeclaration.new
  TypeDeclarations.register_type_declaration(model_handler)
  extended_model_handler = TypeDeclarations::Handlers::ExtendRegisteredModelTypeDeclaration.new
  TypeDeclarations.register_type_declaration(extended_model_handler)

  TypeDeclarations.register_sensitive_type_remover(SensitiveTypeRemovers::Model.new(model_handler))
  TypeDeclarations.register_sensitive_value_remover(model_handler, SensitiveValueRemovers::Model)
  TypeDeclarations.register_sensitive_type_remover(
    SensitiveTypeRemovers::ExtendedModel.new(extended_model_handler)
  )
  # TypeDeclarations.register_sensitive_value_remover(
  #   extended_model_handler,
  #   SensitiveValueRemovers::ExtendedModel
  # )

  atomic_duck = Namespace.global.foobara_lookup_type!(:atomic_duck)
  BuiltinTypes.build_and_register!(:model, atomic_duck, nil)
  # address = build_and_register!(:address, model)
  # us_address = build_and_register!(:us_address, model)
end

.on_reregister(&block) ⇒ Object

Why aren't we using the callbacks project for this?



175
176
177
178
# File 'projects/typesystem/projects/model/src/model.rb', line 175

def on_reregister(&block)
  @on_reregister ||= []
  @on_reregister << block
end

.organization_name ⇒ Object



29
30
31
# File 'projects/typesystem/projects/model/src/model.rb', line 29

def organization_name
  domain.foobara_organization_name
end

.possible_errors(mutable: true) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'projects/typesystem/projects/model/src/model.rb', line 126

def possible_errors(mutable: true)
  if mutable == true
    attributes_type.possible_errors
  elsif mutable
    element_types = attributes_type.element_types

    p = []

    Util.array(mutable).each do |attribute_name|
      attribute_name = attribute_name.to_sym

      # TODO: this doesn't feel quite right... we should be excluding errors so that we don't
      # miss any that are on attributes_type unrelated to the elements.
      element_types[attribute_name].possible_errors.each do |possible_error|
        possible_error = possible_error.dup
        possible_error.prepend_path!(attribute_name)
        p << possible_error
      end
    end

    p
  else
    # Hmmm, can't there still be errors even if it's immutable?
    []
  end
end

.reset_all ⇒ Object



31
32
33
34
# File 'projects/typesystem/projects/model/lib/foobara/model.rb', line 31

def reset_all
  Foobara.raise_if_production!
  install!
end

.subclass(name:) ⇒ Object

will create an anonymous subclass TODO: change to a normal parameter since it's just name



155
156
157
158
159
160
161
162
163
164
# File 'projects/typesystem/projects/model/src/model.rb', line 155

def subclass(name:)
  name = name.to_s if name.is_a?(::Symbol)

  # TODO: How are we going to set the domain and organization?
  Class.new(self) do
    singleton_class.define_method :model_name do
      name
    end
  end
end

.valid_attribute_name?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
# File 'projects/typesystem/projects/model/src/model.rb', line 100

def valid_attribute_name?(attribute_name)
  attribute_names.include?(attribute_name.to_sym)
end

.validate_attribute_name!(attribute_name) ⇒ Object



104
105
106
107
108
# File 'projects/typesystem/projects/model/src/model.rb', line 104

def validate_attribute_name!(attribute_name)
  unless valid_attribute_name?(attribute_name)
    raise NoSuchAttributeError, "No such attribute #{attribute_name} expected one of #{attribute_names}"
  end
end

Instance Method Details

#==(other) ⇒ Object



366
367
368
# File 'projects/typesystem/projects/model/src/model.rb', line 366

def ==(other)
  self.class == other.class && attributes == other.attributes
end

#attributes ⇒ Object



272
273
274
# File 'projects/typesystem/projects/model/src/model.rb', line 272

def attributes
  @attributes ||= {}
end

#attributes_with_delegates ⇒ Object



276
277
278
279
280
281
282
# File 'projects/typesystem/projects/model/src/model.rb', line 276

def attributes_with_delegates
  h = self.class.delegates.keys.to_h do |delegated_attribute_name|
    [delegated_attribute_name, __send__(delegated_attribute_name)]
  end

  attributes.merge(h)
end

#cast_attribute(attribute_name, value) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'projects/typesystem/projects/model/src/model.rb', line 323

def cast_attribute(attribute_name, value)
  attribute_type = attributes_type.element_types[attribute_name]

  return Outcome.success(value) unless attribute_type

  attribute_type.process_value(value).tap do |outcome|
    unless outcome.success?
      outcome.errors.each do |error|
        error.prepend_path!(attribute_name)
      end
    end
  end
end

#cast_attribute!(attribute_name, value) ⇒ Object



337
338
339
340
341
342
343
# File 'projects/typesystem/projects/model/src/model.rb', line 337

def cast_attribute!(attribute_name, value)
  validate_attribute_name!(attribute_name)

  outcome = cast_attribute(attribute_name, value)
  outcome.raise!
  outcome.result
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


370
371
372
# File 'projects/typesystem/projects/model/src/model.rb', line 370

def eql?(other)
  self == other
end

#hash ⇒ Object



374
375
376
# File 'projects/typesystem/projects/model/src/model.rb', line 374

def hash
  attributes.hash
end

#ignore_unexpected_attributes? ⇒ Boolean

Returns:

  • (Boolean)


256
# File 'projects/typesystem/projects/model/src/model.rb', line 256

def ignore_unexpected_attributes? = @ignore_unexpected_attributes

#model_name ⇒ Object



258
259
260
# File 'projects/typesystem/projects/model/src/model.rb', line 258

def model_name
  self.class.model_name
end

#read_attribute(attribute_name) ⇒ Object



314
315
316
# File 'projects/typesystem/projects/model/src/model.rb', line 314

def read_attribute(attribute_name)
  attributes[attribute_name&.to_sym]
end

#read_attribute!(attribute_name) ⇒ Object



318
319
320
321
# File 'projects/typesystem/projects/model/src/model.rb', line 318

def read_attribute!(attribute_name)
  validate_attribute_name!(attribute_name)
  read_attribute(attribute_name)
end

#to_h ⇒ Object



378
379
380
# File 'projects/typesystem/projects/model/src/model.rb', line 378

def to_h
  attributes
end

#to_json(*_args) ⇒ Object



382
383
384
# File 'projects/typesystem/projects/model/src/model.rb', line 382

def to_json(*_args)
  to_h.to_json
end

#valid? ⇒ Boolean

Returns:

  • (Boolean)


345
346
347
# File 'projects/typesystem/projects/model/src/model.rb', line 345

def valid?
  attributes_type.process_value(attributes).success?
end

#valid_attribute_name?(attribute_name) ⇒ Boolean

Returns:

  • (Boolean)


262
263
264
265
266
# File 'projects/typesystem/projects/model/src/model.rb', line 262

def valid_attribute_name?(attribute_name)
  # simplecov:disable
  self.class.valid_attribute_name?(attribute_name)
  # simplecov:enable
end

#validate! ⇒ Object



362
363
364
# File 'projects/typesystem/projects/model/src/model.rb', line 362

def validate!
  attributes_type.process_value!(attributes)
end

#validate_attribute_name!(attribute_name) ⇒ Object



268
269
270
# File 'projects/typesystem/projects/model/src/model.rb', line 268

def validate_attribute_name!(attribute_name)
  self.class.validate_attribute_name!(attribute_name)
end

#validation_errors ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
# File 'projects/typesystem/projects/model/src/model.rb', line 349

def validation_errors
  errors = attributes_type.process_value(attributes).error_collection

  unexpected_attributes_class =
    BuiltinTypes::Attributes::SupportedProcessors::ElementTypeDeclarations::UnexpectedAttributesError

  if ignore_unexpected_attributes?
    errors.reject { unexpected_attributes_class === it }
  else
    errors
  end
end

#write_attribute(attribute_name, value) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
# File 'projects/typesystem/projects/model/src/model.rb', line 284

def write_attribute(attribute_name, value)
  attribute_name = attribute_name.to_sym

  if mutable == true || (mutable != false && mutable&.include?(attribute_name))
    outcome = cast_attribute(attribute_name, value)
    attributes[attribute_name] = outcome.success? ? outcome.result : value
  else
    # simplecov:disable
    raise AttributeIsImmutableError, "Cannot write attribute #{attribute_name} because it is not mutable"
    # simplecov:enable
  end
end

#write_attribute!(attribute_name, value) ⇒ Object



297
298
299
300
# File 'projects/typesystem/projects/model/src/model.rb', line 297

def write_attribute!(attribute_name, value)
  attribute_name = attribute_name.to_sym
  attributes[attribute_name] = cast_attribute!(attribute_name, value)
end

#write_attributes(attributes) ⇒ Object



302
303
304
305
306
# File 'projects/typesystem/projects/model/src/model.rb', line 302

def write_attributes(attributes)
  attributes.each_pair do |attribute_name, value|
    write_attribute(attribute_name, value)
  end
end

#write_attributes!(attributes) ⇒ Object



308
309
310
311
312
# File 'projects/typesystem/projects/model/src/model.rb', line 308

def write_attributes!(attributes)
  attributes.each_pair do |attribute_name, value|
    write_attribute!(attribute_name, value)
  end
end