Module: Card::Subcards::All

Included in:
Card
Defined in:
card/lib/card/subcards/all.rb

Overview

subcard-related Card instance methods

Instance Method Summary collapse

Instance Method Details

#add_subcard(name_or_card, args = {}) ⇒ Object Also known as: attach_subcard

phase_method :attach_subcard, before: :store do |name_or_card, args=nil| TODO: handle differently in different stages



31
32
33
# File 'card/lib/card/subcards/all.rb', line 31

def add_subcard name_or_card, args={}
  subcards.add name_or_card, args
end

#add_subcard!(name_or_card, args = {}) ⇒ Object Also known as: attach_subcard!



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

def add_subcard! name_or_card, args={}
  subcard = subcards.add name_or_card, args
  subcard.director.reset_stage
  subcard
end

#attach_subfield(name_or_card, args = {}) ⇒ Object Also known as: add_subfield

phase_method :attach_subfield, before: :approve do name_or_card, args=nil


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

def attach_subfield name_or_card, args={}
  subcards.add_field name_or_card, args
end

#attach_subfield!(name_or_card, args = {}) ⇒ Object



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

def attach_subfield! name_or_card, args={}
  subcard = subcards.add_field name_or_card, args
  subcard.director.reset_stage
  subcard
end

#clear_subcardsObject



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

def clear_subcards
  subcards.clear
end

#deep_clear_subcardsObject



83
84
85
# File 'card/lib/card/subcards/all.rb', line 83

def deep_clear_subcards
  subcards.deep_clear
end

#detach_subcard(name_or_card) ⇒ Object Also known as: remove_subcard



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

def detach_subcard name_or_card
  subcards.remove name_or_card
end

#detach_subfield(name_or_card) ⇒ Object Also known as: remove_subfield



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

def detach_subfield name_or_card
  subcards.remove_field name_or_card
end

#ensure_subfield(field_name, args = {}) ⇒ Object

ensures subfield is present does NOT override subfield content if already present



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

def ensure_subfield field_name, args={}
  if subfield_present? field_name
    subfield field_name
  else
    add_subfield field_name, args
  end
end

#expire_subcardsObject



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

def expire_subcards
  subcards.clear
end

#field?(tag) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'card/lib/card/subcards/all.rb', line 13

def field? tag
  field(tag) || subfield(tag)
end

#handle_subcard_errorsObject



87
88
89
90
91
92
93
94
# File 'card/lib/card/subcards/all.rb', line 87

def handle_subcard_errors
  subcards.each do |subcard|
    subcard.errors.each do |error|
      subcard_error subcard, error
    end
    subcard.errors.clear
  end
end

#subcard(card_name) ⇒ Object



5
6
7
# File 'card/lib/card/subcards/all.rb', line 5

def subcard card_name
  subcards.card card_name
end

#subcard_error(subcard, error) ⇒ Object



96
97
98
99
100
# File 'card/lib/card/subcards/all.rb', line 96

def subcard_error subcard, error
  msg = error.message
  msg = "#{error.attribute} #{msg}" unless %i[content abort].member? error.attribute
  errors.add subcard.name.from(name), msg
end

#subcardsObject



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

def subcards
  @subcards ||= Card::Subcards.new self
end

#subcards?Boolean

Returns:

  • (Boolean)


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

def subcards?
  subcards.present?
end

#subfield(field_name) ⇒ Object



9
10
11
# File 'card/lib/card/subcards/all.rb', line 9

def subfield field_name
  subcards.field field_name
end

#subfield_present?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'card/lib/card/subcards/all.rb', line 79

def subfield_present? field_name
  subfield(field_name)&.content&.present?
end