Top Level Namespace

Defined Under Namespace

Classes: Processor, Splice

Constant Summary collapse

NO_CHANGE =
Object.new

Instance Method Summary collapse

Instance Method Details

#mainObject



146
147
148
149
150
# File 'lib/dy.rb', line 146

def main
  src, dst, options = read_arguments
  hash = Processor.new(source_file: src).hash
  File.write(dst, "#{options[:prefix] + "\n" if options[:prefix]}#{hash.to_yaml}")
end

#read_argumentsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dy.rb', line 128

def read_arguments
  options = {}
  OptionParser.new do |opts|
    opts.banner = "Usage: test.rb [options]"

    opts.on("-pPREFIX", "--prefix=PREFIX", "Prefix for the yaml output") do |v|
      options[:prefix] = v
    end
  end.parse!

  src, dst, *more = ARGV
  if src.nil? || dst.nil? || !more.empty?
    raise "Need two arguments"
  end

  [src, dst, options]
end

#read_file(filename, erb: false, args: {}) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/dy.rb', line 118

def read_file(filename, erb: false, args: {})
  content = File.read(filename)
  if erb
    renderer = ERB.new(content)
    renderer.result(OpenStruct.new(args).instance_eval { binding })
  else
    content
  end
end

#splice_array!(array) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dy.rb', line 104

def splice_array!(array)
  index = 0
  while index < array.length do
    value = array[index]
    if value.is_a?(Splice)
      raise "Cannot splice except arrays" unless value.array.is_a?(Array)
      array[index, 1] = value.array
      index += value.array.length
    else
      index += 1
    end
  end
end