Sha256: 2e8e704b059b48105f47620759c3ce54d20e10e7014d297d86f1cc91d1084cc5

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

class Card
  class Format
    module ContextNames
      def context_names
        @context_names ||= initial_context_names
      end

      def initial_context_names
        @initial_context_names ||= relevant_context_names do
          parent ? parent.context_names : context_names_from_params
        end
      end

      def relevant_context_names
        part_keys = @card.name.part_names.map(&:key)
        yield.select { |n| part_keys.include? n.key }
      end

      # "slot[name_context]" param is a string;
      # @context_names is an array
      def context_names_from_params
        return [] unless (name_list = Card::Env.slot_opts.delete(:name_context))

        name_list.to_s.split(",").map(&:to_name)
      end

      def context_names_to_params
        return if context_names.empty?

        context_names.join(",")
      end

      def add_name_context name=nil
        name ||= card.name
        @context_names = (context_names + name.to_name.part_names).uniq
      end

      def title_in_context title=nil
        keep_safe = title&.html_safe?
        title = title ? title.to_name.absolute_name(card.name) : card.name
        newtitle = title.from(*context_names)
        keep_safe ? newtitle.html_safe : newtitle
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
card-1.97.0.1 lib/card/format/context_names.rb
card-1.97.0 lib/card/format/context_names.rb