Class: Erbside::Metadata

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

Overview

Metadata belongs to the project being scaffold.

Constant Summary collapse

CANONICAL_FILENAME =

Canonical metadata file.

'.ruby'
ROOT_INDICATORS =

Root directory is indicated by the presence of a meta/ directory, or .meta/ hidden directory.

[ "{#{CANONICAL_FILENAME},meta/,.meta/}" ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resources, options = {}) ⇒ Metadata

Construct new Metadata object.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/erbside/metadata.rb', line 23

def initialize(resources, options={})
  @root = self.class.root(options[:root]) || Dir.pwd
  @data = []

  @resources = [resources].flatten.compact
  @resources << '.ruby' if canonical?

  @resources.each do |source|
    case File.basename(source)
    when CANONICAL_FILENAME
      if canonical?
        if defined?(::DotRuby)
          @data << DotRuby::Spec.find(root) #(CANONICAL_FILENAME)
        else
          @data << YAML.load_file(canonical_file)
        end
      end
    when /\.gemspec$/
      require 'erbside/gemspec'
      @data << ::Gem::Specification.load(source)
    when /\.ya?ml$/
      @data << YAML.load_file(source)
    else
      if File.directory?(source)
        @data << load_metadir(source)
      else
        # now what ?
      end
    end
  end

  @cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

If method is missing, check the POM and metadata cache.



58
59
60
61
# File 'lib/erbside/metadata.rb', line 58

def method_missing(name, *args)
  return super unless args.empty?
  self[name]
end

Instance Attribute Details

#resourcesObject (readonly)

Data resources.



20
21
22
# File 'lib/erbside/metadata.rb', line 20

def resources
  @resources
end

#rootObject (readonly)

Project root pathname.



17
18
19
# File 'lib/erbside/metadata.rb', line 17

def root
  @root
end

Instance Method Details

#[](name) ⇒ Object

Lookup metadata entry.



64
65
66
67
68
69
70
71
72
# File 'lib/erbside/metadata.rb', line 64

def [](name)
  name, value = name.to_s, nil
  return @cache[name] if @cache.key?(name)
  begin
    @data.find{ |d| value = d[name] }
  rescue
  end
  @cache[name] = value
end

#to_hObject

Provide metadata to hash. Some (stencil) template systems need the data in hash form.



76
77
78
# File 'lib/erbside/metadata.rb', line 76

def to_h
  @data.reverse.inject({}){ |h,d| h.merge(d.to_h) }
end