Class: PluginTool::SchemaRequirements

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

Constant Summary collapse

IGNORE_LINE =

comment or blank line

/\A\s*(\#.+)?\z/m
OBJECT_FORMAT =
/\A(?<kind>\S+)\s+(?<code>[a-zA-Z0-9_:-]+)\s*(as\s+(?<name>[a-zA-Z0-9_]+))?\s*\z/m
SCHEMA_LOCALS =
{
  "type" => "T",
  "attribute" => "A",
  "aliased-attribute" => "AA",
  "qualifier" => "Q",
  "label" => "Label",
  "group" => "Group"
}
FIND_USE_REGEXP =
Regexp.new("\\b(#{SCHEMA_LOCALS.values.join('|')})\\.([a-zA-Z0-9]+)\\b")

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ SchemaRequirements

Returns a new instance of SchemaRequirements.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/schema_requirements.rb', line 17

def initialize(filename)
  # Define the special attributes by default
  @schema_names = {"A" => {
    "Parent" => true,
    "Type" => true,
    "Title" => true
  }}
  return unless File.exist?(filename)
  File.open(filename) do |file|
    file.each do |line|
      if (line !~ IGNORE_LINE) && (match = OBJECT_FORMAT.match(line))
        kind = SCHEMA_LOCALS[match[:kind]] || 'UNUSED'
        @schema_names[kind] ||= {}
        name = match[:name]
        if name && !name.empty?
          @schema_names[kind][name] = true
        end
      end
    end
  end
  @schema_names.delete('UNUSED')
end

Instance Method Details

#localsObject



40
41
42
# File 'lib/schema_requirements.rb', line 40

def locals
  @schema_names.keys.dup
end

#report_usage(js) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/schema_requirements.rb', line 46

def report_usage(js)
  report = []
  js.split(/\r?\n/).each_with_index do |line, index|
    line.scan(FIND_USE_REGEXP) do
      kind = $1
      name = $2
      lookup = @schema_names[kind]
      unless lookup && lookup.has_key?(name)
        report << "line #{index+1}: Schema name #{kind}.#{name} isn't declared in requirements.schema\n\n"
      end
    end
  end
  report.empty? ? nil : report.join
end