Module: Indexer::Loadable
- Included in:
- Metadata
- Defined in:
- lib/indexer/loadable.rb
Instance Method Summary collapse
-
#exist?(from = Dir.pwd) ⇒ Boolean
Alias for exists?.
-
#exists?(from = Dir.pwd) ⇒ true, false
Does a locked metadata file exist?.
-
#find(from = Dir.pwd) ⇒ Object
Find project root and read the index file.
-
#import(*sources) ⇒ Object
Create new Metadata instance from atypical sources.
-
#load(io) ⇒ Object
Load from YAML string or IO.
-
#load_file(file) ⇒ Object
Load from YAML file.
-
#lock(*sources) ⇒ Object
Lockdown the metadata (via Importer) and return updated metadata.
-
#lock!(*sources) ⇒ Object
Lockdown the metadata (via Importer) and save.
-
#lock_file_missing(from = nil) ⇒ Object
Raise lock file missing error.
-
#open(file = Dir.pwd) ⇒ Object
Open metadata file and ensure strict validation to canonical format.
-
#read(file = Dir.pwd) ⇒ Object
Like #open, but do not ensure strict validation to canonical format.
-
#root(from = Dir.pwd) ⇒ String
Find project root by looking upward for a locked metadata file.
Instance Method Details
#exist?(from = Dir.pwd) ⇒ Boolean
Alias for exists?
115 116 117 |
# File 'lib/indexer/loadable.rb', line 115 def exist?(from=Dir.pwd) exists?(from) end |
#exists?(from = Dir.pwd) ⇒ true, false
Does a locked metadata file exist?
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/indexer/loadable.rb', line 91 def exists?(from=Dir.pwd) home = File.('~') path = File.(from) while path != '/' and path != home if File.file?(File.join(path,LOCK_FILE)) return path else path = File.dirname(path) end false #lock_file_missing(from) end false #lock_file_missing(from) end |
#find(from = Dir.pwd) ⇒ Object
Find project root and read the index file.
63 64 65 |
# File 'lib/indexer/loadable.rb', line 63 def find(from=Dir.pwd) File.join(root(from), LOCK_FILE) end |
#import(*sources) ⇒ Object
Create new Metadata instance from atypical sources.
TODO: Use Importer to construct Metadata instance.
34 35 |
# File 'lib/indexer/loadable.rb', line 34 def import(*sources) end |
#load(io) ⇒ Object
Load from YAML string or IO.
43 44 45 |
# File 'lib/indexer/loadable.rb', line 43 def load(io) new(YAML.load(io)) end |
#load_file(file) ⇒ Object
Load from YAML file.
53 54 55 |
# File 'lib/indexer/loadable.rb', line 53 def load_file(file) new(YAML.load_file(file)) end |
#lock(*sources) ⇒ Object
Lockdown the metadata (via Importer) and return updated metadata.
Options :force
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/indexer/loadable.rb', line 124 def lock(*sources) opts = (Hash === sources.last ? sources.pop : {}) file = nil needed = true sources = sources.flatten if sources.empty? if file = exists? = Metadata.open sources = .sources else sources = Dir.glob(USER_FILES, File::FNM_CASEFOLD) raise Error.exception("could not find default sources") if sources.empty? end end if file && !opts[:force] date = sources.map{ |s| File.mtime(s) }.max needed = false if File.mtime(file) > date end Importer.import(*sources) if needed end |
#lock!(*sources) ⇒ Object
Lockdown the metadata (via Importer) and save.
152 153 154 155 |
# File 'lib/indexer/loadable.rb', line 152 def lock!(*sources) = lock(*sources) .save! if end |
#lock_file_missing(from = nil) ⇒ Object
Raise lock file missing error.
108 109 110 |
# File 'lib/indexer/loadable.rb', line 108 def lock_file_missing(from=nil) raise Error.exception("could not locate .index file", Errno::ENOENT) end |
#open(file = Dir.pwd) ⇒ Object
Open metadata file and ensure strict validation to canonical format.
12 13 14 15 |
# File 'lib/indexer/loadable.rb', line 12 def open(file=Dir.pwd) file = find(file) if File.directory?(file) valid(YAML.load_file(file)) end |
#read(file = Dir.pwd) ⇒ Object
Like #open, but do not ensure strict validation to canonical format.
24 25 26 27 |
# File 'lib/indexer/loadable.rb', line 24 def read(file=Dir.pwd) file = find(file) if File.directory?(file) new(YAML.load_file(file)) end |
#root(from = Dir.pwd) ⇒ String
Find project root by looking upward for a locked metadata file.
79 80 81 82 83 84 |
# File 'lib/indexer/loadable.rb', line 79 def root(from=Dir.pwd) if not path = exists?(from) lock_file_missing(from) end path end |