Class: Indexer::Importer

Inherits:
Object
  • Object
show all
Includes:
FileImportation, GemfileImportation, GemspecImportation, HTMLImportation, MarkdownImportation, RubyImportation, VersionImportation, YAMLImportation
Defined in:
lib/indexer/importer.rb,
lib/indexer/importer/file.rb,
lib/indexer/importer/html.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,
lib/indexer/importer/markdown.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, HTMLImportation, MarkdownImportation, RubyImportation, VersionImportation, YAMLImportation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MarkdownImportation

#load_markdown, #render_with_kramdown, #render_with_redcarpet

Methods included from YAMLImportation

#load_yaml

Methods included from RubyImportation

#load_ruby

Methods included from HTMLImportation

#load_html, #load_html_authors, #load_html_categories, #load_html_copyrights, #load_html_name, #load_html_organizations, #load_html_repositories, #load_html_requirements, #load_html_resources, #load_html_simple, #load_html_title

Methods included from FileImportation

#load_directory, #load_field_file, #read_customs

Constructor Details

#initialize(metadata = nil) ⇒ Importer

Initialize importer.



75
76
77
78
# File 'lib/indexer/importer.rb', line 75

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.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/indexer/importer.rb', line 111

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.



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

def 
  @metadata
end

Class Method Details

.import(*source) ⇒ Object

Import metadata from external sources.



37
38
39
40
41
42
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
# File 'lib/indexer/importer.rb', line 37

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.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/indexer/importer.rb', line 21

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

Instance Method Details

#import(source) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/indexer/importer.rb', line 88

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.



103
104
105
# File 'lib/indexer/importer.rb', line 103

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)


139
140
141
# File 'lib/indexer/importer.rb', line 139

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