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

Extended by:
Card::Set
Defined in:
tmpsets/set/mod003-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

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, #ensure_set, #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 'tmpsets/set/mod003-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



119
120
121
122
123
124
125
126
127
# File 'tmpsets/set/mod003-core/all/name.rb', line 119

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

#autoname(name) ⇒ Object



83
84
85
86
87
88
89
# File 'tmpsets/set/mod003-core/all/name.rb', line 83

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

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



153
154
155
156
157
158
159
# File 'tmpsets/set/mod003-core/all/name.rb', line 153

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



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

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

#descendant_ids(parent_id = nil) ⇒ Object

ids of children and children's children



162
163
164
165
166
167
168
169
170
# File 'tmpsets/set/mod003-core/all/name.rb', line 162

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



175
176
177
# File 'tmpsets/set/mod003-core/all/name.rb', line 175

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

#field_names(parent_name = nil) ⇒ Object



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

def field_names parent_name=nil
  child_names parent_name, :left
end

#fieldsObject



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

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

#junction?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'tmpsets/set/mod003-core/all/name.rb', line 96

def junction?
  name.junction?
end

#key=(newkey) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'tmpsets/set/mod003-core/all/name.rb', line 51

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



104
105
106
107
108
109
110
111
112
113
# File 'tmpsets/set/mod003-core/all/name.rb', line 104

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

#left_id=(card_or_id) ⇒ Object



210
211
212
# File 'tmpsets/set/mod003-core/all/name.rb', line 210

def left_id= card_or_id
  write_card_or_id :left_id, card_or_id
end

#left_or_new(args = {}) ⇒ Object



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

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

#nameObject



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

def name
  super.to_name
end

#name=(newname) ⇒ Object



34
35
36
37
38
39
40
41
# File 'tmpsets/set/mod003-core/all/name.rb', line 34

def name= newname
  cardname = superize_name newname.to_name
  newkey = cardname.key
  self.key = newkey if key != newkey
  update_subcard_names cardname
  write_attribute :name, cardname.s
  name
end

#raw_nameObject



100
101
102
# File 'tmpsets/set/mod003-core/all/name.rb', line 100

def raw_name
  @raw_name || name
end

#repair_keyObject



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

def repair_key
  Auth.as_bot do
    correct_key = name.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.name = key_blocker.name + "*trash#{rand(4)}"
      key_blocker.save
    end

    saved =   (self.key = correct_key) && save!
    saved ||= (self.name = 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 StandardError
  Rails.logger.info "BROKE ATTEMPTING TO REPAIR BROKEN KEY: #{key}"
  self
end

#right(*args) ⇒ Object



115
116
117
# File 'tmpsets/set/mod003-core/all/name.rb', line 115

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

#right_id=(card_or_id) ⇒ Object



206
207
208
# File 'tmpsets/set/mod003-core/all/name.rb', line 206

def right_id= card_or_id
  write_card_or_id :right_id, card_or_id
end

#simple?Boolean

FIXME: use delegations and include all name functions

Returns:

  • (Boolean)


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

def simple?
  name.simple?
end

#superize_name(cardname) ⇒ Object



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

def superize_name cardname
  return cardname unless @supercard
  @raw_name = cardname.s
  @supercard.subcards.rename key, cardname.key
  @superleft = @supercard if cardname.field_of? @supercard.name
  cardname.absolute_name @supercard.name
end

#tag(*args) ⇒ Object



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

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

#trunk(*args) ⇒ Object



129
130
131
# File 'tmpsets/set/mod003-core/all/name.rb', line 129

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

#type_id=(card_or_id) ⇒ Object



214
215
216
# File 'tmpsets/set/mod003-core/all/name.rb', line 214

def type_id= card_or_id
  write_card_or_id :type_id, card_or_id
end

#update_subcard_names(new_name, name_to_replace = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'tmpsets/set/mod003-core/all/name.rb', line 62

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

#with_id_when_exists(card, &block) ⇒ Object



238
239
240
# File 'tmpsets/set/mod003-core/all/name.rb', line 238

def with_id_when_exists card, &block
  card.director.call_after_store(&block)
end

#write_attribute_to_card(attribute, card) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
# File 'tmpsets/set/mod003-core/all/name.rb', line 226

def write_attribute_to_card attribute, card
  if card.id
    write_attribute attribute, card.id
  else
    add_subcard card
    card.director.prior_store = true
    with_id_when_exists(card) do |id|
      write_attribute attribute, id
    end
  end
end

#write_card_or_id(attribute, card_or_id) ⇒ Object



218
219
220
221
222
223
224
# File 'tmpsets/set/mod003-core/all/name.rb', line 218

def write_card_or_id attribute, card_or_id
  if card_or_id.is_a? Card
    write_attribute_to_card attribute, card_or_id
  else
    write_attribute attribute, card_or_id
  end
end