Module: Card::Subcards::Add

Includes:
Args
Included in:
Card::Subcards
Defined in:
lib/card/subcards/add.rb

Overview

Methods for adding subcards

Instance Method Summary collapse

Methods included from Args

#extract_subcard_args!

Instance Method Details

#<<(value) ⇒ Object



19
20
21
# File 'lib/card/subcards/add.rb', line 19

def << value
  add value
end

#[]=(name, card_or_attr) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/card/subcards/add.rb', line 23

def []= name, card_or_attr
  case card_or_attr
  when Hash
    new_by_attributes name, card_or_attr
  when Card
    new_by_card card_or_attr
  end
end

#add(*args) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/card/subcards/add.rb', line 32

def add *args
  case args.first
  when Card then new_by_card args.first
  when Hash then add_hash args.first
  else new_by_attributes(*args)
  end
end

#add_child(name, args) ⇒ Object Also known as: add_field



48
49
50
51
# File 'lib/card/subcards/add.rb', line 48

def add_child name, args
  name = name.is_a?(Symbol) ? name.cardname : name.to_name
  add name.prepend_joint, args
end

#add_hash(hash) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/card/subcards/add.rb', line 40

def add_hash hash
  if (name = hash.delete :name)
    new_by_attributes name, hash
  else
    multi_add hash
  end
end

#initialize_by_attributes(name, attributes) ⇒ Object



73
74
75
76
# File 'lib/card/subcards/add.rb', line 73

def initialize_by_attributes name, attributes
  attributes[:supercard] ||= @context_card
  Card.assign_or_newish name, attributes, local_only: true
end

#new_by_attributes(name, attributes = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/card/subcards/add.rb', line 63

def new_by_attributes name, attributes={}
  attributes = attributes&.symbolize_keys || {}
  absolute_name = absolutize_subcard_name name
  subcard_args = extract_subcard_args! attributes
  card = initialize_by_attributes absolute_name, attributes
  subcard = new_by_card card
  card.subcards.add subcard_args
  subcard
end

#new_by_card(card) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/card/subcards/add.rb', line 54

def new_by_card card
  card.supercard = @context_card
  card.update_superleft card.name
  @keys << card.key
  Card.write_to_soft_cache card
  card.director = @context_card.director.subdirectors.add card
  card
end