-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconsole
More file actions
executable file
·125 lines (107 loc) · 2.34 KB
/
console
File metadata and controls
executable file
·125 lines (107 loc) · 2.34 KB
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
#!/usr/bin/env ruby
# require all the necessary files
$: << ::File.expand_path('../src', __FILE__)
require 'app'
require 'geofence'
require 'mongo_core'
# at this point bundler should be setup and the :development group
# should have been included. However, just to be sure, I'm going to
# include bundler again and require the console group.
require 'bundler/setup'
Bundler.require(:console)
# specify some sample fences to play with
#
# format:
# [
# [:lon, :lat],
# ...
# ]
fence_1 = [
[0, 0],
[3, 0],
[4, 2],
[6, 3],
[6, 6],
[5, 7],
[3, 5],
[2, 4],
[0, 1]
]
fence_2 = [
[5, 5],
[7, 5],
[3, 2],
[10, 2],
[12, 7],
[12, 10],
[15, 10],
[15, 15],
[12, 15],
[10, 18],
[8, 15],
[7, 15],
[6, 13],
[5, 10]
]
# patch pretty-print to use a smaller width for looking at our fences
class PP
class << self
alias_method :old_pp, :pp
def pp(obj, out = $>, width = 40)
old_pp(obj, out, width)
end
end
end
# include helpers to play around with Sinatra application
include Rack::Test::Methods
self.instance_eval do
@app = App.new
def app; @app; end
end
# add ability to reload console
def reload
reload_msg = '# Reloading the console...'
puts CodeRay.scan(reload_msg, :ruby).term
Pry.save_history
exec('./console')
end
# add helper method to call Geofence methods (even private ones!)
g = Class.new {
def method_missing(m, *args, &block)
Geofence.send(m, *args, &block)
end
}.new
# Add helper method to visualize fences
def print_fence(estimated_fence, grid)
visual_grid = {}
grid.each do |point|
xp, yp = *point
visual_grid[yp] ||= {}
if estimated_fence.include?(point)
visual_grid[yp][xp] = '+'
else
visual_grid[yp][xp] = '-'
end
end
visual_grid.keys.sort{|a,b| b <=> a}.each do |y|
xs = visual_grid[y]
xs.keys.sort.each do |x|
print visual_grid[y][x]
end
puts
end
nil
end
# start the console! :-)
system('clear')
welcome = <<eos
# This is my interactive playground. You can call the Sinatra methods in
# the same way you would in RSpec (get, post). There are also a couple of
# test fences defined for you (fence_1, fence_2). I also monkey-patched
# 'pp' to print short arrays nicer.
#
# Everything you would want to play with (for this project) should be in here
#
eos
puts CodeRay.scan(welcome, :ruby).term
Pry.start