Module Ambling::Base
In: lib/ambling/base.rb

All settings classses include these base functions that populate the data from a hash and generate the xml

Methods

build_xml   new   populate   to_xml  

Public Class methods

[Source]

# File lib/ambling/base.rb, line 25
    def initialize(hash = {})
      populate(hash)
    end

Public Instance methods

build an xml representation of this subcomponent of the settings using the provided builder

[Source]

# File lib/ambling/base.rb, line 65
    def build_xml(builder)
      self.class.send(:const_get, :VALUES).each do |a|
        val = self.send(a)
        if val.respond_to? :build_xml
          builder.tag!(a) {|b| val.build_xml(b) }
        elsif val.is_a? Array
          val.each {|v| builder << v.to_xml }
        elsif not val.nil?
          builder.tag! a, val
        end
      end
    end

populate the settings with data from the hash

[Source]

# File lib/ambling/base.rb, line 32
    def populate(hash = {})
      hash.each do |k,v|
        val = if v.is_a?(Hash)
          klass = self.class.const_get(k.to_s.camelize)
          klass.new(v)
        elsif v.is_a?(Array)
          klass = self.class.const_get(k.to_s.camelize)
          v.collect {|i| klass.new(i)}
        else
          v
        end
        self.send("#{k}=", val)
      end
    end

Return an xml representation of these settings

[Source]

# File lib/ambling/base.rb, line 50
    def to_xml
      builder = Builder::XmlMarkup.new
      tag = self.class.to_s.split("::").last.downcase
      attr_list = self.class.send(:const_get, :ATTRIBUTES) rescue []
      attrs = attr_list.inject({}) do |h,a|
        val = self.send(a)
        val.nil? ? h : h.merge(a => val)
      end
      builder.tag!(tag, attrs) { self.build_xml(builder) }
      builder.target!
    end

[Validate]