Module: Foobara::Model::Concerns::Types::ClassMethods

Defined in:
foobara-0.0.125/projects/model/src/concerns/types.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributes_type=(value) ⇒ Object (writeonly)

Sets the attribute attributes_type

Parameters:

  • value

    the value to set the attribute attributes_type to.


11
12
13
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 11

def attributes_type=(value)
  @attributes_type = value
end

#model_typeObject

Returns the value of attribute model_type.


10
11
12
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 10

def model_type
  @model_type
end

Instance Method Details

#attributesObject

[View source]

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 38

def attributes(...)
  private, attributes_type_declaration = extract_private_from_attributes_declaration(...)

  new_type = domain.foobara_type_from_declaration(attributes_type_declaration)
  existing_type = attributes_type

  if existing_type
    declaration = TypeDeclarations::Attributes.merge(
      existing_type.declaration_data,
      new_type.declaration_data
    )

    new_type = domain.foobara_type_from_declaration(declaration)
  end

  self.attributes_type = new_type
  private_attributes(private)

  set_model_type
end

#delegate_attribute(attribute_name, data_path, writer: false) ⇒ Object

[View source]

186
187
188
189
190
191
192
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
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 186

def delegate_attribute(attribute_name, data_path, writer: false)
  if data_path.is_a?(::Symbol) || data_path.is_a?(::String)
    data_path = [data_path, attribute_name]
  end

  data_path = DataPath.for(data_path)

  delegate_manifest = { data_path: data_path.to_s }

  if writer
    delegate_manifest[:writer] = true
  end

  delegates[attribute_name] = delegate_manifest

  delegated_type_declaration = model_type.type_at_path(data_path).reference_or_declaration_data
  attributes(type: :attributes, element_type_declarations: { attribute_name => delegated_type_declaration })

  define_method attribute_name do
    data_path.value_at(self)
  end

  if writer
    define_method "#{attribute_name}=" do |value|
      data_path.set_value_at(self, value)
    end
  else
    method = :"#{attribute_name}="

    if instance_methods.include?(method)
      # TODO: test this code path
      # :nocov:
      remove_method method
      # :nocov:
    end
  end

  set_model_type
end

#delegate_attributes(delegates) ⇒ Object

[View source]

179
180
181
182
183
184
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 179

def delegate_attributes(delegates)
  delegates.each_pair do |attribute_name, delegate_info|
    data_path = DataPath.for(delegate_info[:data_path])
    delegate_attribute(attribute_name, data_path, writer: delegate_info[:writer])
  end
end

#delegatesObject

[View source]

155
156
157
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 155

def delegates
  @delegates ||= {}
end

#foobara_attributes_typeObject Also known as: attributes_type

[View source]

115
116
117
118
119
120
121
122
123
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 115

def foobara_attributes_type
  return @attributes_type if @attributes_type

  @attributes_type = if model_type
                       model_type.element_types
                     elsif ancestors.find { |ancestor| ancestor < Model }
                       superclass.attributes_type
                     end
end

#foobara_typeObject

[View source]

77
78
79
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 77

def foobara_type
  model_type
end

#has_delegated_attributes?Boolean

Returns:

  • (Boolean)
[View source]

159
160
161
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 159

def has_delegated_attributes?
  !delegates.empty?
end

#mutable(*args) ⇒ Object

[View source]

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 13

def mutable(*args)
  args_size = args.size
  case args.size
  when 0
    if defined?(@mutable_override)
      @mutable_override
    else
      type = model_type

      if type
        if type.declaration_data.key?(:mutable)
          type.declaration_data[:mutable]
        end
      end
    end
  when 1
    @mutable_override = args.first
    set_model_type
  else
    # :nocov:
    raise ArgumentError, "Expected 0 or 1 arguments but got #{args_size}"
    # :nocov:
  end
end

#private_attribute(attribute_name) ⇒ Object

[View source]

173
174
175
176
177
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 173

def private_attribute(attribute_name)
  @private_attribute_names = private_attribute_names | [attribute_name]

  set_model_type
end

#private_attribute_namesObject

[View source]

163
164
165
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 163

def private_attribute_names
  @private_attribute_names ||= []
end

#private_attributes(attribute_names) ⇒ Object

[View source]

167
168
169
170
171
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 167

def private_attributes(attribute_names)
  attribute_names.each do |attribute_name|
    private_attribute attribute_name
  end
end

#set_model_typeObject

[View source]

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 59

def set_model_type
  return if abstract?

  if attributes_type
    declaration = type_declaration(attributes_type.declaration_data)

    if model_type
      unless Foobara::TypeDeclarations.declarations_equal?(declaration, model_type.declaration_data)
        type_domain = domain
        self.model_type = nil
        type_domain.foobara_type_from_declaration(declaration)
      end
    else
      domain.foobara_type_from_declaration(declaration)
    end
  end
end

#type_declaration(attributes_declaration) ⇒ Object

[View source]

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'foobara-0.0.125/projects/model/src/concerns/types.rb', line 81

def type_declaration(attributes_declaration)
  if model_type
    model_module_name = model_type.declaration_data[:model_module]
    model_class = model_type.declaration_data[:model_class]
    model_name = model_type.scoped_name
    model_base_class = superclass.name || superclass.model_type.scoped_full_name
  else
    model_base_class = superclass.name || superclass.full_model_name
    model_class = name || full_model_name

    if model_class.start_with?(closest_namespace_module.name)
      model_module_name = closest_namespace_module.name
      model_name = model_class.gsub(/^#{closest_namespace_module.name}::/, "")
    else
      model_module_name = nil
      model_name = model_class
    end
  end

  Util.remove_blank(
    type: :model,
    name: model_name,
    model_module: model_module_name,
    model_class:,
    model_base_class:,
    attributes_declaration:,
    description:,
    _desugarized: { type_absolutified: true },
    mutable:,
    delegates:,
    private: private_attribute_names
  )
end