Class: Card::Subcards

Inherits:
Object
  • Object
show all
Defined in:
lib/card/subcards.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_card) ⇒ Subcards

Returns a new instance of Subcards.



24
25
26
27
# File 'lib/card/subcards.rb', line 24

def initialize context_card
  @context_card = context_card
  @keys = ::Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



122
123
124
125
# File 'lib/card/subcards.rb', line 122

def method_missing method, *args
  return unless @keys.respond_to? method
  @keys.send method, *args
end

Instance Attribute Details

#context_cardObject

Returns the value of attribute context_card.



23
24
25
# File 'lib/card/subcards.rb', line 23

def context_card
  @context_card
end

#keysObject

Returns the value of attribute keys.



23
24
25
# File 'lib/card/subcards.rb', line 23

def keys
  @keys
end

Instance Method Details

#<<(value) ⇒ Object



118
119
120
# File 'lib/card/subcards.rb', line 118

def << value
  add value
end

#[](name) ⇒ Object



158
159
160
# File 'lib/card/subcards.rb', line 158

def [] name
  card(name) || field(name)
end

#[]=(name, card_or_attr) ⇒ Object



149
150
151
152
153
154
155
156
# File 'lib/card/subcards.rb', line 149

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(name_or_card_or_attr, attr_or_opts = {}) ⇒ 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)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/card/subcards.rb', line 41

def add name_or_card_or_attr, attr_or_opts={}
  case name_or_card_or_attr
  when Card
    new_by_card name_or_card_or_attr, attr_or_opts
  when Symbol, String
    new_by_attributes name_or_card_or_attr, attr_or_opts
  when Hash
    args = name_or_card_or_attr
    if args[:name]
      new_by_attributes args.delete(:name), args
    else
      multi_add args
    end
  end
end

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



57
58
59
# File 'lib/card/subcards.rb', line 57

def add_child name, args
  add prepend_plus(name), args
end

#card(name) ⇒ Object



167
168
169
170
# File 'lib/card/subcards.rb', line 167

def card name
  return unless @keys.include? name.to_name.key
  fetch_subcard name
end

#catch_up_to_stage(stage_index) ⇒ Object



76
77
78
79
80
# File 'lib/card/subcards.rb', line 76

def catch_up_to_stage stage_index
  each_card do |subcard|
    subcard.catch_up_to_stage stage_index
  end
end

#clearObject



82
83
84
85
86
87
88
89
90
# File 'lib/card/subcards.rb', line 82

def clear
  @keys.each do |key|
    if (subcard = fetch_subcard key)
      Card::DirectorRegister.delete subcard.director
    end
    Card.cache.soft.delete key
  end
  @keys = ::Set.new
end

#deep_clear(cleared = ::Set.new) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/card/subcards.rb', line 92

def deep_clear cleared=::Set.new
  each_card do |card|
    next if cleared.include? card.id
    cleared << card.id
    card.subcards.deep_clear cleared
  end
  clear
end

#each_cardObject Also known as: each



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/card/subcards.rb', line 127

def each_card
  # fetch all cards first to avoid side effects
  # e.g. deleting a user adds follow rules and +*account to subcards
  # for deleting but deleting follow rules can remove +*account from the
  # cache if it belongs to the rule cards
  cards = @keys.map do |key|
    fetch_subcard key
  end
  cards.each do |card|
    yield(card) if card
  end
end

#each_with_keyObject



142
143
144
145
146
147
# File 'lib/card/subcards.rb', line 142

def each_with_key
  @keys.each do |key|
    card = fetch_subcard(key)
    yield(card, key) if card
  end
end

#field(name) ⇒ Object



162
163
164
165
# File 'lib/card/subcards.rb', line 162

def field name
  key = field_name_to_key name
  fetch_subcard key if @keys.include? key
end

#present?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/card/subcards.rb', line 172

def present?
  @keys.present?
end

#remove(name_or_card) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/card/subcards.rb', line 101

def remove name_or_card
  key = subcard_key name_or_card
  return unless @keys.include? key
  @keys.delete key
  removed_card = fetch_subcard key
  removed_card.current_action.delete if removed_card.current_action
  Card::DirectorRegister.deep_delete removed_card.director
  Card.cache.soft.delete key
  removed_card
end

#remove_child(name_or_card) ⇒ Object Also known as: remove_field



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

def remove_child name_or_card
  if name_or_card.is_a? Card
    remove name_or_card
  else
    absolute_name = @context_card.cardname.field_name(name_or_card)
    if @keys.include? absolute_name.key
      remove absolute_name
    else
      remove @context_card.cardname.relative_field_name(name_or_card)
    end
  end
end

#rename(old_name, new_name) ⇒ Object



112
113
114
115
116
# File 'lib/card/subcards.rb', line 112

def rename old_name, new_name
  return unless @keys.include? old_name.to_name.key
  @keys.delete old_name.to_name.key
  @keys << new_name.to_name.key
end