Class: Utilities

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#model_directoryObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/pry_utilities.rb', line 47

def model_directory
  return @model_directory if @model_directory
  if File.exists?(user_directory)
    FileUtils.mkdir(user_model_directory) unless File.exists?(user_model_directory)
    @model_directory = user_model_directory
  elsif File.exists?("/tmp")
    @model_directory = File.expand_path("/tmp") if File.exists?("/tmp")
  end
  @model_directory
end

#tmp_directoryObject



64
65
66
# File 'lib/pry_utilities.rb', line 64

def tmp_directory
  @tmp_directory ||= "/tmp"
end

Instance Method Details

#edit_modelsObject



21
22
23
24
25
26
27
# File 'lib/pry_utilities.rb', line 21

def edit_models
  raise "Directory does not exist" unless File.exist?(model_directory)
  raise "ENV['EDITOR'] not set" unless ENV['EDITOR']
  `#{ENV['EDITOR']} #{model_directory}`
  puts "The models are open in your editor, but you will need to load them again after completing your work."
  true
end

#get_note(type = "md") ⇒ Object

Great for writing descriptions without messing around with quotes and escapes and things TODO: Make this work for several sessions. (Thread it?)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pry_utilities.rb', line 72

def get_note(type="md")
  contents = nil
  begin
    filename = File.join(tmp_directory, "#{self.object_id}.#{type}")
    i = 0
    while File.exist?(filename)
      filename = File.join(tmp_directory, "#{self.object_id}#{i}.#{type}")
      i += 1
    end
    raise "ENV['EDITOR'] not set" unless ENV['EDITOR']
    `#{ENV['EDITOR']} #{filename}`
    contents = File.read(filename)
  ensure
    puts "Cleaning up temp file and exiting ..."
    FileUtils.rm_f(filename)
  end
end

#list_modelsObject



59
60
61
62
# File 'lib/pry_utilities.rb', line 59

def list_models
  Dir.glob("#{model_directory}/*.rb").map { |file| File.basename(file).split('.')[0..-2].join('.').to_sym}
  true
end

#load_model(name) ⇒ Object



29
30
31
32
33
# File 'lib/pry_utilities.rb', line 29

def load_model(name)
  raise "Directory does not exist" unless File.exist?(model_directory)
  filename = File.join(model_directory, "#{name}.rb")
  load filename
end

#load_modelsObject



35
36
37
# File 'lib/pry_utilities.rb', line 35

def load_models
  Dir.glob("#{model_directory}/*.rb").map { |filename| load filename}
end

#user_directoryObject



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

def user_directory
  File.expand_path("~/.gearbox")
end

#user_model_directoryObject



43
44
45
# File 'lib/pry_utilities.rb', line 43

def user_model_directory
  @user_model_directory ||= File.join(user_directory, "models")
end

#write_model(name) ⇒ Object Also known as: update_model, build_model

Great for writing ad hoc models. TODO: Make this work for several sessions. (Thread it?)



11
12
13
14
15
16
17
# File 'lib/pry_utilities.rb', line 11

def write_model(name)
  raise "Directory does not exist" unless File.exist?(model_directory)
  filename = File.join(model_directory, "#{name}.rb")
  raise "ENV['EDITOR'] not set" unless ENV['EDITOR']
  `#{ENV['EDITOR']} #{filename}`
  load filename
end