Class: Indexer::Importer

Inherits:
Object
  • Object
show all
Includes:
FileImportation, GemfileImportation, GemspecImportation, RubyImportation, VersionImportation, YAMLImportation
Defined in:
lib/indexer/importer.rb,
lib/indexer/importer/file.rb,
lib/indexer/importer/ruby.rb,
lib/indexer/importer/yaml.rb,
lib/indexer/importer/gemfile.rb,
lib/indexer/importer/gemspec.rb,
lib/indexer/importer/version.rb

Overview

Importer class takes disperate data sources and imports them into a Metadata instance.

Mixins are used to inject import behavior by overriding the #import method. Any such mixin's #import method must call #super if it's method doesn't apply, allowing the routine to fallback the other possible import methods.

Defined Under Namespace

Modules: FileImportation, GemfileImportation, GemspecImportation, RubyImportation, VersionImportation, YAMLImportation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from YAMLImportation

#load_yaml

Methods included from RubyImportation

#load_ruby

Methods included from FileImportation

#load_directory, #load_field_file, #read_customs

Constructor Details

#initialize(metadata = nil) ⇒ Importer

Initialize importer.



81
82
83
84
# File 'lib/indexer/importer.rb', line 81

def initialize(=nil)
  @metadata   =  || Metadata.new
  @file_cache = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a, &b) ⇒ Object

Evaluating on the Importer instance, allows Ruby basic metadata to be built via this method.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/indexer/importer.rb', line 117

def method_missing(s, *a, &b)
  return if s == :import

  r = s.to_s.chomp('=')
  case a.size
  when 0
    if .respond_to?(s)
      return .__send__(s, &b)
    end
  when 1
    if .respond_to?("#{r}=")
      return .__send__("#{r}=", *a)
    end
  else
    if .respond_to?("#{r}=")
      return .__send__("#{r}=", a)
    end
  end

  super(s, *a, &b)  # if cases don't match-up
end

Instance Attribute Details

#metadataObject (readonly)

Metadata being built.



89
90
91
# File 'lib/indexer/importer.rb', line 89

def 
  @metadata
end

Class Method Details

.import(*source) ⇒ Object

Import metadata from external sources.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/indexer/importer.rb', line 43

def self.import(*source)
  options = (Hash === source.last ? source.pop : {})

  require_importers

  #metadata = nil

  ## use source of current metadata if none given
  ## TODO: Only search the current directory or search up to root?
  if source.empty? 
    if file = Dir[LOCK_FILE].first  #or `Metadata.exists?` ?
      data   = YAML.load_file(file)
      source = Array(data['source'])
    end
  end

  if source.empty?
    source = [USER_FILE]
  end

  source.each do |file|
    unless File.exist?(file)
      warn "metadata source file not found - `#{file}'"
    end
  end

  importer = Importer.new #(metadata)

  source.each do |src|
    importer.import(src)
  end

  return importer.
end

.require_importersObject

Require all import mixins.

This method calls super if it is defined which makes it easy for plugins to add new importers.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/indexer/importer.rb', line 24

def self.require_importers
  require_relative 'importer/file'
  require_relative 'importer/ruby'
  require_relative 'importer/yaml'
  require_relative 'importer/gemspec'
  require_relative 'importer/gemfile'
  require_relative 'importer/version'
  #require_relative 'importer/html'
  #require_relative 'importer/markdown'
  #require_relative 'importer/rdoc'
  #require_relative 'importer/textile'

  # for plugins to easily add additional importers
  super if defined?(super)
end

Instance Method Details

#import(source) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/indexer/importer.rb', line 94

def import(source)
  success = super(source) if defined?(super)
  if success
    .sources << source unless .sources.include?(source)
  else
    raise "metadata source not found or not a known type -- #{source}"
  end
end

#read(file) ⇒ Object

Provides a file contents cache. This is used by the YAMLImportation script, for instance, to see if the file begins with ---, in which case the file is taken to be YAML format, even if the file's extension is not .yml or .yaml.



109
110
111
# File 'lib/indexer/importer.rb', line 109

def read(file)
  @file_cache[file] ||= File.read(file)
end

#yaml?(text) ⇒ Boolean

TODO:

Ignore top comments.

Is text a YAML document? It detrmines this simply be checking for --- at the top of the text.

Returns:

  • (Boolean)


145
146
147
# File 'lib/indexer/importer.rb', line 145

def yaml?(text)
  text =~ /\A(---|%TAG|%YAML)/
end