Class: Card::Subcards

Inherits:
Object
  • Object
show all
Includes:
Add, Relate, Remove
Defined in:
lib/card/subcards.rb,
lib/card/subcards/add.rb,
lib/card/subcards/relate.rb,
lib/card/subcards/remove.rb

Overview

API to create/update/delete additional cards together with the main card. The most common case is for fields but subcards don't have to be descendants.

Subcards can be added as card objects or attribute hashes.

Use the methods defined in core/set/all/subcards.rb Example Together with "my address" you want to create the subcards "my address+name", "my address+street", etc.

Defined Under Namespace

Modules: Add, Relate, Remove

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Relate

#child, #field_name_to_key, #relative_child

Methods included from Remove

#clear, #clear_key, #deep_clear, #remove, #remove_child

Methods included from Add

#<<, #[]=, #add, #add_child, #add_hash, #extract_subcard_args!, #initialize_by_attributes, #new_by_attributes, #new_by_card

Constructor Details

#initialize(context_card) ⇒ Subcards

Returns a new instance of Subcards.



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

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



60
61
62
63
64
# File 'lib/card/subcards.rb', line 60

def method_missing method, *args
  return unless respond_to_missing?(method)

  @keys.send method, *args
end

Instance Attribute Details

#context_cardObject

Returns the value of attribute context_card.



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

def context_card
  @context_card
end

#keysObject

Returns the value of attribute keys.



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

def keys
  @keys
end

Instance Method Details

#[](name) ⇒ Object



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

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

#card(name) ⇒ Object



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

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

  fetch_subcard name
end

#cardsObject

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



70
71
72
73
74
# File 'lib/card/subcards.rb', line 70

def cards
  @keys.map do |key|
    fetch_subcard key
  end.compact
end

#catch_up_to_stage(stage_index) ⇒ Object



43
44
45
46
47
# File 'lib/card/subcards.rb', line 43

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

#each_cardObject Also known as: each



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

def each_card
  cards.each do |card|
    yield card
  end
end

#each_with_keyObject



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

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

#fetch_subcard(key) ⇒ Object



91
92
93
# File 'lib/card/subcards.rb', line 91

def fetch_subcard key
  Card.fetch key, local_only: true, new: {}
end

#field(name) ⇒ Object



28
29
30
31
# File 'lib/card/subcards.rb', line 28

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

#present?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/card/subcards.rb', line 39

def present?
  @keys.present?
end

#rename(old_name, new_name) ⇒ Object



49
50
51
52
53
54
# File 'lib/card/subcards.rb', line 49

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

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing? method_name, _include_private=false
  @keys.respond_to? method_name
end