Sha256: a6c3fc4e2fb45c5e102aa3d26745dbf22ea69b1d85477ea51f48c08b246a776a

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

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

# require "card/env"

require "cardname"

class Card
  # The Cardname class provides generalized of Card naming patterns
  # (compound names, key-based variants, etc)
  #
  # Card::Name adds support for deeper card integration
  class Name < Cardname
    include FieldsAndTraits
    include NameVariants

    class << self
      def [] *cardish
        cardish = cardish.first if cardish.size <= 1
        case cardish
        when Card             then cardish.name
        when Symbol, Integer  then Card.fetch_name(cardish)
        when Array            then compose cardish
        when String, NilClass then new cardish
        else
          raise ArgumentError, "#{cardish.class} not supported as name identifier"
        end
      end

      def session
        Card::Auth.current.name # also_yuck
      end

      def params
        Card::Env.params
      end

      def new str, validated_parts=nil
        return compose str if str.is_a?(Array)

        str = str.to_s

        if !validated_parts && str.include?(joint)
          string_compose Cardname.split_parts(str)
        elsif (id = Card.id_from_string str)  # handles ~[id] and :[codename]
          Card.name_from_id_from_string id, str
        else
          super str
        end
      end

      # interprets symbols/integers as codenames/ids
      def compose parts
        new_from_parts(parts) { |part| self[part] }
      end

      private

      def string_compose parts
        new_from_parts(parts) { |part| new part }
      end

      def new_from_parts parts, &block
        name_parts = parts.flatten.map(&block)
        new name_parts.join(joint), true
      end
    end

    def star?
      simple? && s[0, 1] == "*"
    end

    def rstar?
      right && right[0, 1] == "*"
    end

    def code
      Card::Codename[card_id]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
card-1.104.2 lib/card/name.rb
card-1.104.1 lib/card/name.rb
card-1.104.0 lib/card/name.rb