Sha256: f84253a165ab663e3f6f6112fe8516c8ba7014ce95a8ca8cc4dba708d222523e

Contents?: true

Size: 1.77 KB

Versions: 4

Compression:

Stored size: 1.77 KB

Contents

module Gyoku
  module XMLKey
    class << self

      CAMELCASE       = lambda { |key| key.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } }
      LOWER_CAMELCASE = lambda { |key| key[0].chr.downcase + CAMELCASE.call(key)[1..-1] }
      UPCASE          = lambda { |key| key.upcase }

      FORMULAS = {
        :lower_camelcase => lambda { |key| LOWER_CAMELCASE.call(key) },
        :camelcase       => lambda { |key| CAMELCASE.call(key) },
        :upcase          => lambda { |key| UPCASE.call(key) },
        :none            => lambda { |key| key }
      }

      # Converts a given +object+ with +options+ to an XML key.
      def create(key, options = {})
        xml_key = chop_special_characters key.to_s

        if unqualified = unqualify?(xml_key)
          xml_key = xml_key.split(":").last
        end

        xml_key = key_converter(options).call(xml_key) if Symbol === key

        if !unqualified && qualify?(options) && !xml_key.include?(":")
          xml_key = "#{options[:namespace]}:#{xml_key}"
        end

        xml_key
      end

    private

      # Returns the formula for converting Symbol keys.
      def key_converter(options)
        key_converter = options[:key_converter] || :lower_camelcase
        FORMULAS[key_converter]
      end

      # Chops special characters from the end of a given +string+.
      def chop_special_characters(string)
        ["!", "/"].include?(string[-1, 1]) ? string.chop : string
      end

      # Returns whether to remove the namespace from a given +key+.
      def unqualify?(key)
        key[0, 1] == ":"
      end

      # Returns whether to namespace all keys (elementFormDefault).
      def qualify?(options)
        options[:element_form_default] == :qualified && options[:namespace]
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
vagrant-tiktalik-0.0.3 vendor/bundle/ruby/2.0.0/gems/gyoku-1.1.1/lib/gyoku/xml_key.rb
gyoku-1.1.1 lib/gyoku/xml_key.rb
gyoku-1.1.0 lib/gyoku/xml_key.rb
gyoku-1.0.0 lib/gyoku/xml_key.rb