Module: Fileable::DSL
- Defined in:
- lib/more/facets/fileable.rb
Instance Method Summary collapse
-
#file(path = nil) ⇒ Object
Find file.
-
#filename ⇒ Object
Override this with the name or name-glob of the default file.
-
#included(base) ⇒ Object
While this doesn’t allpy to classes, for modules it is needed to keep the DSL inheritance going.
-
#load(path_or_data) ⇒ Object
An initializer that can take either a File, Pathname or raw data.
-
#load_cache ⇒ Object
Load cache.
-
#locate(name = nil) ⇒ Object
Locate file (case insensitive).
-
#lookup(name = nil) ⇒ Object
Lookup file.
-
#open(path = nil) ⇒ Object
Load from file(s).
Instance Method Details
#file(path = nil) ⇒ Object
Find file. The path has to be either the exact path or the directory where a standard-named file resides.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/more/facets/fileable.rb', line 133 def file(path=nil) if !path raise LoadError unless filename path = filename elsif File.directory?(path) raise LoadError unless filename path = File.join(path, filename) end if file = Dir.glob(path, File::FNM_CASEFOLD)[0] File.(file) else raise Errno::ENOENT end end |
#filename ⇒ Object
Override this with the name or name-glob of the default file. If no default, return nil.
79 |
# File 'lib/more/facets/fileable.rb', line 79 def filename; nil; end |
#included(base) ⇒ Object
While this doesn’t allpy to classes, for modules it is needed to keep the DSL inheritance going.
72 73 74 |
# File 'lib/more/facets/fileable.rb', line 72 def included(base) base.extend DSL end |
#load(path_or_data) ⇒ Object
An initializer that can take either a File, Pathname or raw data. This works much like YAML::load does. Unlike open, load requires an exact path parameter.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/more/facets/fileable.rb', line 96 def load(path_or_data) case path_or_data when File open(path_or_data.path) when Pathname open(path_or_data.realpath) else new(path_or_data) end end |
#load_cache ⇒ Object
Load cache. PackageInfo is multiton when loaded by file.
150 151 152 |
# File 'lib/more/facets/fileable.rb', line 150 def load_cache @load_cache ||= {} end |
#locate(name = nil) ⇒ Object
Locate file (case insensitive).
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/more/facets/fileable.rb', line 116 def locate(name=nil) name ||= filename raise LoadError unless name Dir.ascend(Dir.pwd) do |dir| match = File.join(dir, name) files = Dir.glob(match, File::FNM_CASEFOLD) if file = files[0] return file end end return nil end |
#lookup(name = nil) ⇒ Object
Lookup file.
109 110 111 112 |
# File 'lib/more/facets/fileable.rb', line 109 def lookup(name=nil) file = locate(name) file ? open(file) : nil #raise LoadError end |
#open(path = nil) ⇒ Object
Load from file(s).
83 84 85 86 87 88 89 90 |
# File 'lib/more/facets/fileable.rb', line 83 def open(path=nil) file = file(path) if file fobj = new fobj.send(:read, file) return fobj end end |