Class: Textmerge::Merge

Inherits:
Object
  • Object
show all
Defined in:
lib/textmerge/merge.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Merge

Returns a new instance of Merge.



6
7
8
9
10
11
# File 'lib/textmerge/merge.rb', line 6

def initialize(options)
  @template_file = options[:template]
  @input_file = options[:input]
  @output_file = options[:output]
  # @sections = options[:sections]
end

Instance Method Details

#build_input_file(template) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/textmerge/merge.rb', line 28

def build_input_file(template)
  # Collect questions
  output = ""
  questions = get_requests(template)
  # output hash to string and send to write_file method
  questions.each do |num,question|
    output << "#{num}:#{question}\n"
  end
  output
end

#build_input_from_sectionsObject



13
14
15
16
17
18
19
20
21
# File 'lib/textmerge/merge.rb', line 13

def build_input_from_sections
  #Returns input file
  if @sections
    section_array = @sections.split(" ")
    section_array.each do |sect|

    end
  end
end

#get_requests(data) ⇒ Object



52
53
54
55
56
# File 'lib/textmerge/merge.rb', line 52

def get_requests(data)
  regex = /\{(\d{1,3})\:(\w.+?)\}/
  collection = data.scan(regex)
  array_to_hash(collection)
end

#get_responses(requests) ⇒ Object



58
59
60
61
62
63
# File 'lib/textmerge/merge.rb', line 58

def get_responses(requests)
  requests.each do |k,v|
    requests[k] = ask_basic_question(v)
  end
  requests
end

#get_responses_from_input_file(data_file = @input_file) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/textmerge/merge.rb', line 43

def get_responses_from_input_file(data_file = @input_file)
  collection = []
  File.open(data_file, 'r') do |file|
    collection = file.map { |line| line.chomp.split(':') }
  end
  # collection.sort!
  array_to_hash(collection)
end

#merge_responses(responses, data) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/textmerge/merge.rb', line 65

def merge_responses(responses,data)
  responses.each do |k,v|
    regex = /\{#{k}\:.*?\}/
    data.gsub!(regex,v)
  end
  data
end

#name_input_fileObject



39
40
41
# File 'lib/textmerge/merge.rb', line 39

def name_input_file
  @template_file.split(".txt")[0] + "-input.txt"
end

#read_templateObject



23
24
25
26
# File 'lib/textmerge/merge.rb', line 23

def read_template
  file = File.open(@template_file, "rb")
  file.read
end

#write_file(contents, filename = @output_file) ⇒ Object



73
74
75
# File 'lib/textmerge/merge.rb', line 73

def write_file(contents,filename = @output_file)
  File.open(filename, 'w') {|f| f.write(contents)}
end