Sha256: 711d6a9505eb6d9d0d8e39f780e1bc25c387f4e21c4058d0485ae607a121c00d

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module DMAPParser
  # The TagContainer class is a Tag which contains other Tags
  class TagContainer < Tag
    def initialize(type, value = [])
      super
    end

    def inspect(level = 0)
      "#{'  ' * level}#{type}:\n" + value.reduce('') do |a, e|
        a + e.inspect(level + 1).chomp + "\n"
      end
    end

    def get_value(key)
      return value[key] if key.is_a? Fixnum
      tag = get_tag(key)
      return unless tag
      tag.type.container? ? tag : tag.value
    end

    def get_tag(key)
      key = key.to_s
      value.find { |e| e.type.tag == key || e.type.name == key }
    end

    alias_method :[], :get_value

    def has?(key)
      !get_tag(key).nil?
    end

    def method_missing(method, *arguments, &block)
      if method =~ /(.*)\?$/
        has?(Regexp.last_match[1])
      elsif has?(method)
        get_value(method)
      else
        nil
      end
    end

    def respond_to?(method)
      method.to_s.match(/(.*)\??$/)
      has?(Regexp.last_match[1]) || super
    end

    def to_a
      value
    end

    private

    def convert_value(value)
      value.reduce('') { |a, e| a + e.to_dmap }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dmapparser-0.1.0 lib/dmapparser/tag_container.rb