Module: CsvRowModel::Model::Children

Extended by:
ActiveSupport::Concern
Included in:
CsvRowModel::Model
Defined in:
lib/csv_row_model/model/children.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.has_many(relation_name, row_model_class) ⇒ Object (protected)

Defines a relationship between a row model (only one relation per model for now).

Parameters:

  • relation_name (Symbol)

    the name of the relation

  • row_model_class (CsvRowModel::Import)

    class of the relation



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/csv_row_model/model/children.rb', line 61

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

.has_many_relationshipsHash

Won't work for Export right now

Returns:

  • (Hash)

    map of relation_name => CsvRowModel::Import or CsvRowModel::Export class



46
47
48
# File 'lib/csv_row_model/model/children.rb', line 46

def has_many_relationships
  inherited_class_var :@_has_many_relationships, {}, :merge
end

.merge_has_many_relationships(relation_hash) ⇒ Object (protected)



51
52
53
54
55
# File 'lib/csv_row_model/model/children.rb', line 51

def merge_has_many_relationships(relation_hash)
  @_has_many_relationships ||= {}
  deep_clear_class_cache(:@_has_many_relationships)
  @_has_many_relationships.merge! relation_hash
end

Instance Method Details

#append_child(source, options = {}) ⇒ Model

Appends child to the parent and returns it

Returns:

  • (Model)

    return the child if it is valid, otherwise returns nil



14
15
16
17
18
19
20
21
22
23
# File 'lib/csv_row_model/model/children.rb', line 14

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?Boolean

Returns true, if the instance is a child

Returns:

  • (Boolean)

    returns true, if the instance is a child



7
8
9
# File 'lib/csv_row_model/model/children.rb', line 7

def child?
  !!parent
end

#children_public_send(method_name) ⇒ Array

Convenience method to return an array of calling public_send(method_name) on it's children

Returns:

  • (Array)

    results of public_send(method_name) in a flattened array



28
29
30
31
32
# File 'lib/csv_row_model/model/children.rb', line 28

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) ⇒ Array

Convenience method to return an array of calling public_send(method_name) on itself and it's children

Returns:

  • (Array)

    results of public_send(method_name) in a flattened array



37
38
39
40
# File 'lib/csv_row_model/model/children.rb', line 37

def deep_public_send(method_name)
  result = [public_send(method_name)]
  result + children_public_send(method_name)
end