Module: Gyoku::XMLKey

Defined in:
lib/gyoku/xml_key.rb

Constant Summary collapse

FORMULAS =
{
  :lower_camelcase => lambda { |key| key.lower_camelcase },
  :camelcase       => lambda { |key| key.camelcase },
  :none            => lambda { |key| key }
}

Class Method Summary collapse

Class Method Details

.create(key, options = {}) ⇒ Object

Converts a given object with options to an XML key.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gyoku/xml_key.rb', line 14

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 = symbol_converter.call(xml_key) if Symbol === key

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

  xml_key
end

.symbol_converterObject

Returns the formula for converting Symbol keys.



31
32
33
# File 'lib/gyoku/xml_key.rb', line 31

def symbol_converter
  @symbol_converter ||= FORMULAS[:lower_camelcase]
end

.symbol_converter=(formula) ⇒ Object

Sets the formula for converting Symbol keys. Accepts one of FORMULAS of an object responding to :call.

Raises:

  • (ArgumentError)


37
38
39
40
41
42
# File 'lib/gyoku/xml_key.rb', line 37

def symbol_converter=(formula)
  formula = FORMULAS[formula] unless formula.respond_to? :call
  raise ArgumentError, "Invalid symbol_converter formula" unless formula

  @symbol_converter = formula
end