Module: Indexer::Loadable

Included in:
Metadata
Defined in:
lib/indexer/loadable.rb

Instance Method Summary collapse

Instance Method Details

#exist?(from = Dir.pwd) ⇒ Boolean

Alias for exists?

Returns:

  • (Boolean)


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?

Returns:

  • (true, false)

    Whether locked metadata file exists.



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.expand_path('~')
  path = File.expand_path(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.

Parameters:

  • from (String) (defaults to: Dir.pwd)

    The directory from which to start the upward search.



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.

Parameters:

  • String (String, #read)

    or IO object The file name from which to read the YAML metadata.



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.

Parameters:

  • file (String)

    The file name from which to read the YAML metadata.



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 a metadata source.") 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.

Parameters:

  • file (String) (defaults to: Dir.pwd)

    or directory The file name from which to read the YAML metadata, or a directory from which to lookup the file.



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.

Parameters:

  • file (String) (defaults to: Dir.pwd)

    or directory The file name from which to read the YAML metadata, or a directory from which to lookup the file.



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.

Parameters:

  • from (String) (defaults to: Dir.pwd)

    The directory from which to start the upward search.

Returns:

  • (String)

    The path to the locked metadata file.

Raises:

  • (Errno::ENOENT)

    The locked metadata file could not be located.



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