Sha256: 2d687739007c817c00ae187e664b9fff0ed32b940cb35935fa7a75c581ed5d2e

Contents?: true

Size: 1.1 KB

Versions: 6

Compression:

Stored size: 1.1 KB

Contents

module Crystal
  class TagNotFound < StandardError
  end

  class Tags
    attr_reader :tags
    delegate :each, :to => :tags

    def initialize(options)
      @tags = []
      add_hash(options)
      InterpolatesTags.new(self).interpolate!
    end

    def find_by_name(name)
      name = name.to_s
      tags.find{|tag| tag.name == name} || raise(TagNotFound, %|can't find meta tag "#{name}"|)
    end

    def filter(pattern = //)
      tags.select{|tag| pattern === tag.name}
    end

    private

    def add_hash(hash, prefix = nil)
      sort_keys(hash.keys).each{|k| add(k, hash[k], prefix)}
    end

    # Sorts keys alphabetically and also bubble url to the top
    def sort_keys(keys)
      keys = keys.sort
      if index = keys.index('url')
        keys.unshift(keys.delete_at(index))
      else
        keys
      end
    end

    def add(name, value, prefix = nil)
      name = [prefix, name].reject(&:blank?).join(":")
      case value
      when Hash
        add_hash(value, name)
      when Array
        value.each{|v| add(name, v)}
      else
        tags << Tag.new(name, value)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
crystalmeta-1.0.0 lib/crystal/tags.rb
crystalmeta-0.9.4 lib/crystal/tags.rb
crystalmeta-0.9.3 lib/crystal/tags.rb
crystalmeta-0.9.2 lib/crystal/tags.rb
crystalmeta-0.9.1 lib/crystal/tags.rb
crystalmeta-0.9.0 lib/crystal/tags.rb