Class: Bricolage::FileSystem
- Inherits:
-
Object
- Object
- Bricolage::FileSystem
- Defined in:
- lib/bricolage/filesystem.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.extract_home_dirs(job_path) ⇒ Object
job_path -> [home_path, subsys_id].
- .for_option_pathes(home_path, job_path, env) ⇒ Object
Instance Method Summary collapse
-
#all_typed_pathes(type) ⇒ Object
/.TYPE.
- #config_file_loader ⇒ Object
- #config_pathes(name) ⇒ Object
- #exist?(name) ⇒ Boolean
- #file(name) ⇒ Object
- #glob(pattern) ⇒ Object
- #home_path ⇒ Object
-
#initialize(path, env) ⇒ FileSystem
constructor
A new instance of FileSystem.
- #inspect ⇒ Object
- #job_dir ⇒ Object
- #job_file(id) ⇒ Object
- #parameter_file(name, type) ⇒ Object
- #parameter_file_loader ⇒ Object
- #relative_path(name) ⇒ Object (also: #relative)
- #root ⇒ Object
- #root_relative_path(rel) ⇒ Object (also: #root_relative)
- #scoped? ⇒ Boolean
- #subdirectories ⇒ Object
- #subsystem(id) ⇒ Object
- #subsystems ⇒ Object
-
#typed_file(name, type) ⇒ Object
typed_file(“make_master”, “sql”) -> FileResource(“$prefix/make_master.sql”).
-
#typed_name(name, type) ⇒ Object
typed_name(“make_master”, “sql”) -> Pathname(“$prefix/make_master.sql”).
- #typed_pathes(type) ⇒ Object
Constructor Details
#initialize(path, env) ⇒ FileSystem
Returns a new instance of FileSystem.
32 33 34 35 36 |
# File 'lib/bricolage/filesystem.rb', line 32 def initialize(path, env) @path = Pathname(path) @environment = env @subsystems = {} end |
Instance Attribute Details
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
43 44 45 |
# File 'lib/bricolage/filesystem.rb', line 43 def environment @environment end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
42 43 44 |
# File 'lib/bricolage/filesystem.rb', line 42 def path @path end |
Class Method Details
.extract_home_dirs(job_path) ⇒ Object
job_path -> [home_path, subsys_id]
25 26 27 28 29 30 |
# File 'lib/bricolage/filesystem.rb', line 25 def FileSystem.extract_home_dirs(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 |
.for_option_pathes(home_path, job_path, env) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/bricolage/filesystem.rb', line 8 def FileSystem.for_option_pathes(home_path, job_path, env) if job_path home, subsys_id = extract_home_dirs(job_path) if home_path and home_path.realpath.to_s != home.realpath.to_s raise OptionError, "--home option and job file is exclusive" end new(home, env).subsystem(subsys_id) elsif home_path new(home_path, env) elsif home = ENV['BRICOLAGE_HOME'] new(home, env) else new(Pathname.getwd, env) end end |
Instance Method Details
#all_typed_pathes(type) ⇒ Object
/.TYPE
144 145 146 |
# File 'lib/bricolage/filesystem.rb', line 144 def all_typed_pathes(type) subsystems.map {|subsys| subsys.typed_pathes(type) }.flatten end |
#config_file_loader ⇒ Object
129 130 131 |
# File 'lib/bricolage/filesystem.rb', line 129 def config_file_loader ConfigLoader.new(home_path) end |
#config_pathes(name) ⇒ Object
125 126 127 |
# File 'lib/bricolage/filesystem.rb', line 125 def config_pathes(name) [ "config/#{name}", "config/#{@environment}/#{name}" ].map {|rel| root.relative(rel) } end |
#exist?(name) ⇒ Boolean
82 83 84 |
# File 'lib/bricolage/filesystem.rb', line 82 def exist?(name) relative(name).exist? end |
#file(name) ⇒ Object
86 87 88 |
# File 'lib/bricolage/filesystem.rb', line 86 def file(name) FileResource.new(relative(name)) end |
#glob(pattern) ⇒ Object
139 140 141 |
# File 'lib/bricolage/filesystem.rb', line 139 def glob(pattern) Dir.glob("#{@path}/#{pattern}").map {|path| Pathname(path) } end |
#home_path ⇒ Object
74 75 76 |
# File 'lib/bricolage/filesystem.rb', line 74 def home_path root.path end |
#inspect ⇒ Object
45 46 47 |
# File 'lib/bricolage/filesystem.rb', line 45 def inspect "\#<#{self.class} #{@path}>" end |
#job_dir ⇒ Object
78 79 80 |
# File 'lib/bricolage/filesystem.rb', line 78 def job_dir scoped? ? @path : nil end |
#job_file(id) ⇒ Object
133 134 135 136 137 |
# File 'lib/bricolage/filesystem.rb', line 133 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
117 118 119 |
# File 'lib/bricolage/filesystem.rb', line 117 def parameter_file(name, type) name.count('/') == 0 ? typed_file(name, type) : root.file(name) end |
#parameter_file_loader ⇒ Object
121 122 123 |
# File 'lib/bricolage/filesystem.rb', line 121 def parameter_file_loader ConfigLoader.new(home_path) end |
#relative_path(name) ⇒ Object Also known as: relative
96 97 98 99 100 101 102 103 |
# File 'lib/bricolage/filesystem.rb', line 96 def relative_path(name) path = Pathname(name) if path.absolute? path else @path + path end end |
#root ⇒ Object
49 50 51 |
# File 'lib/bricolage/filesystem.rb', line 49 def root self end |
#root_relative_path(rel) ⇒ Object Also known as: root_relative
90 91 92 |
# File 'lib/bricolage/filesystem.rb', line 90 def root_relative_path(rel) root.relative_path(rel) end |
#scoped? ⇒ Boolean
38 39 40 |
# File 'lib/bricolage/filesystem.rb', line 38 def scoped? false end |
#subdirectories ⇒ Object
70 71 72 |
# File 'lib/bricolage/filesystem.rb', line 70 def subdirectories @path.children(true).select {|path| path.directory? } end |
#subsystem(id) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/bricolage/filesystem.rb', line 53 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 |
#subsystems ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/bricolage/filesystem.rb', line 62 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”)
113 114 115 |
# File 'lib/bricolage/filesystem.rb', line 113 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”)
108 109 110 |
# File 'lib/bricolage/filesystem.rb', line 108 def typed_name(name, type) relative(name.count('.') > 0 ? name : "#{name}.#{type}") end |
#typed_pathes(type) ⇒ Object
148 149 150 |
# File 'lib/bricolage/filesystem.rb', line 148 def typed_pathes(type) @path.children.select {|path| path.file? and path.extname.to_s == ".#{type}" } end |