Module: AIXM::Concerns::Association::ClassMethods

Defined in:
lib/aixm/concerns/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#belongs_to_attributesObject (readonly)

Returns the value of attribute belongs_to_attributes.



174
175
176
# File 'lib/aixm/concerns/association.rb', line 174

def belongs_to_attributes
  @belongs_to_attributes
end

#has_many_attributesObject (readonly)

Returns the value of attribute has_many_attributes.



174
175
176
# File 'lib/aixm/concerns/association.rb', line 174

def has_many_attributes
  @has_many_attributes
end

#has_one_attributesObject (readonly)

Returns the value of attribute has_one_attributes.



174
175
176
# File 'lib/aixm/concerns/association.rb', line 174

def has_one_attributes
  @has_one_attributes
end

Instance Method Details

#belongs_to(attribute, as: nil, readonly: false) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/aixm/concerns/association.rb', line 245

def belongs_to(attribute, as: nil, readonly: false)
  association = self.to_s.inflect(:demodulize, :tableize, :singularize)
  inversion = (as || association).to_s
  (@belongs_to_attributes ||= []) << attribute
  # feature
  attr_reader attribute
  unless readonly
    # feature=
    define_method(:"#{attribute}=") do |object|
      instance_variable_get(:"@#{attribute}")&.send(:"remove_#{inversion}", self)
      object&.send(:"add_#{inversion}", self)
      object
    end
    # add_feature
    define_method(:"add_#{attribute}") do |object|
      send("#{attribute}=", object)
      self
    end
  end
end

#has_many(attribute, as: nil, accept: nil, &association_block) ⇒ Object



176
177
178
179
180
181
182
183
184
185
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
# File 'lib/aixm/concerns/association.rb', line 176

def has_many(attribute, as: nil, accept: nil, &association_block)
  association = attribute.to_s.inflect(:singularize)
  inversion = as || self.to_s.inflect(:demodulize, :tableize, :singularize)
  class_names = [accept || association].flatten.map { AIXM::CLASSES[_1.to_sym] || _1 }
  (@has_many_attributes ||= []) << attribute
  # features
  define_method(attribute) do
    instance_variable_get(:"@#{attribute}") || AIXM::Concerns::Association::Array.new
  end
  # add_feature
  define_method(:"add_#{association}") do |object=nil, **options, &add_block|
    unless object
      fail(ArgumentError, "must pass object to add") if class_names.count > 1
      object = class_names.first.to_class.new(**options)
      add_block.call(object) if add_block
    end
    instance_exec(object, **options, &association_block) if association_block
    fail(ArgumentError, "#{object.__class__} not allowed") unless class_names.any? { |c| object.is_a?(c.to_class) }
    instance_eval("@#{attribute} ||= AIXM::Concerns::Association::Array.new")
    send(attribute).send(:push, object)
    object.instance_variable_set(:"@#{inversion}", self)
    self
  end
  # add_features
  define_method(:"add_#{attribute}") do |objects=[], **options, &add_block|
    objects.each { send(:"add_#{association}", _1, **options, &add_block) }
    self
  end
  # remove_feature
  define_method(:"remove_#{association}") do |object|
    send(attribute).send(:delete, object)
    object.instance_variable_set(:"@#{inversion}", nil)
    self
  end
  # remove_features
  define_method(:"remove_#{attribute}") do |objects=[]|
    objects.each { send(:"remove_#{association}", _1) }
    self
  end
end

#has_one(attribute, as: nil, accept: nil, allow_nil: false) ⇒ Object



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
# File 'lib/aixm/concerns/association.rb', line 217

def has_one(attribute, as: nil, accept: nil, allow_nil: false)
  association = attribute.to_s
  inversion = (as || self.to_s.inflect(:demodulize, :tableize, :singularize)).to_s
  class_names = [accept || association].flatten.map { AIXM::CLASSES[_1.to_sym] || _1 }
  class_names << 'NilClass' if allow_nil
  (@has_one_attributes ||= []) << attribute
  # feature
  attr_reader attribute
  # feature=
  define_method(:"#{association}=") do |object|
    fail(ArgumentError, "#{object.__class__} not allowed") unless class_names.any? { |c| object.is_a?(c.to_class) }
    instance_variable_get(:"@#{attribute}")&.instance_variable_set(:"@#{inversion}", nil)
    instance_variable_set(:"@#{attribute}", object)
    object&.instance_variable_set(:"@#{inversion}", self)
    object
  end
  # add_feature
  define_method(:"add_#{association}") do |object|
    send("#{association}=", object)
    self
  end
  # remove_feature
  define_method(:"remove_#{association}") do |_|
    send(:"#{association}=", nil)
    self
  end
end