Sha256: 5ef9d615d460b8802b7b3bb9d3c7c7f4338ef600fce9ef5dbc68116234d88b28

Contents?: true

Size: 1.66 KB

Versions: 16

Compression:

Stored size: 1.66 KB

Contents

# -*- encoding : utf-8 -*-

class Card
  class Content
    module Chunk
      class Reference < Abstract
        attr_accessor :referee_name, :name

        def referee_name
          return if name.nil?
          @referee_name ||= begin
            rendered_name = render_obj(name)
            ref_card = fetch_referee_card rendered_name
            ref_card ? ref_card.name : rendered_name.to_name
          end
          @referee_name = @referee_name.absolute(card.name).to_name
        end

        def referee_card
          @referee_card ||= referee_name && Card.fetch(referee_name)
        end

        # FIXME: if we need this, then it should be faster, using fetch_id
        # def referee_id
        #   referee_card and referee_card.id
        # end

        def replace_name_reference old_name, new_name
          @referee_card = nil
          @referee_name = nil
          if name.is_a? Card::Content
            name.find_chunks(Chunk::Reference).each do |chunk|
              chunk.replace_reference old_name, new_name
            end
          else
            @name = name.to_name.swap old_name, new_name
          end
        end

        def render_obj raw
          if format && raw.is_a?(Card::Content)
            format.process_content raw
          else
            raw
          end
        end

        private

        def fetch_referee_card rendered_name
          case rendered_name # FIXME: this should be standard fetch option.
          when /^\~(\d+)$/ # get by id
            Card.fetch Regexp.last_match(1).to_i
          when /^\:(\w+)$/ # get by codename
            Card.fetch Regexp.last_match(1).to_sym
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
card-1.94.1 mod/core/chunk/reference.rb
card-1.94.0 mod/core/chunk/reference.rb
card-1.93.13 mod/core/chunk/reference.rb
card-1.93.12 mod/core/chunk/reference.rb
card-1.93.11 mod/core/chunk/reference.rb
card-1.93.10 mod/core/chunk/reference.rb
card-1.93.9 mod/core/chunk/reference.rb
card-1.93.8 mod/core/chunk/reference.rb
card-1.93.7 mod/core/chunk/reference.rb
card-1.93.6 mod/core/chunk/reference.rb
card-1.93.5 mod/core/chunk/reference.rb
card-1.93.4 mod/core/chunk/reference.rb
card-1.93.3 mod/core/chunk/reference.rb
card-1.93.2 mod/core/chunk/reference.rb
card-1.93.1 mod/core/chunk/reference.rb
card-1.93.0 mod/core/chunk/reference.rb