Class: Yoda::Store::FileTree

Inherits:
Object
  • Object
show all
Defined in:
lib/yoda/store/file_tree.rb

Defined Under Namespace

Classes: EventSet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path:) ⇒ FileTree

Returns a new instance of FileTree.

Parameters:

  • base_path (String, nil)


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_pathString? (readonly)

Returns:

  • (String, nil)


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.

Parameters:

  • path (String)


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

Parameters:

  • path (String)

Returns:

  • (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.

Parameters:

  • path (String)


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

Parameters:

  • path (String)

Returns:

  • (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.expand_path(path, base_path)
  else
    File.absolute_path(path)
  end
end

#on_change {|path:, content:| ... } ⇒ Object

Yields:

  • (path:, content:)

Yield Parameters:

  • path (String)
  • content (String, nil)


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?

Parameters:

  • path (String)

Returns:

  • (String, nil)


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?

Parameters:

  • path (String)

Returns:

  • (String, nil)


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

Parameters:

  • path (String)
  • content (String)


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

Parameters:

  • path (String)

Returns:

  • (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