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.



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

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



64
65
66
67
68
# File 'lib/card/subcards.rb', line 64

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.



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

def context_card
  @context_card
end

#keysObject

Returns the value of attribute keys.



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

def keys
  @keys
end

Instance Method Details

#[](name) ⇒ Object



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

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

#card(name) ⇒ Object



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

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

  fetch_subcard name
end

#catch_up_to_stage(stage_index) ⇒ Object



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

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



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/card/subcards.rb', line 70

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



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

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

#fetch_subcard(key) ⇒ Object



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

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

#field(name) ⇒ Object



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

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

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  @keys.present?
end

#rename(old_name, new_name) ⇒ Object



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

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)


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

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