File: generator.rb

package info (click to toggle)
ruby-iniparse 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 280 kB
  • sloc: ruby: 2,469; makefile: 4
file content (204 lines) | stat: -rw-r--r-- 5,909 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
module IniParse
  # Generator provides a means for easily creating new INI documents.
  #
  # Rather than trying to hack together new INI documents by manually creating
  # Document, Section and Option instances, it is preferable to use Generator
  # which will handle it all for you.
  #
  # The Generator is exposed through IniParse.gen.
  #
  #   IniParse.gen do |doc|
  #     doc.section("vehicle") do |vehicle|
  #       vehicle.option("road_side", "left")
  #       vehicle.option("realistic_acceleration", true)
  #       vehicle.option("max_trains", 500)
  #     end
  #
  #     doc.section("construction") do |construction|
  #       construction.option("build_on_slopes", true)
  #       construction.option("autoslope", true)
  #     end
  #   end
  #
  #   # => IniParse::Document
  #
  # This can be simplified further if you don't mind the small overhead
  # which comes with +method_missing+:
  #
  #   IniParse.gen do |doc|
  #     doc.vehicle do |vehicle|
  #       vehicle.road_side = "left"
  #       vehicle.realistic_acceleration = true
  #       vehicle.max_trains = 500
  #     end
  #
  #     doc.construction do |construction|
  #       construction.build_on_slopes = true
  #       construction.autoslope = true
  #     end
  #   end
  #
  #   # => IniParse::Document
  #
  # If you want to add slightly more complicated formatting to your document,
  # each line type (except blanks) takes a number of optional parameters:
  #
  # :comment::
  #   Adds an inline comment at the end of the line.
  # :comment_offset::
  #   Indent the comment. Measured in characters from _beginning_ of the line.
  #   See String#ljust.
  # :indent::
  #   Adds the supplied text to the beginning of the line.
  #
  # If you supply +:indent+, +:comment_sep+, or +:comment_offset+ options when
  # adding a section, the same options will be inherited by all of the options
  # which belong to it.
  #
  #   IniParse.gen do |doc|
  #     doc.section("vehicle",
  #       :comment => "Options for vehicles", :indent  => "    "
  #     ) do |vehicle|
  #       vehicle.option("road_side", "left")
  #       vehicle.option("realistic_acceleration", true)
  #       vehicle.option("max_trains", 500, :comment => "More = slower")
  #     end
  #   end.to_ini
  #
  #       [vehicle] ; Options for vehicles
  #       road_side = left
  #       realistic_acceleration = true
  #       max_trains = 500 ; More = slower
  #
  class Generator
    attr_reader :context
    attr_reader :document

    def initialize(opts = {}) # :nodoc:
      @document   = IniParse::Document.new
      @context    = @document

      @in_section = false
      @opt_stack  = [opts]
    end

    def gen # :nodoc:
      yield self
      @document
    end

    # Creates a new IniParse::Document with the given sections and options.
    #
    # ==== Returns
    # IniParse::Document
    #
    def self.gen(opts = {}, &blk)
      new(opts).gen(&blk)
    end

    # Creates a new section with the given name and adds it to the document.
    #
    # You can optionally supply a block (as detailed in the documentation for
    # Generator#gen) in order to add options to the section.
    #
    # ==== Parameters
    # name<String>:: A name for the given section.
    #
    def section(name, opts = {})
      if @in_section
        # Nesting sections is bad, mmmkay?
        raise LineNotAllowed, "You can't nest sections in INI files."
      end

      # Add to a section if it already exists
      if @document.has_section?(name.to_s())
        @context = @document[name.to_s()]
      else
        @context = Lines::Section.new(name, line_options(opts))
        @document.lines << @context
      end

      if block_given?
        begin
          @in_section = true
          with_options(opts) { yield self }
          @context = @document
          blank()
        ensure
          @in_section = false
        end
      end
    end

    # Adds a new option to the current section.
    #
    # Can only be called as part of a section block, or after at least one
    # section has been added to the document.
    #
    # ==== Parameters
    # key<String>:: The key (name) for this option.
    # value::       The option's value.
    # opts<Hash>::  Extra options for the line (formatting, etc).
    #
    # ==== Raises
    # IniParse::NoSectionError::
    #   If no section has been added to the document yet.
    #
    def option(key, value, opts = {})
      @context.lines << Lines::Option.new(
        key, value, line_options(opts)
      )
    rescue LineNotAllowed
      # Tried to add an Option to a Document.
      raise NoSectionError,
        'Your INI document contains an option before the first section is ' \
        'declared which is not allowed.'
    end

    # Adds a new comment line to the document.
    #
    # ==== Parameters
    # comment<String>:: The text for the comment line.
    #
    def comment(comment, opts = {})
      @context.lines << Lines::Comment.new(
        line_options(opts.merge(:comment => comment))
      )
    end

    # Adds a new blank line to the document.
    def blank
      @context.lines << Lines::Blank.new
    end

    # Wraps lines, setting default options for each.
    def with_options(opts = {}) # :nodoc:
      opts = opts.dup
      opts.delete(:comment)
      @opt_stack.push( @opt_stack.last.merge(opts))
      yield self
      @opt_stack.pop
    end

    def method_missing(name, *args, &blk) # :nodoc:
      if m = name.to_s.match(/(.*)=$/)
        option(m[1], *args)
      else
        section(name.to_s, *args, &blk)
      end
    end

    #######
    private
    #######

    # Returns options for a line.
    #
    # If the context is a section, we use the section options as a base,
    # rather than the global defaults.
    #
    def line_options(given_opts) # :nodoc:
      @opt_stack.last.empty? ? given_opts : @opt_stack.last.merge(given_opts)
    end
  end
end