Sha256: 22ce95ccd3099905b3b7b3cf6a5ccbd20e56d0c4b60176abb7d76eee50dd163c

Contents?: true

Size: 869 Bytes

Versions: 1

Compression:

Stored size: 869 Bytes

Contents

module Hexp
  class Array < ::Array
    def self.[](*args)
      super(*normalize(args))
    end

    def self.normalize(args)
      idx = 0
      case args[idx]
      when Symbol
        tag = args[idx] ; idx+=1
        attrs = case args[idx]
                when Hash
                  idx += 1 ; args[idx-1]
                else
                  {}
                end
        children = normalize_children args[idx]
        [tag, attrs, children]
      end
    end

    def self.normalize_children(children)
      case children
      when String
        [ children ]
      when ::Array
        children.map do |child|
          case child
          when String
            child
          when ::Array
            Hexp::Array[*child]
          else
            raise "bad input #{child}"
          end
        end
      else
        []
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hexp-0.0.1.pre lib/hexp/array.rb