Class: Sourcery::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/sourcery/metadata.rb

Constant Summary collapse

ROOT_INDICATORS =

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

['.ruby,var/,meta/,.meta/,.git,.hg,_darcs']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ Metadata

Initialize new Metadata object.



14
15
16
17
18
19
20
21
22
# 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)

  
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#rootObject (readonly)

Project root pathname.



9
10
11
# File 'lib/sourcery/metadata.rb', line 9

def root
  @root
end

Class Method Details

.locate_root_at(indicator) ⇒ Object

Helper method for ‘Metadata.root()`.



122
123
124
125
126
127
128
129
130
131
132
# 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

.root(local = Dir.pwd) ⇒ Object

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.



108
109
110
111
112
113
114
115
116
117
# 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

Instance Method Details

#to_hObject

Return copy of metadata store.



42
43
44
# File 'lib/sourcery/metadata.rb', line 42

def to_h
  @cache.dup
end