# -*- encoding : utf-8 -*- class Card; module Set; class All # Set: All cards (Name) # module Name; extend Card::Set def self.source_location; "/Users/ethan/dev/decko/gem/card/mod/core/set/all/name.rb"; end require "uuid" module ClassMethods def uniquify_name name, rename=:new return name unless Card.exists? name uniq_name = generate_alternative_name name return uniq_name unless rename == :old rename!(name, uniq_name) name end def generate_alternative_name name uniq_name = "#{name} 1" uniq_name.next! while Card.exists?(uniq_name) uniq_name end def rename! oldname, newname Card[oldname].update! name: newname, update_referers: true end end def name @name ||= left_id ? Card::Lexicon.lex_to_name([left_id, right_id]) : super.to_name end def key @key ||= left_id ? name.key : super end 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 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 # assigns left_id and right_id based on names. # if side card is new, id is temporarily stored as -1 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 def superize_name cardname return cardname unless @supercard @raw_name = cardname.s @supercard.subcards.rename key, cardname.key update_superleft cardname cardname.absolute_name @supercard.name end def update_superleft cardname @superleft = @supercard if cardname.field_of? @supercard.name end 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 def clean_patterns return unless patterns? reset_patterns patterns end 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 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 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 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 def autoname name if Card.exists?(name) || ActManager.include?(name) autoname name.next else name end end # FIXME: use delegations and include all name functions def simple? name.simple? end def junction? name.junction? end def raw_name @raw_name || name end 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 def right *args Card.fetch(name.right, *args) unless simple? end 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 def trunk *args simple? ? self : left(*args) end def tag *args simple? ? self : Card.fetch(name.right, *args) end def left_or_new args={} left(args) || Card.new(args.merge(name: name.left)) end # NOTE: for all these helpers, method returns *all* fields/children/descendants. # (Not just those current user has permission to read.) def fields field_ids.map { |id| Card[id] } end def field_names field_ids.map { |id| Card::Name[id] } end def field_ids child_ids :left end 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 # eg, A+B is a child of A and B 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 def each_descendant &block each_child do |child| yield child child.each_descendant(&block) end end def right_id= card_or_id write_card_or_id :right_id, card_or_id end def left_id= card_or_id write_card_or_id :left_id, card_or_id end 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 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 def with_id_when_exists card, &block card.director.call_after_store(&block) end end;end;end;end; # ~~ generated from /Users/ethan/dev/decko/gem/card/mod/core/set/all/name.rb ~~