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

Extended by:
Card::Set
Defined in:
tmpsets/set/mod001-01_core/all/name.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Card::Set

abstract_set?, all_set?, card_accessor, card_reader, card_writer, clean_empty_module_from_hash, clean_empty_modules, define_active_job, define_event_method, define_event_perform_later_method, define_on_format, ensure_set, event, extended, format, phase_method, process_base_module_list, process_base_modules, register_set, register_set_format, shortname, view, write_tmp_file

Instance Method Details

#[](*args) ⇒ Object



103
104
105
106
107
108
109
110
111
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 103

def [] *args
  case args[0]
  when Fixnum, Range
    fetch_name = Array.wrap(cardname.parts[args[0]]).compact.join '+'
    Card.fetch(fetch_name, args[1] || {}) if !simple?
  else
    super
  end
end

#absolute_name(context_name = nil) ⇒ Object



81
82
83
84
85
86
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 81

def absolute_name context_name=nil
  if !context_name && @supercard
    context_name = @supercard.cardname
  end
  cardname.absolute_name(context_name)
end

#autoname(name) ⇒ Object



53
54
55
56
57
58
59
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 53

def autoname name
  if Card.exists? name
    autoname name.next
  else
    name
  end
end

#cardnameObject



49
50
51
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 49

def cardname
  name.to_name
end

#childrenObject



125
126
127
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 125

def children
  Card.search((simple? ? :part : :left) => name).to_a
end

#contextual_nameObject



70
71
72
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 70

def contextual_name
  @contextual_name || name
end

#dependentsObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 129

def dependents
  return [] if new_card?

  if @dependents.nil?
    @dependents =
      Auth.as_bot do
        deps = children
        deps.inject(deps) do |array, card|
          array + card.dependents
        end
      end
    # Rails.logger.warn "dependents[#{inspect}] #{@dependents.inspect}"
  end
  @dependents
end

#execute_referencers_update(dependents) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 300

def execute_referencers_update dependents
  Auth.as_bot do
    [name_referencers(name_was) + dependents.map(&:referencers)]
      .flatten.uniq.each do |card|
      # FIXME:  using 'name_referencers' instead of plain 'referencers' for self
      # because there are cases where trunk and tag
      # have already been saved via association by this point and therefore
      # referencers misses things
      # eg.  X includes Y, and Y is renamed to X+Z.  When X+Z is saved, X is
      # first updated as a trunk before X+Z gets to this point.
      # so at this time X is still including Y, which does not exist.
      # therefore #referencers doesn't find it, but name_referencers(old_name)
      # does.
      # some even more complicated scenario probably breaks on the dependents,
      # so this probably needs a more thoughtful refactor
      # aligning the dependent saving with the name cascading

      Rails.logger.debug "------------------ UPDATE REFERER #{card.name} " \
                         '------------------------'
      unless card == self || card.structure
        card = card.refresh
        card.db_content = card.replace_references name_was, name
        card.save!
      end
    end
  end
end

#junction?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 66

def junction?
  cardname.junction?
end

#left(*args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 88

def left *args
  return if simple?
  @superleft || begin
    unless name_changed? &&
           name.to_name.trunk_name.key == name_was.to_name.key
      # prevent recursion when, eg, renaming A+B to A+B+C
      Card.fetch cardname.left, *args
    end
  end
end

#left_or_new(args = {}) ⇒ Object



121
122
123
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 121

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

#name=(newname) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 22

def name= newname
  cardname = newname.to_name
  if @supercard
    @supercard.subcards.rename key, cardname.key
    @contextual_name = cardname.to_s
    relparts = cardname.parts
    if relparts.size == 2 &&
       (relparts.first.blank? || relparts.first.to_name.key == @supercard.key)
      @superleft = @supercard
    end
    cardname = cardname.to_absolute_name @supercard.name
  end

  newkey = cardname.key
  if key != newkey
    self.key = newkey
    # reset the old name - should be handled in tracked_attributes!!
    reset_patterns_if_rule
    reset_patterns
  end
  subcards.each do |subcard|
    subcard.name = subcard.cardname.replace_part name, newname
  end

  write_attribute :name, cardname.s
end

#relative_name(context_name = nil) ⇒ Object



74
75
76
77
78
79
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 74

def relative_name context_name=nil
  if !context_name && @supercard
    context_name = @supercard.cardname
  end
  cardname.relative_name(context_name)
end

#repair_keyObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 145

def repair_key
  Auth.as_bot do
    correct_key = cardname.key
    current_key = key
    return self if current_key == correct_key

    if (key_blocker = Card.find_by_key_and_trash(correct_key, true))
      key_blocker.cardname = key_blocker.cardname + "*trash#{rand(4)}"
      key_blocker.save
    end

    saved =   (self.key      = correct_key) && self.save!
    saved ||= (self.cardname = current_key) && self.save!

    if saved
      dependents.each(&:repair_key)
    else
      Rails.logger.debug "FAILED TO REPAIR BROKEN KEY: #{key}"
      self.name = "BROKEN KEY: #{name}"
    end
    self
  end
rescue
  Rails.logger.info "BROKE ATTEMPTING TO REPAIR BROKEN KEY: #{key}"
  self
end

#right(*args) ⇒ Object



99
100
101
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 99

def right *args
  Card.fetch(cardname.right, *args) if !simple?
end

#simple?Boolean

FIXME: use delegations and include all cardname functions

Returns:

  • (Boolean)


62
63
64
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 62

def simple?
  cardname.simple?
end

#suspend_name(name) ⇒ Object



266
267
268
269
270
271
272
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 266

def suspend_name name
  # move the current card out of the way, in case the new name will require
  # re-creating a card with the current name, ie.  A -> A+B
  Card.expire name
  tmp_name = 'tmp:' + UUID.new.generate
  Card.where(id: id).update_all(name: tmp_name, key: tmp_name)
end

#tag(*args) ⇒ Object



117
118
119
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 117

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

#trunk(*args) ⇒ Object



113
114
115
# File 'tmpsets/set/mod001-01_core/all/name.rb', line 113

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