class Card::Subcards

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.

Attributes

context_card[RW]
keys[RW]

Public Class Methods

new(context_card) click to toggle source
# File lib/card/subcards.rb, line 19
def initialize context_card
  @context_card = context_card
  @keys = ::Set.new
end

Public Instance Methods

[](name) click to toggle source
# File lib/card/subcards.rb, line 24
def [] name
  card(name) || field(name)
end
card(name) click to toggle source
# File lib/card/subcards.rb, line 33
def card name
  return unless @keys.include? name.to_name.key

  fetch_subcard name
end
cards() click to toggle source

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

# 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) click to toggle source
# 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(&block)
Alias for: each_card
each_card(&block) click to toggle source
# File lib/card/subcards.rb, line 76
def each_card &block
  cards.each(&block)
end
Also aliased as: each
each_with_key() { |card, key| ... } click to toggle source
# File lib/card/subcards.rb, line 82
def each_with_key
  @keys.each do |key|
    card = fetch_subcard(key)
    yield(card, key) if card
  end
end
fetch_subcard(key) click to toggle source
# File lib/card/subcards.rb, line 89
def fetch_subcard key
  Card.fetch key, local_only: true, new: {}
end
field(name) click to toggle source
# File lib/card/subcards.rb, line 28
def field name
  key = field_name_to_key name
  fetch_subcard key if @keys.include? key
end
method_missing(method, *args) click to toggle source
# File lib/card/subcards.rb, line 60
def method_missing method, *args
  return unless respond_to_missing?(method)

  @keys.send method, *args
end
present?() click to toggle source
# File lib/card/subcards.rb, line 39
def present?
  @keys.present?
end
rename(old_name, new_name) click to toggle source
# 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) click to toggle source
# File lib/card/subcards.rb, line 56
def respond_to_missing? method_name, _include_private=false
  @keys.respond_to? method_name
end

Private Instance Methods

absolutize_subcard_name(name) click to toggle source
# File lib/card/subcards.rb, line 105
def absolutize_subcard_name name
  name = Card::Name[name]
  return name if @context_card.name.parts.first.blank?

  name.absolute_name @context_card.name
end
subcard_key(cardish) click to toggle source
# File lib/card/subcards.rb, line 95
def subcard_key cardish
  key = case cardish
        when Card   then cardish.key
        when Symbol then fetch_subcard(cardish).key
        else             cardish.to_name.key
        end
  key = absolutize_subcard_name(key).key unless @keys.include?(key)
  key
end