Class: Bricolage::FileSystem

Inherits:
Object
  • Object
show all
Defined in:
lib/bricolage/filesystem.rb

Direct Known Subclasses

ScopedFileSystem

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, env) ⇒ FileSystem

Returns a new instance of FileSystem.



29
30
31
32
33
# File 'lib/bricolage/filesystem.rb', line 29

def initialize(path, env)
  @path = Pathname(path)
  @environment = env
  @subsystems = {}
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



44
45
46
# File 'lib/bricolage/filesystem.rb', line 44

def environment
  @environment
end

#pathObject (readonly)

Returns the value of attribute path.



43
44
45
# File 'lib/bricolage/filesystem.rb', line 43

def path
  @path
end

Class Method Details

.for_job_path(job_path, env) ⇒ Object



16
17
18
19
# File 'lib/bricolage/filesystem.rb', line 16

def FileSystem.for_job_path(job_path, env)
  home, subsys_id = parse_job_path(job_path)
  new(home, env).subsystem(subsys_id)
end

.for_options(home, env) ⇒ Object



12
13
14
# File 'lib/bricolage/filesystem.rb', line 12

def FileSystem.for_options(home, env)
  new(home_path(home), env)
end

.home_path(opt_path = nil) ⇒ Object



8
9
10
# File 'lib/bricolage/filesystem.rb', line 8

def FileSystem.home_path(opt_path = nil)
  Pathname(opt_path || ENV['BRICOLAGE_HOME'] || Dir.getwd)
end

.parse_job_path(job_path) ⇒ Object

job_path -> [home_path, subsys_id]



22
23
24
25
26
27
# File 'lib/bricolage/filesystem.rb', line 22

def FileSystem.parse_job_path(job_path)
  subsys_path = Pathname(job_path).realpath.parent
  return subsys_path.parent, subsys_path.basename
rescue SystemCallError => err
  raise ParameterError, "failed to access job file: #{err.message}"
end

Instance Method Details

#all_typed_pathes(type) ⇒ Object

/.TYPE



145
146
147
# File 'lib/bricolage/filesystem.rb', line 145

def all_typed_pathes(type)
  subsystems.map {|subsys| subsys.typed_pathes(type) }.flatten
end

#config_file_loaderObject



130
131
132
# File 'lib/bricolage/filesystem.rb', line 130

def config_file_loader
  ConfigLoader.new(home_path)
end

#config_pathes(name) ⇒ Object



126
127
128
# File 'lib/bricolage/filesystem.rb', line 126

def config_pathes(name)
  [ "config/#{name}", "config/#{@environment}/#{name}" ].map {|rel| root.relative(rel) }
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/bricolage/filesystem.rb', line 83

def exist?(name)
  relative(name).exist?
end

#file(name) ⇒ Object



87
88
89
# File 'lib/bricolage/filesystem.rb', line 87

def file(name)
  FileResource.new(relative(name))
end

#glob(pattern) ⇒ Object



140
141
142
# File 'lib/bricolage/filesystem.rb', line 140

def glob(pattern)
  Dir.glob("#{@path}/#{pattern}").map {|path| Pathname(path) }
end

#home_pathObject



75
76
77
# File 'lib/bricolage/filesystem.rb', line 75

def home_path
  root.path
end

#inspectObject



46
47
48
# File 'lib/bricolage/filesystem.rb', line 46

def inspect
  "\#<#{self.class} #{@path}>"
end

#job_dirObject



79
80
81
# File 'lib/bricolage/filesystem.rb', line 79

def job_dir
  scoped? ? @path : nil
end

#job_file(id) ⇒ Object



134
135
136
137
138
# File 'lib/bricolage/filesystem.rb', line 134

def job_file(id)
  path = typed_name(id, 'job')
  return path if path.exist?
  glob("#{id}.*.job").first or path
end

#parameter_file(name, type) ⇒ Object



118
119
120
# File 'lib/bricolage/filesystem.rb', line 118

def parameter_file(name, type)
  name.count('/') == 0 ? typed_file(name, type) : root.file(name)
end

#parameter_file_loaderObject



122
123
124
# File 'lib/bricolage/filesystem.rb', line 122

def parameter_file_loader
  ConfigLoader.new(home_path)
end

#relative_path(name) ⇒ Object Also known as: relative



97
98
99
100
101
102
103
104
# File 'lib/bricolage/filesystem.rb', line 97

def relative_path(name)
  path = Pathname(name)
  if path.absolute?
    path
  else
    @path + path
  end
end

#rootObject



50
51
52
# File 'lib/bricolage/filesystem.rb', line 50

def root
  self
end

#root_relative_path(rel) ⇒ Object Also known as: root_relative



91
92
93
# File 'lib/bricolage/filesystem.rb', line 91

def root_relative_path(rel)
  root.relative_path(rel)
end

#scopeObject



39
40
41
# File 'lib/bricolage/filesystem.rb', line 39

def scope
  nil
end

#scoped?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/bricolage/filesystem.rb', line 35

def scoped?
  false
end

#subdirectoriesObject



71
72
73
# File 'lib/bricolage/filesystem.rb', line 71

def subdirectories
  @path.children(true).select {|path| path.directory? }
end

#subsystem(id) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/bricolage/filesystem.rb', line 54

def subsystem(id)
  @subsystems[id.to_s] ||= begin
    unless root.relative(id).directory?
      raise ParameterError, "no such subsystem: #{id}"
    end
    ScopedFileSystem.new(root, id)
  end
end

#subsystemsObject



63
64
65
66
67
68
69
# File 'lib/bricolage/filesystem.rb', line 63

def subsystems
  root.subdirectories
      .map {|path| path.basename.to_s }
      .select {|name| /\A\w+\z/ =~ name }
      .reject {|name| name == 'config' }
      .map {|name| subsystem(name) }
end

#typed_file(name, type) ⇒ Object

typed_file(“make_master”, “sql”) -> FileResource(“$prefix/make_master.sql”)



114
115
116
# File 'lib/bricolage/filesystem.rb', line 114

def typed_file(name, type)
  FileResource.new(typed_name(name, type))
end

#typed_name(name, type) ⇒ Object

typed_name(“make_master”, “sql”) -> Pathname(“$prefix/make_master.sql”)



109
110
111
# File 'lib/bricolage/filesystem.rb', line 109

def typed_name(name, type)
  relative(name.count('.') > 0 ? name : "#{name}.#{type}")
end

#typed_pathes(type) ⇒ Object



149
150
151
# File 'lib/bricolage/filesystem.rb', line 149

def typed_pathes(type)
  @path.children.select {|path| path.file? and path.extname.to_s == ".#{type}" }
end