Module: Card::Set::All::Name

Extended by:
Card::Set
Defined in:
card/tmpsets/set/mod002-core/all/name.rb

Overview

Set: All cards (Name)

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



7
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/card/mod/core/set/all/name.rb"; end

Instance Method Details

#[](*args) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 148

def [] *args
  case args[0]
  when Integer, Range
    fetch_name = Array.wrap(name.parts[args[0]]).compact.join Card::Name.joint
    Card.fetch(fetch_name, args[1] || {}) unless simple?
  else
    super
  end
end

#assign_side_id(side_id_equals, side_name) ⇒ Object

assigns left_id and right_id based on names. if side card is new, id is temporarily stored as -1



58
59
60
61
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 58

def assign_side_id side_id_equals, side_name
  side_id = Card::Lexicon.id(name.send(side_name)) || -1
  send side_id_equals, side_id
end

#assign_side_idsObject



47
48
49
50
51
52
53
54
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 47

def assign_side_ids
  if name.simple?
    self.left_id = self.right_id = nil
  else
    assign_side_id :left_id=, :left_name
    assign_side_id :right_id=, :right_name
  end
end

#autoname(name) ⇒ Object



122
123
124
125
126
127
128
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 122

def autoname name
  if Card.exists?(name) || Director.include?(name)
    autoname name.next
  else
    name
  end
end

#child_ids(side = nil) ⇒ Object

eg, A+B is a child of A and B



193
194
195
196
197
198
199
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 193

def child_ids side=nil
  return [] unless id
  side ||= name.simple? ? :part : :left_id
  Auth.as_bot do
    Card.search({ side => id, return: :id, limit: 0 }, "children of #{name}")
  end
end

#clean_patternsObject



85
86
87
88
89
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 85

def clean_patterns
  return unless patterns?
  reset_patterns
  patterns
end

#each_childObject



185
186
187
188
189
190
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 185

def each_child
  child_ids.each do |id|
    (child = Card[id]) && yield(child)
    # check should not be needed (remove after fixing data problems)
  end
end

#each_descendant(&block) ⇒ Object



201
202
203
204
205
206
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 201

def each_descendant &block
  each_child do |child|
    yield child
    child.each_descendant(&block)
  end
end

#field_idsObject



181
182
183
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 181

def field_ids
  child_ids :left
end

#field_namesObject



177
178
179
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 177

def field_names
  field_ids.map { |id| Card::Name[id] }
end

#fieldsObject

NOTE: for all these helpers, method returns all fields/children/descendants. (Not just those current user has permission to read.)



173
174
175
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 173

def fields
  field_ids.map { |id| Card[id] }
end

#keyObject



34
35
36
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 34

def key
  @key ||= left_id ? name.key : super
end

#key=(newkey) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 75

def key= newkey
  return if newkey == key
  update_cache_key key do
    write_attribute :key, (name.simple? ? newkey : nil)
    @key = newkey
  end
  clean_patterns
  @key
end

#left(*args) ⇒ Object



133
134
135
136
137
138
139
140
141
142
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 133

def left *args
  case
  when simple?    then nil
  when superleft then superleft
  when name_is_changing? && name.to_name.trunk_name == name_before_act.to_name
    nil # prevent recursion when, eg, renaming A+B to A+B+C
  else
    Card.fetch name.left, *args
  end
end

#left_id=(cardish) ⇒ Object



212
213
214
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 212

def left_id= cardish
  write_card_or_id :left_id, cardish
end

#left_or_new(args = {}) ⇒ Object



166
167
168
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 166

def left_or_new args={}
  left(args) || Card.new(args.merge(name: name.left))
end

#nameObject



30
31
32
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 30

def name
  @name ||= left_id ? Card::Lexicon.lex_to_name([left_id, right_id]) : super.to_name
end

#name=(newname) ⇒ Object



38
39
40
41
42
43
44
45
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 38

def name= newname
  @name = superize_name newname.to_name
  self.key = @name.key
  update_subcard_names @name
  write_attribute :name, (@name.simple? ? @name.s : nil)
  assign_side_ids
  @name
end

#name_to_replace_for_subcard(subcard, new_name) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 110

def name_to_replace_for_subcard subcard, new_name
  # if subcard has a relative name like +C
  # and self is a subcard as well that changed from +B to A+B then
  # +C should change to A+B+C. #replace doesn't work in this case
  # because the old name +B is not a part of +C
  if subcard.name.starts_with_joint? && new_name.parts.first.present?
    "".to_name
  else
    name
  end
end

#right(*args) ⇒ Object



144
145
146
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 144

def right *args
  Card.fetch(name.right, *args) unless simple?
end

#right_id=(cardish) ⇒ Object



208
209
210
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 208

def right_id= cardish
  write_card_or_id :right_id, cardish
end

#superize_name(cardname) ⇒ Object



63
64
65
66
67
68
69
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 63

def superize_name cardname
  return cardname unless @supercard

  @supercard.subcards.rename key, cardname.key
  update_superleft cardname
  @supercard.name.relative? ? cardname : cardname.absolute_name(@supercard.name)
end

#tag(*args) ⇒ Object



162
163
164
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 162

def tag *args
  simple? ? self : Card.fetch(name.right, *args)
end

#trunk(*args) ⇒ Object



158
159
160
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 158

def trunk *args
  simple? ? self : left(*args)
end

#update_cache_key(oldkey) ⇒ Object



91
92
93
94
95
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 91

def update_cache_key oldkey
  yield
  was_in_cache = Card.cache.soft.delete oldkey
  Card.write_to_soft_cache self if was_in_cache
end

#update_subcard_name(subcard, new_name, name_to_replace) ⇒ Object



104
105
106
107
108
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 104

def update_subcard_name subcard, new_name, name_to_replace
  name_to_replace ||= name_to_replace_for_subcard subcard, new_name
  subcard.name = subcard.name.swap name_to_replace, new_name.s
  subcard.update_subcard_names new_name, name # needed?  shouldn't #name= trigger this?
end

#update_subcard_names(new_name, name_to_replace = nil) ⇒ Object



97
98
99
100
101
102
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 97

def update_subcard_names new_name, name_to_replace=nil
  return unless @subcards
  subcards.each do |subcard|
    update_subcard_name subcard, new_name, name_to_replace if subcard.new?
  end
end

#update_superleft(cardname) ⇒ Object



71
72
73
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 71

def update_superleft cardname
  @superleft = @supercard if cardname.field_of? @supercard.name
end

#when_id_exists(cardish, &block) ⇒ Object



220
221
222
223
224
225
226
227
228
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 220

def when_id_exists cardish, &block
  if (card_id = Card.id cardish)
    yield card_id
  elsif cardish.is_a? Card
    with_id_after_store cardish, &block
  else
    yield cardish # eg nil
  end
end

#with_id_after_store(subcard) ⇒ Object

subcards are usually saved after super cards; after_store forces it to save the subcard first and callback afterwards



233
234
235
236
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 233

def with_id_after_store subcard
  add_subcard subcard
  subcard.director.after_store { |card| yield card.id }
end

#write_card_or_id(attribute, cardish) ⇒ Object



216
217
218
# File 'card/tmpsets/set/mod002-core/all/name.rb', line 216

def write_card_or_id attribute, cardish
  when_id_exists(cardish) { |id| write_attribute attribute, id }
end