Class: Elasticshell::Scope

Inherits:
Object
  • Object
show all
Includes:
HasVerb
Defined in:
lib/elasticshell/scopes.rb

Constant Summary

Constants included from HasVerb

HasVerb::VERBS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasVerb

#verb, #verb=

Constructor Details

#initialize(path, options) ⇒ Scope

Returns a new instance of Scope.



62
63
64
65
66
67
# File 'lib/elasticshell/scopes.rb', line 62

def initialize path, options
  self.verb   = options.delete(:verb)
  self.path   = path
  self.client = options[:client]
  self.scopes = initial_scopes
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



56
57
58
# File 'lib/elasticshell/scopes.rb', line 56

def client
  @client
end

#last_refresh_atObject

Returns the value of attribute last_refresh_at.



56
57
58
# File 'lib/elasticshell/scopes.rb', line 56

def last_refresh_at
  @last_refresh_at
end

#pathObject

Returns the value of attribute path.



56
57
58
# File 'lib/elasticshell/scopes.rb', line 56

def path
  @path
end

#scopesObject

Returns the value of attribute scopes.



56
57
58
# File 'lib/elasticshell/scopes.rb', line 56

def scopes
  @scopes
end

Class Method Details

.requestsObject



58
59
60
# File 'lib/elasticshell/scopes.rb', line 58

def self.requests
  @requests ||= {}
end

Instance Method Details

#completing_scope_path_and_prefix(prefix) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/elasticshell/scopes.rb', line 134

def completing_scope_path_and_prefix prefix
  if prefix =~ %r!^/!
    if prefix =~ %r!/$!
      index = -1
      prefix_within_completing_scope = ''
    else
      index = -2
    end
    completing_scope_path            = prefix.split('/')[0..index].join('/')
    completing_scope_path            = '/' if completing_scope_path.empty?
    prefix_within_completing_scope ||= (prefix.split('/').last || '')
  else
    if prefix =~ %r!/$!
      index = -1
      prefix_within_completing_scope = ''
    else
      index = -2
    end
    completing_scope_path            = File.join(self.path, prefix.split('/')[0..index].join('/'))
    completing_scope_path            = self.path if completing_scope_path.empty?
    prefix_within_completing_scope ||= (prefix.split('/').last || '')
  end
  [completing_scope_path, prefix_within_completing_scope]
end

#completion_procObject



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
# File 'lib/elasticshell/scopes.rb', line 73

def completion_proc
  Proc.new do |shell, prefix|
    refresh if client.connected?
    case
    when Readline.line_buffer =~ /^\s*cd\s+\S*$/
      # User has typed 'cd' so we should be completing scopes only
      completing_scope_path, prefix_within_completing_scope = completing_scope_path_and_prefix(prefix)
      completing_scope = shell.scope_from_path(File.expand_path(completing_scope_path, shell.scope.path))
      completing_scope.refresh if client.connected?
      completing_scope.scopes_matching(prefix_within_completing_scope)
    when Readline.line_buffer =~ /^\s*\S*$/
      # User has started but not completed the first word in the
      # line so it must be a request available in the current
      # scope.
      requests_matching(prefix)
    when Readline.line_buffer =~ />.*$/
      # User has started to complete the name of a filesystem path
      # to redirect output to.
      Dir[prefix + '*']
    else
      # The user has finished the first word on the line so we try
      # to match to a filesystem path.
      Dir[prefix + '*']
    end
  end
end

#exists?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'lib/elasticshell/scopes.rb', line 178

def exists?
  false
end

#fetch_scopesObject



127
128
# File 'lib/elasticshell/scopes.rb', line 127

def fetch_scopes
end

#initial_scopesObject



130
131
132
# File 'lib/elasticshell/scopes.rb', line 130

def initial_scopes
  []
end

#refreshObject



116
117
118
# File 'lib/elasticshell/scopes.rb', line 116

def refresh
  refresh! unless refreshed?
end

#refresh!Object



120
121
122
123
124
125
# File 'lib/elasticshell/scopes.rb', line 120

def refresh!
  reset!
  fetch_scopes
  self.last_refresh_at = Time.now
  true
end

#refreshed?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'lib/elasticshell/scopes.rb', line 174

def refreshed?
  ! self.last_refresh_at.nil?
end

#request_namesObject



104
105
106
# File 'lib/elasticshell/scopes.rb', line 104

def request_names
  requests.keys
end

#requestsObject



100
101
102
# File 'lib/elasticshell/scopes.rb', line 100

def requests
  self.class.requests[verb] || {}
end

#requests_matching(prefix) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/elasticshell/scopes.rb', line 108

def requests_matching prefix
  if prefix.empty?
    request_names.sort
  else
    request_names.find_all { |name| name[0...prefix.length] == prefix }.sort
  end
end

#reset!Object



169
170
171
172
# File 'lib/elasticshell/scopes.rb', line 169

def reset!
  self.scopes = initial_scopes
  true
end

#scopes_matching(prefix) ⇒ Object



159
160
161
162
163
164
165
166
167
# File 'lib/elasticshell/scopes.rb', line 159

def scopes_matching prefix
  scopes.find_all do |scope|
    prefix.empty? ? true : scope[0...prefix.length] == prefix
  end.map do |scope|
    scope =~ %r!/$! ? scope : scope + '/'
  end.sort.map do |scope|
    File.join(path, scope)
  end
end

#to_sObject



69
70
71
# File 'lib/elasticshell/scopes.rb', line 69

def to_s
  self.path
end