Class: QuickAndRuby::Data::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/quick_and_ruby/data/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(here, id: nil, parent: nil) ⇒ Context

Returns a new instance of Context.



11
12
13
14
15
# File 'lib/quick_and_ruby/data/context.rb', line 11

def initialize(here, id: nil, parent: nil)
  @here = here
  @id = id
  @parent = parent
end

Instance Attribute Details

#hereObject (readonly)

Returns the value of attribute here.



9
10
11
# File 'lib/quick_and_ruby/data/context.rb', line 9

def here
  @here
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/quick_and_ruby/data/context.rb', line 9

def id
  @id
end

#parentObject (readonly)

Returns the value of attribute parent.



9
10
11
# File 'lib/quick_and_ruby/data/context.rb', line 9

def parent
  @parent
end

Instance Method Details

#cat(path) ⇒ Object



39
40
41
# File 'lib/quick_and_ruby/data/context.rb', line 39

def cat(path)
  item_at(path)
end

#cd(path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/quick_and_ruby/data/context.rb', line 43

def cd(path)
  if path == '..'
    parent
  else
    item = item_at(path)
    if item.nil?
      nil
    else
      Context.new(item, id: path, parent: self)
    end
  end
end

#completions(input) ⇒ Object



56
57
58
# File 'lib/quick_and_ruby/data/context.rb', line 56

def completions(input)
  to_s.split(/\s+/).grep(/^#{input}/)
end

#pwdObject



29
30
31
32
33
34
35
36
37
# File 'lib/quick_and_ruby/data/context.rb', line 29

def pwd
  pwd_parts = []
  pwd_cur = self
  while pwd_cur
    pwd_parts.unshift pwd_cur.id if pwd_cur.id
    pwd_cur = pwd_cur.parent
  end
  "/" + pwd_parts.join('/')
end

#to_sObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/quick_and_ruby/data/context.rb', line 17

def to_s
  if here.is_a? Array
    indices = []
    here.each_index { |i| indices << i }
    indices.join(' ')
  elsif here.is_a? Hash
    here.keys.join(' ')
  else
    here.to_s
  end
end