module CsvRowModel::Model::Children
Public Instance Methods
append_child(source, options={})
click to toggle source
Appends child to the parent and returns it
@return [Model] return the child if it is valid, otherwise returns nil
# File lib/csv_row_model/model/children.rb, line 17 def append_child(source, options={}) self.class.has_many_relationships.each do |relation_name, child_class| child_row_model = child_class.new(source, options.reverse_merge(parent: self)) if child_row_model.valid? public_send(relation_name) << child_row_model return child_row_model end end nil end
child?()
click to toggle source
@return [Boolean] returns true, if the instance is a child
# File lib/csv_row_model/model/children.rb, line 10 def child? !!parent end
children_public_send(method_name)
click to toggle source
Convenience method to return an array of calling ‘public_send(method_name)` on it’s children
@return [Array] results of ‘public_send(method_name)` in a flattened array
# File lib/csv_row_model/model/children.rb, line 31 def children_public_send(method_name) self.class.has_many_relationships.keys.map do |relation_name| public_send(relation_name).map(&method_name) end.flatten(1) end
deep_public_send(method_name)
click to toggle source
Convenience method to return an array of calling ‘public_send(method_name)` on itself and it’s children
@return [Array] results of ‘public_send(method_name)` in a flattened array
# File lib/csv_row_model/model/children.rb, line 40 def deep_public_send(method_name) result = [public_send(method_name)] result + children_public_send(method_name) end
has_many(relation_name, row_model_class)
click to toggle source
Defines a relationship between a row model (only one relation per model for now).
@param [Symbol] relation_name the name of the relation @param [CsvRowModel::Import] row_model_class class of the relation
# File lib/csv_row_model/model/children.rb, line 50 def has_many(relation_name, row_model_class) raise "for now, CsvRowModel's has_many may only be called once" if @_has_many_relationships.present? relation_name = relation_name.to_sym merge_has_many_relationships(relation_name => row_model_class) define_method(relation_name) do # # equal to: @relation_name ||= [] # variable_name = "@#{relation_name}" instance_variable_get(variable_name) || instance_variable_set(variable_name, []) end end