Module: Card::Subcards::Add

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

Overview

Methods for adding subcards

Instance Method Summary collapse

Instance Method Details

#<<(value) ⇒ Object

add 'spoiler', content: 'John Snow is a Targaryen', transact_in_stage: :integrate add card_obj, delayed: true

Examples:

Add a subcard with name 'spoiler'

add 'spoiler', type: 'Phrase', content: 'John Snow is a Targaryen'
card_obj = Card.new name: 'spoiler', type: 'Phrase',
                    content: 'John Snow is a Targaryen'
add card_obj
add name: 'spoiler', type: 'Phrase', content: 'John Snow is a Targaryen'

Add a subcard that is added in the integration phase

(and hence doesn't hold up the transaction for the main card)


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

def << value
  add value
end

#[]=(name, card_or_attr) ⇒ Object



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

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



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

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

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



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

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



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

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

#extract_subcard_args!(args) ⇒ Object

TODO: this method already exists as card instance method in tracked_attributes.rb. Find a place for it where its accessible for both. There is one important difference. The keys are symbols here instead of strings



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/card/subcards/add.rb', line 83

def extract_subcard_args! args
  subcards = args.delete(:subcards) || {}
  if (subfields = args.delete(:subfields))
    subfields.each_pair do |key, value|
      subcards[normalize_subfield_key(key)] = value
    end
  end
  args.keys.each do |key|
    subcards[key] = args.delete(key) if key =~ /^\+/
  end
  subcards
end

#initialize_by_attributes(name, attributes) ⇒ Object



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

def initialize_by_attributes name, attributes
  Card.assign_or_initialize_by name, attributes, local_only: true
end

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



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

def new_by_attributes name, attributes={}
  attributes ||= {}
  absolute_name = absolutize_subcard_name name
  subcard_args = extract_subcard_args! attributes
  t_i_s = attributes.delete(:transact_in_stage)
  card = initialize_by_attributes absolute_name, attributes
  subcard = new_by_card card, transact_in_stage: t_i_s
  card.subcards.add subcard_args
  subcard
end

#new_by_card(card, opts = {}) ⇒ Object



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

def new_by_card card, opts={}
  card.supercard = @context_card
  if !card.name.simple? && card.name.field_of?(@context_card.name)
    card.superleft = @context_card
  end
  @keys << card.key
  Card.write_to_soft_cache card
  card.director = @context_card.director.subdirectors.add(card, opts)
  card
end