class Sourcery::Metadata

Constants

ROOT_INDICATORS

Root directory is indicated by the presence of a src/ directory.

Attributes

root[R]

Project root pathname.

Public Class Methods

locate_root_at(indicator) click to toggle source

Helper method for `Metadata.root()`.

# File lib/sourcery/metadata.rb, line 122
def self.locate_root_at(indicator)
  root = nil
  dir  = Dir.pwd
  while !root && dir != '/'
    find = File.join(dir, indicator)
    root = Dir.glob(find, File::FNM_CASEFOLD).first
    #break if root
    dir = File.dirname(dir)
  end
  root ? Pathname.new(root) : nil
end
new(root=nil) click to toggle source

Initialize new Metadata object.

# File lib/sourcery/metadata.rb, line 14
def initialize(root=nil)
  #@root = self.class.root(root) || Dir.pwd
  @root  = root || Dir.pwd
  @cache = {}

  raise "not a directory -- #{root}" unless File.directory?(root)

  load_metadata
end
root(local=Dir.pwd) click to toggle source

Locate the project's root directory. This is determined by ascending up the directory tree from the current position until the ROOT_INDICATORS is matched. Returns nil if not found.

# File lib/sourcery/metadata.rb, line 108
def self.root(local=Dir.pwd)
  local ||= Dir.pwd
  Dir.chdir(local) do
    dir = nil
    ROOT_INDICATORS.find do |i|
      dir = locate_root_at(i)
    end
    dir ? Pathname.new(File.dirname(dir)) : nil
  end
end

Public Instance Methods

method_missing(s, *a) click to toggle source

If method is missing see if there is a metadata entry for it.

Calls superclass method
# File lib/sourcery/metadata.rb, line 27
def method_missing(s, *a)
  m = s.to_s
  case m
  when /=$/
    raise ArgumentError if a.size != 1
    @cache[s.to_s] = a.first
  else
    super(s, *a) unless a.empty?
    @cache[s.to_s]
  end
end
to_h() click to toggle source

Return copy of metadata store.

# File lib/sourcery/metadata.rb, line 42
def to_h
  @cache.dup
end

Private Instance Methods

load_metadata() click to toggle source

Load metadata.

# File lib/sourcery/metadata.rb, line 51
def load_metadata
  load_metadata_from_directory
  load_metadata_from_dotruby
end
load_metadata_from_directory() click to toggle source

Load metadata from metadata directory.

# File lib/sourcery/metadata.rb, line 59
def load_metadata_from_directory
  entries = Dir.glob(File.join(meta_dir, '*'))
  entries.each do |f|
    val = File.read(f).strip
    val = YAML.load(val) if val =~ /\A---/
    @cache[File.basename(f)] = val
  end
end
load_metadata_from_dotruby() click to toggle source

Load metadata from .ruby file.

# File lib/sourcery/metadata.rb, line 71
def load_metadata_from_dotruby
  file = Dir[File.join(root, '.ruby')].first
  if file
    @cache.update(YAML.load_file(file))
  end
end
meta_dir() click to toggle source

Locate the project's metadata directory. This is either `meta` or `.meta` or `var`.

# File lib/sourcery/metadata.rb, line 82
def meta_dir
  @meta_dir ||= Dir[File.join(root, '{meta,.meta,var}/')].first || '.meta/'
end