Class Ambling::Utils::SettingsSection
In: lib/ambling/utils.rb
Parent: Object

Stores information about the settings as derived from the XML

Methods

indent   inspect   new   spaces   to_class_s  

Attributes

attributes  [RW] 
comment  [RW] 
key  [RW] 
values  [RW] 

Public Class methods

[Source]

# File lib/ambling/utils.rb, line 11
      def initialize(key, attributes=[])
        @key = key
        @comment = ""
        @values = ActiveSupport::OrderedHash.new
        @attributes = attributes
      end

Public Instance methods

Indent the str n spaces

[Source]

# File lib/ambling/utils.rb, line 43
      def indent(str, n)
        str.split(/\n/).collect {|s| spaces(n) + s}.join("\n")
      end

[Source]

# File lib/ambling/utils.rb, line 18
      def inspect
        "<SettingsSection [:key => #{@key}, :comment => #{@comment.inspect}, :values => #{@values.inspect}, :attributes => #{@attributes.inspect}]"
      end

I must be missing something, because it feels wrong that there isn‘t an easier way to generate spaces

[Source]

# File lib/ambling/utils.rb, line 49
      def spaces(n)
        (0..n-1).inject("") {|w,n| w+= " "}
      end

Generate the class code

[Source]

# File lib/ambling/utils.rb, line 23
      def to_class_s(indent = 0)
        cdef = "\n#\n##{@comment}\n#\nclass #{key.camelize}\n"
        cbody = "\ninclude Base\n\n"
        cbody << "VALUES = [#{@values.keys.collect {|k| ':' + k}.join(',')}]\n"
        cbody << "ATTRIBUTES = [#{@attributes.collect {|k| ':' + k}.join(',')}]\n" if !@attributes.empty?
        subclasses = []
        @values.each do |k,v|
          cbody << "#\n# #{v.comment}\n#\nattr_accessor :#{k}\n\n"
          subclasses << v if not v.values.empty?
        end
        @attributes.each do |a|
          cbody << "#\n# xml attribute\n#\nattr_accessor :#{a}\n\n"
        end
        subclasses.each do |sc|
          cbody << sc.to_class_s
        end
        indent(cdef, indent) + indent(cbody, indent + 2) + indent("\nend\n", indent)
      end

[Validate]