module AIXM::Association::ClassMethods

Attributes

belongs_to_attributes[R]
has_many_attributes[R]
has_one_attributes[R]

Public Instance Methods

belongs_to(attribute, as: nil, readonly: false) click to toggle source
    # File lib/aixm/association.rb
236 def belongs_to(attribute, as: nil, readonly: false)
237   association = self.to_s.inflect(:demodulize, :tableize, :singularize)
238   inversion = (as || association).to_s
239   (@belongs_to_attributes ||= []) << attribute
240   # feature
241   attr_reader attribute
242   # feature=
243   unless readonly
244     define_method(:"#{attribute}=") do |object|
245       instance_variable_get(:"@#{attribute}")&.send(:"remove_#{inversion}", self)
246       object&.send(:"add_#{inversion}", self)
247     end
248   end
249 end
has_many(attribute, as: nil, accept: nil, &association_block) click to toggle source
    # File lib/aixm/association.rb
173 def has_many(attribute, as: nil, accept: nil, &association_block)
174   association = attribute.to_s.inflect(:singularize)
175   inversion = as || self.to_s.inflect(:demodulize, :tableize, :singularize)
176   class_names = [accept || association].flatten.map { AIXM::CLASSES[_1.to_sym] || _1 }
177   (@has_many_attributes ||= []) << attribute
178   # features
179   define_method(attribute) do
180     instance_eval("@#{attribute} ||= AIXM::Association::Array.new")
181   end
182   # add_feature
183   define_method(:"add_#{association}") do |object=nil, **options, &add_block|
184     unless object
185       fail(ArgumentError, "must pass object to add") if class_names.count > 1
186       object = class_names.first.to_class.new(**options)
187       add_block.call(object) if add_block
188     end
189     instance_exec(object, **options, &association_block) if association_block
190     fail(ArgumentError, "#{object.__class__} not allowed") unless class_names.reduce(false){ |m, c| m || object.is_a?(c.to_class) }
191     send(attribute).send(:push, object)
192     object.instance_variable_set(:"@#{inversion}", self)
193     self
194   end
195   # add_features
196   define_method(:"add_#{attribute}") do |objects=[], **options, &add_block|
197     objects.each { send(:"add_#{association}", _1, **options, &add_block) }
198     self
199   end
200   # remove_feature
201   define_method(:"remove_#{association}") do |object|
202     send(attribute).send(:delete, object)
203     object.instance_variable_set(:"@#{inversion}", nil)
204     self
205   end
206   # remove_features
207   define_method(:"remove_#{attribute}") do |objects=[]|
208     objects.each { send(:"remove_#{association}", _1) }
209     self
210   end
211 end
has_one(attribute, as: nil, accept: nil, allow_nil: false) click to toggle source
    # File lib/aixm/association.rb
213 def has_one(attribute, as: nil, accept: nil, allow_nil: false)
214   association = attribute.to_s
215   inversion = (as || self.to_s.inflect(:demodulize, :tableize, :singularize)).to_s
216   class_names = [accept || association].flatten.map { AIXM::CLASSES[_1.to_sym] || _1 }
217   class_names << 'NilClass' if allow_nil
218   (@has_one_attributes ||= []) << attribute
219   # feature
220   attr_reader attribute
221   # feature= / add_feature
222   define_method(:"#{association}=") do |object|
223     fail(ArgumentError, "#{object.__class__} not allowed") unless class_names.reduce(false){ |m, c| m || object.is_a?(c.to_class) }
224     instance_variable_get(:"@#{attribute}")&.instance_variable_set(:"@#{inversion}", nil)
225     instance_variable_set(:"@#{attribute}", object)
226     object&.instance_variable_set(:"@#{inversion}", self)
227   end
228   alias_method(:"add_#{association}", :"#{association}=")
229   # remove_feature
230   define_method(:"remove_#{association}") do |_|
231     send(:"#{association}=", nil)
232     self
233   end
234 end