Class: QuickAndRuby::Data::Context
- Inherits:
-
Object
- Object
- QuickAndRuby::Data::Context
- Defined in:
- lib/quick_and_ruby/data/context.rb
Instance Attribute Summary collapse
-
#here ⇒ Object
readonly
Returns the value of attribute here.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
- #cat(path) ⇒ Object
- #cd(path) ⇒ Object
- #completions(input) ⇒ Object
-
#initialize(here, id: nil, parent: nil) ⇒ Context
constructor
A new instance of Context.
- #pwd ⇒ Object
- #to_s ⇒ Object
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
#here ⇒ Object (readonly)
Returns the value of attribute here.
9 10 11 |
# File 'lib/quick_and_ruby/data/context.rb', line 9 def here @here end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/quick_and_ruby/data/context.rb', line 9 def id @id end |
#parent ⇒ Object (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 |
#pwd ⇒ Object
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_s ⇒ Object
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 |