Class: Environment::Directory
- Inherits:
-
Object
- Object
- Environment::Directory
- Defined in:
- lib/rash/ext/filesystem.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #add_child(path) ⇒ Object
- #add_local_method(name, &block) ⇒ Object
- #add_parent(dir) ⇒ Object
- #child(path) ⇒ Object
-
#clear_local_method(name) ⇒ Object
might not be useful.
-
#initialize(parent, dir) ⇒ Directory
constructor
A new instance of Directory.
- #local_method(name) ⇒ Object
- #local_method?(name) ⇒ Boolean
- #local_methods ⇒ Object
- #lock_method(name) ⇒ Object
- #root? ⇒ Boolean
- #to_s ⇒ Object
- #unlocked_local_methods ⇒ Object
Constructor Details
#initialize(parent, dir) ⇒ Directory
Returns a new instance of Directory.
80 81 82 83 84 85 86 |
# File 'lib/rash/ext/filesystem.rb', line 80 def initialize(parent, dir) @path = Dir.new(dir) @parent = parent @children = [] @local_methods = parent&.unlocked_local_methods || {} @locked_methods = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
74 75 76 |
# File 'lib/rash/ext/filesystem.rb', line 74 def children @children end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
74 75 76 |
# File 'lib/rash/ext/filesystem.rb', line 74 def parent @parent end |
Class Method Details
.root(dir) ⇒ Object
76 77 78 |
# File 'lib/rash/ext/filesystem.rb', line 76 def self.root(dir) Directory.new(nil, dir) end |
Instance Method Details
#add_child(path) ⇒ Object
127 128 129 130 131 |
# File 'lib/rash/ext/filesystem.rb', line 127 def add_child(path) dir = Directory.new(self, path) @children << dir dir end |
#add_local_method(name, &block) ⇒ Object
133 134 135 136 137 |
# File 'lib/rash/ext/filesystem.rb', line 133 def add_local_method(name, &block) raise ArgumentError.new "no method body provided" unless block_given? @local_methods[name.to_sym] = block # if name already exists, its function is overriden name.to_sym end |
#add_parent(dir) ⇒ Object
115 116 117 118 119 |
# File 'lib/rash/ext/filesystem.rb', line 115 def add_parent(dir) @parent = Directory.root(dir) @parent.add_child(self.to_s) @parent end |
#child(path) ⇒ Object
121 122 123 124 125 |
# File 'lib/rash/ext/filesystem.rb', line 121 def child(path) ch = @children.find {|c| c.to_s == path} ch = add_child(path) unless ch ch end |
#clear_local_method(name) ⇒ Object
might not be useful
140 141 142 143 |
# File 'lib/rash/ext/filesystem.rb', line 140 def clear_local_method(name) @local_methods.delete(name.to_sym) name.to_sym end |
#local_method(name) ⇒ Object
88 89 90 |
# File 'lib/rash/ext/filesystem.rb', line 88 def local_method(name) @local_methods[name.to_sym] end |
#local_method?(name) ⇒ Boolean
107 108 109 |
# File 'lib/rash/ext/filesystem.rb', line 107 def local_method?(name) @local_methods.key?(name.to_sym) end |
#local_methods ⇒ Object
92 93 94 |
# File 'lib/rash/ext/filesystem.rb', line 92 def local_methods @local_methods.keys end |
#lock_method(name) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/rash/ext/filesystem.rb', line 100 def lock_method(name) n = name.to_sym raise NameError.new("#{name} is not a local method", n) unless @local_methods.key?(n) @locked_methods << n unless @locked_methods.include?(n) n end |
#root? ⇒ Boolean
111 112 113 |
# File 'lib/rash/ext/filesystem.rb', line 111 def root? parent.nil? end |
#to_s ⇒ Object
145 146 147 |
# File 'lib/rash/ext/filesystem.rb', line 145 def to_s @path.path end |
#unlocked_local_methods ⇒ Object
96 97 98 |
# File 'lib/rash/ext/filesystem.rb', line 96 def unlocked_local_methods @local_methods.filter{|k, v| !@locked_methods.include?(k)} end |