Class: Yoda::Store::FileTree
- Inherits:
-
Object
- Object
- Yoda::Store::FileTree
- Defined in:
- lib/yoda/store/file_tree.rb
Defined Under Namespace
Classes: EventSet
Instance Attribute Summary collapse
- #base_path ⇒ String? readonly
Instance Method Summary collapse
- #clear_editing_at(path) ⇒ void
- #editing_at?(path) ⇒ Boolean
-
#initialize(base_path:) ⇒ FileTree
constructor
A new instance of FileTree.
- #mark_deleted(path) ⇒ void
- #normalize_path(path) ⇒ String
- #on_change {|path:, content:| ... } ⇒ Object
- #open_at(path) ⇒ Object
- #read_at(path) ⇒ String?
- #read_real_at(path) ⇒ String?
- #set_editing_at(path, content) ⇒ Object
- #subpath?(path) ⇒ Boolean
Constructor Details
#initialize(base_path:) ⇒ FileTree
Returns a new instance of FileTree.
8 9 10 |
# File 'lib/yoda/store/file_tree.rb', line 8 def initialize(base_path:) @base_path = base_path ? File.absolute_path(base_path) : nil end |
Instance Attribute Details
#base_path ⇒ String? (readonly)
5 6 7 |
# File 'lib/yoda/store/file_tree.rb', line 5 def base_path @base_path end |
Instance Method Details
#clear_editing_at(path) ⇒ void
This method returns an undefined value.
51 52 53 54 55 |
# File 'lib/yoda/store/file_tree.rb', line 51 def clear_editing_at(path) normalized_path = normalize_path(path) editing_content.delete(normalized_path) notify_changed(path: normalized_path, content: read_real_at(normalized_path)) end |
#editing_at?(path) ⇒ Boolean
45 46 47 |
# File 'lib/yoda/store/file_tree.rb', line 45 def editing_at?(path) editing_content.has_key?(normalize_path(path)) end |
#mark_deleted(path) ⇒ void
This method returns an undefined value.
59 60 61 62 63 |
# File 'lib/yoda/store/file_tree.rb', line 59 def mark_deleted(path) normalized_path = normalize_path(path) editing_content.delete(normalized_path) notify_changed(path: normalized_path, content: nil) end |
#normalize_path(path) ⇒ String
67 68 69 70 71 72 73 |
# File 'lib/yoda/store/file_tree.rb', line 67 def normalize_path(path) if base_path File.(path, base_path) else File.absolute_path(path) end end |
#on_change {|path:, content:| ... } ⇒ Object
88 89 90 |
# File 'lib/yoda/store/file_tree.rb', line 88 def on_change(&handler) changed_events.listen(&handler) end |
#open_at(path) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/yoda/store/file_tree.rb', line 12 def open_at(path) normalized_path = normalize_path(path) content = read_at(normalized_path) notify_changed(path: normalized_path, content: content) content end |
#read_at(path) ⇒ String?
21 22 23 |
# File 'lib/yoda/store/file_tree.rb', line 21 def read_at(path) editing_content[normalize_path(path)] || read_real_at(path) end |
#read_real_at(path) ⇒ String?
27 28 29 30 31 32 33 |
# File 'lib/yoda/store/file_tree.rb', line 27 def read_real_at(path) if File.file?(path) File.read(path) else nil end end |
#set_editing_at(path, content) ⇒ Object
37 38 39 40 41 |
# File 'lib/yoda/store/file_tree.rb', line 37 def set_editing_at(path, content) normalized_path = normalize_path(path) editing_content[normalized_path] = content notify_changed(path: normalized_path, content: content) end |
#subpath?(path) ⇒ Boolean
77 78 79 80 81 82 83 |
# File 'lib/yoda/store/file_tree.rb', line 77 def subpath?(path) if base_path File.fnmatch(File.join(base_path, "**/*"), path) else false end end |