Class: Ambling::Utils::SettingsSection

Inherits:
Object
  • Object
show all
Defined in:
lib/ambling/utils.rb

Overview

Stores information about the settings as derived from the XML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, attributes = []) ⇒ SettingsSection

Returns a new instance of SettingsSection.



11
12
13
14
15
16
# File 'lib/ambling/utils.rb', line 11

def initialize(key, attributes=[])
  @key = key
  @comment = ""
  @values = ActiveSupport::OrderedHash.new
  @attributes = attributes
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



9
10
11
# File 'lib/ambling/utils.rb', line 9

def attributes
  @attributes
end

#commentObject

Returns the value of attribute comment.



9
10
11
# File 'lib/ambling/utils.rb', line 9

def comment
  @comment
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/ambling/utils.rb', line 9

def key
  @key
end

#valuesObject

Returns the value of attribute values.



9
10
11
# File 'lib/ambling/utils.rb', line 9

def values
  @values
end

Instance Method Details

#indent(str, n) ⇒ Object

Indent the str n spaces



47
48
49
# File 'lib/ambling/utils.rb', line 47

def indent(str, n)
  str.split(/\n/).collect {|s| spaces(n) + s}.join("\n")
end

#inspectObject



18
19
20
# File 'lib/ambling/utils.rb', line 18

def inspect
  "<SettingsSection [:key => #{@key}, :comment => #{@comment.inspect}, :values => #{@values.inspect}, :attributes => #{@attributes.inspect}]"
end

#spaces(n) ⇒ Object

Generate n spaces



52
53
54
# File 'lib/ambling/utils.rb', line 52

def spaces(n)
  " " * n
end

#to_class_s(indent = 0) ⇒ Object

Generate the class code



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ambling/utils.rb', line 23

def to_class_s(indent = 0)
  cdef = "\n#\n"
  cdef << @comment.split(/\n/).map {|l| "# #{l.strip}"}.join("\n") << "\n"
  cdef << "#\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"
    cbody << v.comment.split(/\n/).map {|l| "# #{l}"}.join("\n") << "\n"
    cbody << "#\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