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

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from Format

Format::TEMPLATE_DIR

Instance Method Summary collapse

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

#abstract_set?, #all_set?, #num_set_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#attachment, #ensure_set, #stage_method

Methods included from Format

#all_set_format_mod!, #before, #define_on_format, #format, layout_method_name, #register_set_format, #view, #view_for_override, view_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Basket

#abstract_basket, #add_to_basket, #basket

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Instance Method Details

#[](*args) ⇒ Object



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

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

#absolute_name(context_name = nil) ⇒ Object



102
103
104
105
# File 'tmpsets/set/mod002-core/all/name.rb', line 102

def absolute_name context_name=nil
  context_name ||= @supercard.cardname if @supercard
  cardname.absolute_name context_name
end

#autoname(name) ⇒ Object



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

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

#cardnameObject



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

def cardname
  name.to_name
end

#child_names(parent_name = nil, side = nil) ⇒ Object



156
157
158
159
160
161
162
# File 'tmpsets/set/mod002-core/all/name.rb', line 156

def child_names parent_name=nil, side=nil
  # eg, A+B is a child of A and B
  parent_name ||= name
  side ||= parent_name.to_name.simple? ? :part : :left
  Card.search({ side => parent_name, return: :name },
              "(#{side}) children of #{parent_name}")
end

#childrenObject



152
153
154
# File 'tmpsets/set/mod002-core/all/name.rb', line 152

def children
  child_names.map { |name| Card[name] }
end

#contextual_nameObject



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

def contextual_name
  @contextual_name || name
end

#descendant_ids(parent_id = nil) ⇒ Object

ids of children and children's children



165
166
167
168
169
170
171
172
173
# File 'tmpsets/set/mod002-core/all/name.rb', line 165

def descendant_ids parent_id=nil
  return [] if new_card?
  parent_id ||= id
  Auth.as_bot do
    child_ids = Card.search part: parent_id, return: :id
    child_descendant_ids = child_ids.map { |cid| descendant_ids cid }
    (child_ids + child_descendant_ids).flatten.uniq
  end
end

#descendantsObject

children and children's children NOTE - set modules are not loaded -- should only be used for name manipulations



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

def descendants
  @descendants ||= descendant_ids.map { |id| Card.quick_fetch id }
end

#field_names(parent_name = nil) ⇒ Object



148
149
150
# File 'tmpsets/set/mod002-core/all/name.rb', line 148

def field_names parent_name=nil
  child_names parent_name, :left
end

#fieldsObject



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

def fields
  field_names.map { |name| Card[name] }
end

#junction?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'tmpsets/set/mod002-core/all/name.rb', line 89

def junction?
  cardname.junction?
end

#key=(newkey) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'tmpsets/set/mod002-core/all/name.rb', line 41

def key= newkey
  was_in_cache = Card.cache.soft.delete key
  write_attribute :key, newkey
  # keep the soft cache up-to-date
  Card.write_to_soft_cache self if was_in_cache
  # reset the old name - should be handled in tracked_attributes!!
  reset_patterns_if_rule
  reset_patterns
  newkey
end

#left(*args) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'tmpsets/set/mod002-core/all/name.rb', line 107

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

#left_or_new(args = {}) ⇒ Object



140
141
142
# File 'tmpsets/set/mod002-core/all/name.rb', line 140

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
# File 'tmpsets/set/mod002-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
  self.key = newkey if key != newkey
  update_subcard_names cardname
  write_attribute :name, cardname.s
end

#relative_name(context_name = nil) ⇒ Object



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

def relative_name context_name=nil
  context_name ||= @supercard.cardname if @supercard
  cardname.relative_name context_name
end

#repair_keyObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'tmpsets/set/mod002-core/all/name.rb', line 182

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) && save!
    saved ||= (self.cardname = current_key) && save!

    if saved
      descendants.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



118
119
120
# File 'tmpsets/set/mod002-core/all/name.rb', line 118

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

#simple?Boolean

FIXME: use delegations and include all cardname functions

Returns:

  • (Boolean)


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

def simple?
  cardname.simple?
end

#suspend_name(name) ⇒ Object



255
256
257
258
259
260
261
# File 'tmpsets/set/mod002-core/all/name.rb', line 255

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



136
137
138
# File 'tmpsets/set/mod002-core/all/name.rb', line 136

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

#trunk(*args) ⇒ Object



132
133
134
# File 'tmpsets/set/mod002-core/all/name.rb', line 132

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

#update_subcard_names(cardname) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'tmpsets/set/mod002-core/all/name.rb', line 52

def update_subcard_names cardname
  return unless @subcards
  subcards.each do |subcard|
    # 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_part doesn't work in this case
    # because the old name +B is not a part of +C
    name_to_replace =
      if subcard.cardname.junction? &&
         subcard.cardname.parts.first.empty? &&
         cardname.parts.first.present?
        # replace the empty part
        "".to_name
      else
        name
      end
    subcard.name = subcard.cardname.replace_part name_to_replace, cardname.s
  end
end