Class: Card::Subcards
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(context_card) ⇒ Subcards
Returns a new instance of Subcards.
39
40
41
42
|
# File 'lib/card/subcards.rb', line 39
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
110
111
112
113
|
# File 'lib/card/subcards.rb', line 110
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.
38
39
40
|
# File 'lib/card/subcards.rb', line 38
def context_card
@context_card
end
|
Returns the value of attribute keys.
38
39
40
|
# File 'lib/card/subcards.rb', line 38
def keys
@keys
end
|
Instance Method Details
#<<(value) ⇒ Object
106
107
108
|
# File 'lib/card/subcards.rb', line 106
def << value
add value
end
|
146
147
148
|
# File 'lib/card/subcards.rb', line 146
def [] name
card(name) || field(name)
end
|
#[]=(name, card_or_attr) ⇒ Object
137
138
139
140
141
142
143
144
|
# File 'lib/card/subcards.rb', line 137
def []= name, card_or_attr
case card_or_attr
when Hash
new_by_attributes name, card_or_attr
when Card
new_by_card card_or_attr
end
end
|
#add(name_or_card_or_attr, card_or_attr = nil) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/card/subcards.rb', line 68
def add name_or_card_or_attr, card_or_attr=nil
if card_or_attr
name = name_or_card_or_attr
else
card_or_attr = name_or_card_or_attr
end
case card_or_attr
when Hash
args = card_or_attr
if name
new_by_attributes name, args
elsif args[:name]
new_by_attributes args.delete(:name), args
else
args.each_pair do |key, val|
case val
when String then new_by_attributes key, content: val
when Card
val.name = absolutize_subcard_name key
new_by_card val
else new_by_attributes key, val
end
end
end
when Card
new_by_card card_or_attr
when Symbol, String
new_by_attributes card_or_attr, {}
end
end
|
#add_child(name, args) ⇒ Object
Also known as:
add_field
162
163
164
|
# File 'lib/card/subcards.rb', line 162
def add_child name, args
add prepend_plus(name), args
end
|
#card(name) ⇒ Object
157
158
159
160
|
# File 'lib/card/subcards.rb', line 157
def card name
return unless @keys.include? name.to_name.key
fetch_subcard name
end
|
44
45
46
47
48
49
|
# File 'lib/card/subcards.rb', line 44
def clear
@keys.each do |key|
Card.cache.delete_local key
end
@keys = ::Set.new
end
|
#each_card ⇒ Object
Also known as:
each
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/card/subcards.rb', line 115
def each_card
cards = @keys.map do |key|
fetch_subcard key
end
cards.each do |card|
yield(card) if card
end
end
|
#each_with_key ⇒ Object
130
131
132
133
134
135
|
# File 'lib/card/subcards.rb', line 130
def each_with_key
@keys.each do |key|
card = fetch_subcard(key)
yield(card, key) if card
end
end
|
#field(name) ⇒ Object
150
151
152
153
154
155
|
# File 'lib/card/subcards.rb', line 150
def field name
key = field_name_to_key name
if @keys.include? key
fetch_subcard key
end
end
|
#present? ⇒ Boolean
182
183
184
|
# File 'lib/card/subcards.rb', line 182
def present?
@keys.present?
end
|
#remove(name_or_card) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/card/subcards.rb', line 51
def remove name_or_card
key = case name_or_card
when Card
name_or_card.key
when Symbol
fetch_subcard(name_or_card).key
else
name_or_card.to_name.key
end
key = absolutize_subcard_name(key).key unless @keys.include?(key)
@keys.delete key
removed_card = fetch_subcard key
Card.cache.delete_local key
removed_card
end
|
#remove_child(name_or_card) ⇒ Object
Also known as:
remove_field
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/card/subcards.rb', line 166
def remove_child name_or_card
if name_or_card.is_a? Card
remove name_or_card
else
absolute_name = @context_card.cardname.field_name(name_or_card)
if @keys.include? absolute_name.key
remove absolute_name
else
remove @context_card.cardname.relative_field_name(name_or_card)
end
end
end
|
#rename(old_name, new_name) ⇒ Object
99
100
101
102
|
# File 'lib/card/subcards.rb', line 99
def rename old_name, new_name
return unless @keys.include? old_name.to_name.key
end
|