Sha256: 404a54269ec31002ba9e74091b09cdb46032ae662316bb28e397f5e4923adec8

Contents?: true

Size: 1.65 KB

Versions: 4

Compression:

Stored size: 1.65 KB

Contents

module ROXML
  HASH_KEYS = [:attrs, :key, :value].freeze
  TYPE_KEYS = [:attr, :text, :hash, :content].freeze

  class HashDefinition # :nodoc:
    attr_reader :key, :value, :wrapper

    def initialize(opts, wrapper)
      unless (invalid_keys = opts.keys - HASH_KEYS).empty?
        raise ArgumentError, "Invalid Hash description keys: #{invalid_keys.join(', ')}"
      end

      @wrapper = wrapper
      if opts.has_key? :attrs
        @key   = to_hash_args(opts, :attr, opts[:attrs][0])
        @value = to_hash_args(opts, :attr, opts[:attrs][1])
      else
        @key = to_hash_args opts, *fetch_element(opts, :key)
        @value = to_hash_args opts, *fetch_element(opts, :value)
      end
    end

  private
    def fetch_element(opts, what)
      case opts[what]
      when Hash
        raise ArgumentError, "Hash #{what} is over-specified: #{opts[what].inspect}" unless opts[what].keys.one?
        type = opts[what].keys.first
        [type, opts[what][type]]
      when :content
        [:content, opts[:name]]
      when :name
        [:name, '*']
      when String
        [:text, opts[what]]
      when Symbol
        [:text, opts[what]]
      else
        raise ArgumentError, "unrecognized hash parameter: #{what} => #{opts[what]}"
      end
    end

    def to_hash_args(args, type, name)
      args = [args] unless args.is_a? Array

      if args.one? && !(args.first.keys & HASH_KEYS).empty?
        opts = {type => name}
        if type == :content
          opts[:type] = :text
          (opts[:as] ||= []) << :content
        end
        Definition.new(name, opts)
      else
        opts = args.extract_options!
        raise opts.inspect
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
Empact-roxml-2.4.2 lib/roxml/hash_definition.rb
Empact-roxml-2.4.3 lib/roxml/hash_definition.rb
roxml-2.4.3 lib/roxml/hash_definition.rb
roxml-2.4.2 lib/roxml/hash_definition.rb