Class: HaveAPI::Fs::Component
- Inherits:
-
Object
- Object
- HaveAPI::Fs::Component
- Defined in:
- lib/haveapi/fs/component.rb
Overview
The basic building block of the file system. Every directory and file is represented by a subclass of this class.
Direct Known Subclasses
HaveAPI::Fs::Components::Directory, HaveAPI::Fs::Components::File
Defined Under Namespace
Classes: Children
Instance Attribute Summary collapse
-
#atime ⇒ Object
Returns the value of attribute atime.
-
#context ⇒ Object
Returns the value of attribute context.
-
#ctime ⇒ Object
Returns the value of attribute ctime.
-
#mtime ⇒ Object
Returns the value of attribute mtime.
Class Method Summary collapse
-
.children_reader(*args) ⇒ Object
Define reader methods for child components.
-
.component(name = nil) ⇒ nil, Symbol
Set or get a component name.
-
.inherited(subclass) ⇒ Object
Pass component name to the subclass.
Instance Method Summary collapse
-
#abspath ⇒ String
Absolute path of this component from the system root.
- #bound=(b) ⇒ Object
- #bound? ⇒ Boolean
- #contents ⇒ Object
- #directory? ⇒ Boolean
- #executable? ⇒ Boolean
- #file? ⇒ Boolean
-
#find(name) ⇒ HaveAPI::Fs::Component?
Attempt to find a child component with
name
. -
#initialize(bound: false) ⇒ Component
constructor
A new instance of Component.
- #invalid? ⇒ Boolean
-
#invalidate ⇒ Object
Mark the component and all its descendats as invalid.
- #parent ⇒ Object
-
#path ⇒ String
Path of this component in the tree without the leading /.
- #readable? ⇒ Boolean
-
#reset ⇒ Object
Shortcut for #drop_children and #setup.
-
#setup ⇒ Object
Called by Factory when the instance is prepared.
- #times ⇒ Object
- #title ⇒ Object
-
#unsaved?(n = nil) ⇒ Boolean
A component is unsaved if it or any of its descendants has been modified and not saved.
-
#use(*names) ⇒ Object
Attempt to find and use nested components with
names
. - #writable? ⇒ Boolean
Constructor Details
#initialize(bound: false) ⇒ Component
Returns a new instance of Component.
80 81 82 83 |
# File 'lib/haveapi/fs/component.rb', line 80 def initialize(bound: false) @bound = bound @atime = @mtime = @ctime = Time.now end |
Instance Attribute Details
#atime ⇒ Object
Returns the value of attribute atime.
77 78 79 |
# File 'lib/haveapi/fs/component.rb', line 77 def atime @atime end |
#context ⇒ Object
Returns the value of attribute context.
77 78 79 |
# File 'lib/haveapi/fs/component.rb', line 77 def context @context end |
#ctime ⇒ Object
Returns the value of attribute ctime.
77 78 79 |
# File 'lib/haveapi/fs/component.rb', line 77 def ctime @ctime end |
#mtime ⇒ Object
Returns the value of attribute mtime.
77 78 79 |
# File 'lib/haveapi/fs/component.rb', line 77 def mtime @mtime end |
Class Method Details
.children_reader(*args) ⇒ Object
Define reader methods for child components.
50 51 52 53 54 |
# File 'lib/haveapi/fs/component.rb', line 50 def children_reader(*args) args.each do |arg| define_method(arg) { children[arg] } end end |
.component(name = nil) ⇒ nil, Symbol
Set or get a component name. Component name is used for finding components within a HaveAPI::Fs::Context.
62 63 64 65 66 67 68 69 |
# File 'lib/haveapi/fs/component.rb', line 62 def component(name = nil) if name @component = name else @component end end |
.inherited(subclass) ⇒ Object
Pass component name to the subclass.
72 73 74 |
# File 'lib/haveapi/fs/component.rb', line 72 def inherited(subclass) subclass.component(@component) end |
Instance Method Details
#abspath ⇒ String
Returns absolute path of this component from the system root.
173 174 175 176 177 178 |
# File 'lib/haveapi/fs/component.rb', line 173 def abspath File.join( context.mountpoint, path ) end |
#bound=(b) ⇒ Object
125 126 127 |
# File 'lib/haveapi/fs/component.rb', line 125 def bound=(b) @bound = b end |
#bound? ⇒ Boolean
121 122 123 |
# File 'lib/haveapi/fs/component.rb', line 121 def bound? @bound end |
#contents ⇒ Object
149 150 151 |
# File 'lib/haveapi/fs/component.rb', line 149 def contents raise NotImplementedError end |
#directory? ⇒ Boolean
129 130 131 |
# File 'lib/haveapi/fs/component.rb', line 129 def directory? !file? end |
#executable? ⇒ Boolean
145 146 147 |
# File 'lib/haveapi/fs/component.rb', line 145 def executable? false end |
#file? ⇒ Boolean
133 134 135 |
# File 'lib/haveapi/fs/component.rb', line 133 def file? !directory? end |
#find(name) ⇒ HaveAPI::Fs::Component?
Attempt to find a child component with name
.
95 96 97 98 99 100 |
# File 'lib/haveapi/fs/component.rb', line 95 def find(name) return @children[name] if @children.has_key?(name) c = new_child(name) @children.set(name, Factory.create(context, name, *c)) if c end |
#invalid? ⇒ Boolean
206 207 208 |
# File 'lib/haveapi/fs/component.rb', line 206 def invalid? @invalid end |
#invalidate ⇒ Object
Mark the component and all its descendats as invalid. Invalid components can still be in the cache and are dropped on hit.
200 201 202 203 204 |
# File 'lib/haveapi/fs/component.rb', line 200 def invalidate @invalid = true children.each { |_, c| c.invalidate } end |
#parent ⇒ Object
180 181 182 |
# File 'lib/haveapi/fs/component.rb', line 180 def parent context.object_path[-2][1] end |
#path ⇒ String
Returns path of this component in the tree without the leading /.
168 169 170 |
# File 'lib/haveapi/fs/component.rb', line 168 def path context.file_path.join('/') end |
#readable? ⇒ Boolean
137 138 139 |
# File 'lib/haveapi/fs/component.rb', line 137 def readable? true end |
#reset ⇒ Object
Shortcut for #drop_children and #setup.
158 159 160 161 |
# File 'lib/haveapi/fs/component.rb', line 158 def reset drop_children setup end |
#setup ⇒ Object
Called by Factory when the instance is prepared. Subclasses must call this method.
87 88 89 |
# File 'lib/haveapi/fs/component.rb', line 87 def setup @children = Children.new(context) end |
#times ⇒ Object
153 154 155 |
# File 'lib/haveapi/fs/component.rb', line 153 def times [@atime, @mtime, @ctime] end |
#title ⇒ Object
163 164 165 |
# File 'lib/haveapi/fs/component.rb', line 163 def title self.class.name end |
#unsaved?(n = nil) ⇒ Boolean
A component is unsaved if it or any of its descendants has been modified and not saved.
189 190 191 192 193 194 195 196 |
# File 'lib/haveapi/fs/component.rb', line 189 def unsaved?(n = nil) return @is_unsaved if n && @last_unsaved == n child = @children.detect { |_, c| c.unsaved? } @last_unsaved = n @is_unsaved = !child.nil? end |
#use(*names) ⇒ Object
Attempt to find and use nested components with names
. Each name is for
the next descendant. If the target component is found, it and all
components in its path will be bound. Bound components are not
automatically deleted when not in use.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/haveapi/fs/component.rb', line 106 def use(*names) ret = self path = [] names.each do |n| ret = ret.find(n) return if ret.nil? path << ret end path.each { |c| c.bound = true } ret end |
#writable? ⇒ Boolean
141 142 143 |
# File 'lib/haveapi/fs/component.rb', line 141 def writable? false end |