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.
- #clear_local_variable(name) ⇒ Object
-
#initialize(parent, dir) ⇒ Directory
constructor
A new instance of Directory.
- #local_method(name) ⇒ Object
- #local_method?(name) ⇒ Boolean
- #local_methods ⇒ Object
- #local_variable(name) ⇒ Object
- #local_variable?(name) ⇒ Boolean
- #local_variables ⇒ Object
- #lock_method(name) ⇒ Object
- #lock_variable(name) ⇒ Object
- #root? ⇒ Boolean
- #set_local_variable(name, value) ⇒ Object
- #to_s ⇒ Object
- #unlocked_local_methods ⇒ Object
- #unlocked_local_variable(name) ⇒ Object
- #unlocked_local_variable?(name) ⇒ Boolean
- #unlocked_local_variables ⇒ Object
Constructor Details
#initialize(parent, dir) ⇒ Directory
Returns a new instance of Directory.
100 101 102 103 104 105 106 107 108 |
# File 'lib/rash/ext/filesystem.rb', line 100 def initialize(parent, dir) @path = Dir.new(dir) @parent = parent @children = [] @local_methods = parent&.unlocked_local_methods || {} @locked_methods = [] @local_variables = {} @locked_variables = [] end |
Instance Attribute Details
#children ⇒ Object (readonly)
Returns the value of attribute children.
94 95 96 |
# File 'lib/rash/ext/filesystem.rb', line 94 def children @children end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
94 95 96 |
# File 'lib/rash/ext/filesystem.rb', line 94 def parent @parent end |
Class Method Details
Instance Method Details
#add_child(path) ⇒ Object
198 199 200 201 202 |
# File 'lib/rash/ext/filesystem.rb', line 198 def add_child(path) dir = Directory.new(self, path) @children << dir dir end |
#add_local_method(name, &block) ⇒ Object
204 205 206 207 208 |
# File 'lib/rash/ext/filesystem.rb', line 204 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
186 187 188 189 190 |
# File 'lib/rash/ext/filesystem.rb', line 186 def add_parent(dir) @parent = Directory.root(dir) @parent.add_child(self.to_s) @parent end |
#child(path) ⇒ Object
192 193 194 195 196 |
# File 'lib/rash/ext/filesystem.rb', line 192 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
211 212 213 214 |
# File 'lib/rash/ext/filesystem.rb', line 211 def clear_local_method(name) @local_methods.delete(name.to_sym) name.to_sym end |
#clear_local_variable(name) ⇒ Object
177 178 179 180 |
# File 'lib/rash/ext/filesystem.rb', line 177 def clear_local_variable(name) @local_variables.delete(name.to_sym) name.to_sym end |
#local_method(name) ⇒ Object
110 111 112 |
# File 'lib/rash/ext/filesystem.rb', line 110 def local_method(name) @local_methods[name.to_sym] end |
#local_method?(name) ⇒ Boolean
129 130 131 |
# File 'lib/rash/ext/filesystem.rb', line 129 def local_method?(name) @local_methods.key?(name.to_sym) end |
#local_methods ⇒ Object
114 115 116 |
# File 'lib/rash/ext/filesystem.rb', line 114 def local_methods @local_methods.keys end |
#local_variable(name) ⇒ Object
133 134 135 136 |
# File 'lib/rash/ext/filesystem.rb', line 133 def local_variable(name) name = name.to_sym @local_variables[name] || @parent&.unlocked_local_variable(name) end |
#local_variable?(name) ⇒ Boolean
138 139 140 |
# File 'lib/rash/ext/filesystem.rb', line 138 def local_variable?(name) @local_variables.include?(name.to_sym) || !!@parent&.unlocked_local_variable?(name.to_sym) end |
#local_variables ⇒ Object
142 143 144 |
# File 'lib/rash/ext/filesystem.rb', line 142 def local_variables @local_variables.keys + (@parent&.unlocked_local_variables).to_a end |
#lock_method(name) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/rash/ext/filesystem.rb', line 122 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 |
#lock_variable(name) ⇒ Object
170 171 172 173 174 175 |
# File 'lib/rash/ext/filesystem.rb', line 170 def lock_variable(name) n = name.to_sym raise NameError.new("#{name} is not a local variable", n) unless @local_variables.key?(n) @locked_variables << n unless @locked_variables.include?(n) n end |
#root? ⇒ Boolean
182 183 184 |
# File 'lib/rash/ext/filesystem.rb', line 182 def root? parent.nil? end |
#set_local_variable(name, value) ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/rash/ext/filesystem.rb', line 146 def set_local_variable(name, value) name = name.to_sym if !@local_variables.key?(name) && @parent&.unlocked_local_variable?(name) @parent&.set_local_variable(name, value) else @local_variables[name] = value end end |
#to_s ⇒ Object
216 217 218 |
# File 'lib/rash/ext/filesystem.rb', line 216 def to_s @path.path end |
#unlocked_local_methods ⇒ Object
118 119 120 |
# File 'lib/rash/ext/filesystem.rb', line 118 def unlocked_local_methods @local_methods.filter{|k, v| !@locked_methods.include?(k)} end |
#unlocked_local_variable(name) ⇒ Object
159 160 161 162 |
# File 'lib/rash/ext/filesystem.rb', line 159 def unlocked_local_variable(name) name = name.to_sym @local_variables.filter{|k| !@locked_variables.include?(k)}[name] || @parent&.unlocked_local_variable(name) end |
#unlocked_local_variable?(name) ⇒ Boolean
164 165 166 167 168 |
# File 'lib/rash/ext/filesystem.rb', line 164 def unlocked_local_variable?(name) name = name.to_sym @local_variables.filter{|k,_v| !@locked_variables.include?(k)}.key?(name) || !!@parent&.unlocked_local_variable?(name) end |
#unlocked_local_variables ⇒ Object
155 156 157 |
# File 'lib/rash/ext/filesystem.rb', line 155 def unlocked_local_variables @local_variables.keys.filter{|k| !@locked_variables.include?(k)} + (@parent&.unlocked_local_variables).to_a end |