Class: HaveAPI::Fs::Component

Inherits:
Object
  • Object
show all
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.

Defined Under Namespace

Classes: Children

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bound: false) ⇒ Component

Returns a new instance of Component.

Parameters:

  • bound (Boolean) (defaults to: false)


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

#atimeObject

Returns the value of attribute atime.



77
78
79
# File 'lib/haveapi/fs/component.rb', line 77

def atime
  @atime
end

#contextObject

Returns the value of attribute context.



77
78
79
# File 'lib/haveapi/fs/component.rb', line 77

def context
  @context
end

#ctimeObject

Returns the value of attribute ctime.



77
78
79
# File 'lib/haveapi/fs/component.rb', line 77

def ctime
  @ctime
end

#mtimeObject

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.

Parameters:

  • name (Symbol) (defaults to: nil)

Returns:

  • (nil)

    if name is set

  • (Symbol)

    if name is nil



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

#abspathString

Returns absolute path of this component from the system root.

Returns:

  • (String)

    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

Returns:

  • (Boolean)


121
122
123
# File 'lib/haveapi/fs/component.rb', line 121

def bound?
  @bound
end

#contentsObject

Raises:

  • (NotImplementedError)


149
150
151
# File 'lib/haveapi/fs/component.rb', line 149

def contents
  raise NotImplementedError
end

#directory?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/haveapi/fs/component.rb', line 129

def directory?
  !file?
end

#executable?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/haveapi/fs/component.rb', line 145

def executable?
  false
end

#file?Boolean

Returns:

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

Returns:



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

Returns:

  • (Boolean)


206
207
208
# File 'lib/haveapi/fs/component.rb', line 206

def invalid?
  @invalid
end

#invalidateObject

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

#parentObject



180
181
182
# File 'lib/haveapi/fs/component.rb', line 180

def parent
  context.object_path[-2][1]
end

#pathString

Returns path of this component in the tree without the leading /.

Returns:

  • (String)

    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

Returns:

  • (Boolean)


137
138
139
# File 'lib/haveapi/fs/component.rb', line 137

def readable?
  true
end

#resetObject

Shortcut for #drop_children and #setup.



158
159
160
161
# File 'lib/haveapi/fs/component.rb', line 158

def reset
  drop_children
  setup
end

#setupObject

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

#timesObject



153
154
155
# File 'lib/haveapi/fs/component.rb', line 153

def times
  [@atime, @mtime, @ctime]
end

#titleObject



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.

Parameters:

  • n (Integer) (defaults to: nil)

    used to determine the result just once per the same n

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


141
142
143
# File 'lib/haveapi/fs/component.rb', line 141

def writable?
  false
end