Class: Card::Subcards
- Inherits:
-
Object
- Object
- Card::Subcards
- 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
Instance Attribute Summary collapse
-
#context_card ⇒ Object
Returns the value of attribute context_card.
-
#keys ⇒ Object
Returns the value of attribute keys.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #card(name) ⇒ Object
- #catch_up_to_stage(stage_index) ⇒ Object
- #each_card ⇒ Object (also: #each)
- #each_with_key ⇒ Object
- #fetch_subcard(key) ⇒ Object
- #field(name) ⇒ Object
-
#initialize(context_card) ⇒ Subcards
constructor
A new instance of Subcards.
- #method_missing(method, *args) ⇒ Object
- #present? ⇒ Boolean
- #rename(old_name, new_name) ⇒ Object
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
58 59 60 61 |
# File 'lib/card/subcards.rb', line 58 def method_missing method, *args return unless @keys.respond_to? method @keys.send method, *args end |
Instance Attribute Details
#context_card ⇒ Object
Returns the value of attribute context_card.
22 23 24 |
# File 'lib/card/subcards.rb', line 22 def context_card @context_card end |
#keys ⇒ Object
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 |
# 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
46 47 48 49 50 |
# File 'lib/card/subcards.rb', line 46 def catch_up_to_stage stage_index each_card do |subcard| subcard.catch_up_to_stage stage_index end end |
#each_card ⇒ Object Also known as: each
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/card/subcards.rb', line 63 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_key ⇒ Object
78 79 80 81 82 83 |
# File 'lib/card/subcards.rb', line 78 def each_with_key @keys.each do |key| card = fetch_subcard(key) yield(card, key) if card end end |
#fetch_subcard(key) ⇒ Object
85 86 87 |
# File 'lib/card/subcards.rb', line 85 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
42 43 44 |
# File 'lib/card/subcards.rb', line 42 def present? @keys.present? end |
#rename(old_name, new_name) ⇒ Object
52 53 54 55 56 |
# File 'lib/card/subcards.rb', line 52 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 |