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/all.rb,
lib/card/subcards/args.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, All, Args, 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, #initialize_by_attributes, #new_by_attributes, #new_by_card

Methods included from Args

#extract_subcard_args!

Constructor Details

#initialize(context_card) ⇒ Subcards

Returns a new instance of Subcards.



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

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



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

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



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

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

#card(name) ⇒ Object



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

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



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

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

#catch_up_to_stage(stage_index) ⇒ Object



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

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

#each_card(&block) ⇒ Object Also known as: each



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

def each_card &block
  cards.each(&block)
end

#each_with_keyObject



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

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

#fetch_subcard(key) ⇒ Object



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

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

#field(name) ⇒ Object



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

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

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  @keys.present?
end

#rename(old_name, new_name) ⇒ Object



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

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)


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

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