Sha256: 81697568461c8f01c67a9ae0a5137b9599cfaa1fbfe47d3b559edbf8f26b27c3

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require 'dmapparser/tag_definitions'
module DMAPParser
  # This class provides a DSL to create DMAP responses
  class Builder
    attr_reader :result

    def initialize
      @dmap_stack = []
    end

    def self.method_missing(method, *args, &block)
      new.send(method, *args, &block)
    end

    def method_missing(method, *args, &block)
      tag = TagDefinition[method]
      return super if tag.nil?

      if block_given? || tag.container?
        fail "Tag #{tag} is not a container type" unless tag.container?
        build_container(tag, &block)
      else
        build_tag(tag, args)
      end
    end

    private

    def build_tag(tag, args)
      if @dmap_stack.length > 0
        args = args.size > 1 ? args : args.first
        @dmap_stack.last.value << Tag.new(tag, args)
      else
        fail 'Cannot build DMAP without a valid container'
      end
    end

    def build_container(tag, &block)
      @dmap_stack << TagContainer.new(tag)
      instance_exec(&block)
      if @dmap_stack.length > 1
        cur_tag = @dmap_stack.pop
        @dmap_stack.last.value << cur_tag
      else
        @result = @dmap_stack.pop
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dmapparser-0.2.0 lib/dmapparser/builder.rb
dmapparser-0.1.0 lib/dmapparser/builder.rb