Class: Reservoir::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/reservoir/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Application

Returns a new instance of Application.



9
10
11
12
13
# File 'lib/reservoir/application.rb', line 9

def initialize(data = {})
  @display = data[:display] || :stdio
  @file_resets = []
  @version_displayed_already = false
end

Instance Attribute Details

#displayObject

Returns the value of attribute display.



7
8
9
# File 'lib/reservoir/application.rb', line 7

def display
  @display
end

Instance Method Details

#exception_message(error) ⇒ Object



29
30
31
# File 'lib/reservoir/application.rb', line 29

def exception_message(error)
  print "ERROR: #{error.message}\n\n  #{error.backtrace.join('\n  ')}"
end

#flushObject



104
105
106
107
108
109
110
# File 'lib/reservoir/application.rb', line 104

def flush
  return if @file_resets.include?(display)
  return unless is_file?
  @version_displayed_already = false
  @file_resets<< display
  File.delete(@display[:filename]) if File.exist?(@display[:filename])
end

#interactive_messageObject



41
42
43
# File 'lib/reservoir/application.rb', line 41

def interactive_message
  print "Welcome to reservoir interactive shell"
end

#interactive_quit_messageObject



45
46
47
# File 'lib/reservoir/application.rb', line 45

def interactive_quit_message
  print "Goodbye"
end


112
113
114
115
116
117
118
119
120
121
# File 'lib/reservoir/application.rb', line 112

def print(output)
  if @display == :stdio
    STDOUT.puts output
  elsif is_file?
    system("mkdir -p #{Pathname.new(@display[:filename]).parent}")
    open(@display[:filename], 'a+') { |f| f.puts(output) }
  else
    output
  end
end

#project_message(project) ⇒ Object



33
34
35
# File 'lib/reservoir/application.rb', line 33

def project_message(project)
  print "server: #{project.name}\nfile: #{project.file}\n"
end

#run(args) ⇒ Object



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
# File 'lib/reservoir/application.rb', line 49

def run(args)
  
  if args.nil? || args.empty?
    usage_message
    return
  end
  
  if "-i" == args[0]
    interactive_message
    v = Version.new
    loop do
      script = user_input
      if script == "quit" || script == "exit"
        interactive_quit_message
        return
      end
      v.go(script)
      print v.version
    end
    return
  end

  if ["--version","-version","-v","--v","version"].include?(args[0])
    version_message
    return
  end
  
  if ["--help","-help","-h","--h","help"].include?(args[0])
    version_message
    usage_message
    return
  end

  begin
    args.each do |filename|
      all_projects = Project.load_from_file(filename)
      all_projects.each do |p|
        self.display = p.display
        self.flush
        version_message
        project_message(p) unless is_data_only?
        p.scripts.each do |script|
          which_script = p.template(WhichScript)
          version = p.template(Version)
          which_script.go(script)
          version.go(script)
          which_version_message(script,version.version,which_script.response)
        end
      end
    end
  rescue
    exception_message($!)
  end
end

#usage_messageObject



25
26
27
# File 'lib/reservoir/application.rb', line 25

def usage_message
  print "USAGE: reservoir <project_file1> <project_file2> ...\n   or  reservoir help to see this message\n"
end

#user_inputObject



123
124
125
# File 'lib/reservoir/application.rb', line 123

def user_input
  STDIN.gets.chomp
end

#version_messageObject



19
20
21
22
23
# File 'lib/reservoir/application.rb', line 19

def version_message
  return nil if @version_displayed_already
  @version_displayed_already = true
  print "reservoir, version #{Reservoir::VERSION}\n"
end

#which_version_message(script, version, path) ⇒ Object



37
38
39
# File 'lib/reservoir/application.rb', line 37

def which_version_message(script,version,path)
  print "#{script} : #{version} : #{path}\n"
end