Module: Card::Set::All::References

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

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, process_base_module_list, process_base_modules, register_set, register_set_format, shortname, view, write_tmp_file

Instance Method Details

#extended_referencersObject



9
10
11
12
# File 'tmpsets/set/mod001-01_core/all/references.rb', line 9

def extended_referencers
  # FIXME .. we really just need a number here.
  (dependents + [self]).map(&:referencers).flatten.uniq
end

#includeesObject



103
104
105
106
# File 'tmpsets/set/mod001-01_core/all/references.rb', line 103

def includees
  return [] unless refs = references_to.where( :ref_type => 'I' )
  refs.map { |ref| Card.fetch ref.referee_key, :new=>{} }.compact
end

#includersObject



93
94
95
96
# File 'tmpsets/set/mod001-01_core/all/references.rb', line 93

def includers
  return [] unless refs = references_from.where( :ref_type => 'I' )
  refs.map(&:referer_id).map( &Card.method(:fetch) ).compact
end

#name_referencers(link_name = nil) ⇒ Object



4
5
6
7
# File 'tmpsets/set/mod001-01_core/all/references.rb', line 4

def name_referencers link_name=nil
  link_name = link_name.nil? ? key : link_name.to_name.key
  Card.joins( :references_to ).where card_references: { referee_key: link_name }
end

#refereesObject



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

def referees
  return [] unless refs = references_to
  refs.map { |ref| Card.fetch ref.referee_key, :new=>{} }.compact
end

#referencersObject



88
89
90
91
# File 'tmpsets/set/mod001-01_core/all/references.rb', line 88

def referencers
  return [] unless refs = references_from
  refs.map(&:referer_id).map( &Card.method(:fetch) ).compact
end

#replace_references(old_name, new_name) ⇒ Object

replace references in card content



16
17
18
19
20
21
22
23
24
25
26
# File 'tmpsets/set/mod001-01_core/all/references.rb', line 16

def replace_references old_name, new_name
  obj_content = Card::Content.new raw_content, card=self
  obj_content.find_chunks( Card::Chunk::Reference ).select do |chunk|
    if old_ref_name = chunk.referee_name and new_ref_name = old_ref_name.replace_part(old_name, new_name)
      chunk.referee_name = chunk.replace_reference old_name, new_name
      Card::Reference.where( :referee_key => old_ref_name.key ).update_all :referee_key => new_ref_name.key
    end
  end

  obj_content.to_s
end

#update_references(rendered_content = nil, refresh = false) ⇒ Object

update entries in reference table



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'tmpsets/set/mod001-01_core/all/references.rb', line 29

def update_references rendered_content = nil, refresh = false
  raise "update references should not be called on new cards" if id.nil?

  Card::Reference.delete_all_from self

  # FIXME: why not like this: references_expired = nil # do we have to make sure this is saved?
  #Card.update( id, :references_expired=>nil )
  #  or just this and save it elsewhere?
  #references_expired=nil

  Card.connection.execute("update cards set references_expired=NULL where id=#{id}")
#  references_expired = nil
  expire if refresh

  rendered_content ||= Card::Content.new(raw_content, card=self)
  rendered_content.find_chunks(Card::Chunk::Reference).each do |chunk|
    if referee_name = chunk.referee_name # name is referenced (not true of commented inclusions)
      referee_id = chunk.referee_id
      if id != referee_id               # not self reference

        #update_references chunk.referee_name if Card::Content === chunk.referee_name
        # for the above to work we will need to get past delete_all!
        referee_name.piece_names.each do |name|
          if name.key != key # don't create self reference

            # reference types:
            # L = link
            # I = inclusion
            # P = partial (i.e. the name is part of a compound name that is referenced by a link or inclusion)

            # The partial type is needed to keep track of references of virtual cards.
            # For example a link [[A+*self]] won't make it to the reference table because A+*self is virtual and
            # doesn't have an id but when A's name is changed we have to find and update that link.
            ref_type =
              if name == referee_name
                case chunk
                when Card::Chunk::Link then 'L'
                when Card::Chunk::QueryReference then 'Q'
                else 'I'
                end
              else 'P'
              end
            Card::Reference.create!(
              :referer_id  => id,
              :referee_id  => Card.where(:key=>name.key).pluck(:id).first,
              :referee_key => name.key,
              :ref_type    => ref_type,
              :present     => 1
            )
          end
        end
      end

    end
  end
end