Module: AIXM::Association::ClassMethods

Defined in:
lib/aixm/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


171
172
173
# File 'lib/aixm/association.rb', line 171

def belongs_to_attributes
  @belongs_to_attributes
end

#has_many_attributesObject (readonly)

Returns the value of attribute has_many_attributes


171
172
173
# File 'lib/aixm/association.rb', line 171

def has_many_attributes
  @has_many_attributes
end

#has_one_attributesObject (readonly)

Returns the value of attribute has_one_attributes


171
172
173
# File 'lib/aixm/association.rb', line 171

def has_one_attributes
  @has_one_attributes
end

Instance Method Details

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


236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/aixm/association.rb', line 236

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
  # feature=
  unless readonly
    define_method(:"#{attribute}=") do |object|
      instance_variable_get(:"@#{attribute}")&.send(:"remove_#{inversion}", self)
      object&.send(:"add_#{inversion}", self)
    end
  end
end

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


173
174
175
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
# File 'lib/aixm/association.rb', line 173

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_eval("@#{attribute} ||= AIXM::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.reduce(false){ |m, c| m || object.is_a?(c.to_class) }
    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


213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/aixm/association.rb', line 213

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= / add_feature
  define_method(:"#{association}=") do |object|
    fail(ArgumentError, "#{object.__class__} not allowed") unless class_names.reduce(false){ |m, c| m || 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)
  end
  alias_method(:"add_#{association}", :"#{association}=")
  # remove_feature
  define_method(:"remove_#{association}") do |_|
    send(:"#{association}=", nil)
    self
  end
end