Sha256: 9e3a60b079e4ce6bc9f936ca8a85cc31c5833673f1a8a33b435d9bbdc6afb178

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

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

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

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

      if opts.has_key? :attrs
        ActiveSupport::Deprecation.warn(":as => {:attrs} is going away in 3.0.  Use explicit :key and :value instead.")
        @key   = to_hash_args(opts, :from => "@#{opts[:attrs][0]}")
        @value = to_hash_args(opts, :from => "@#{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
        if opts[what].keys.one?
          ActiveSupport::Deprecation.warn(":as => {:key => {Type => 'name'} ... } is going away in 3.0.  Use explicit :key => {:from => 'name', :as => Type} instead.")
          type = opts[what].keys.first
          case type
          when :attr
            {:from => "@#{opts[what][type]}"}
          when :text
            {:from => opts[what][type]}
          else
            {:as => type, :from => opts[what][type]}
          end
        else
          opts[what]
        end
      when String, Symbol
        {:from => opts[what]}
      else
        raise ArgumentError, "unrecognized hash parameter: #{what} => #{opts[what]}"
      end
    end

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

      if args.one? && !(args.first.keys & HASH_KEYS).empty?
        Definition.new(nil, opts)
      else
        opts = args.extract_options!
        raise opts.inspect
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
Empact-roxml-2.5.1 lib/roxml/hash_definition.rb
roxml-2.5.1 lib/roxml/hash_definition.rb
roxml-2.5.0 lib/roxml/hash_definition.rb
roxml-2.5.3 lib/roxml/hash_definition.rb
roxml-2.5.2 lib/roxml/hash_definition.rb