Class: Glyph::Analyzer

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/glyph/analyzer.rb

Overview

This class is used to collect statistics about a Glyph document.

Since:

  • 0.4.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#clean_xml_document, #complex_output?, #current_output_setting, #debug, #error, #file_copy, #file_load, #file_write, #info, #load_files_from_dir, #macro_alias?, #macro_aliases_for, #macro_definition_for, #macro_eq?, #msg, #multiple_output_files?, #project?, #run_external_command, #titled_sections, #warning, #with_files_from, #yaml_dump, #yaml_load

Constructor Details

#initialize(doc = Glyph.document) ⇒ Analyzer

Initializes a new Analyzer

Parameters:

  • doc (Glyph::Document) (defaults to: Glyph.document)

    the document to analyze

Since:

  • 0.4.0



15
16
17
18
19
20
# File 'lib/glyph/analyzer.rb', line 15

def initialize(doc=Glyph.document)
  @doc = doc
  @stats = {}
  @macros = []
  @macros_by_def = {}
end

Instance Attribute Details

#statsObject (readonly)

Since:

  • 0.4.0



11
12
13
# File 'lib/glyph/analyzer.rb', line 11

def stats
  @stats
end

Instance Method Details

#macro_array_for(name) ⇒ Object

Helper method used to return an array of specific macro instances

Parameters:

  • name (String, Symbol)

    the name of the macro definition

Since:

  • 0.4.0



24
25
26
27
28
29
# File 'lib/glyph/analyzer.rb', line 24

def macro_array_for(name)
  return @macros_by_def[name.to_sym] if @macros_by_def[name.to_sym]
  key = @macros_by_def.keys.select{|k| macro_eq?(k, name.to_sym) }[0] || name.to_sym
  @macros_by_def[key] = [] unless @macros_by_def[key]
  @macros_by_def[key]
end

#stats_for(stats_type, *args) ⇒ Object

Retrieves statistics of a given type (:macros, :bookmarks, :links, :snippets, :files, :global, :macro, :bookmark, :snippet, :link)

Parameters:

  • stats_type (String, Symbol)

    the type of stats to retrieve

  • *args (String, Symbol)

    Stats parameter(s) (e.g. a macro name, bookmark ID, etc.)

Since:

  • 0.4.0



70
71
72
73
74
75
76
# File 'lib/glyph/analyzer.rb', line 70

def stats_for(stats_type, *args)
  begin
    send :"stats_#{stats_type}", *args 
  rescue NoMethodError => e
    raise RuntimeError, "Unable to calculate #{stats_type} stats"
  end
end

#with_macros(name = nil) {|n| ... } ⇒ Object

Iterator over specific macro instances

Parameters:

  • name (String, Symbol) (defaults to: nil)

    the name of the macro definition (if left blank, iterates over all macro instances)

Yield Parameters:

Raises:

  • (ArgumentError)

Since:

  • 0.4.0



34
35
36
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
# File 'lib/glyph/analyzer.rb', line 34

def with_macros(name=nil, &block)
  raise ArgumentError, "No block given" unless block_given?
  name = name.to_sym if name
  if !name then
    unless @macros.blank? then
      @macros.each(&block)
    else
      @doc.structure.descend do |n, level|
        if n.is_a?(Glyph::MacroNode) && n.source
          @macros << n
          macro_array_for(n[:name]) << n
          block.call n
        end
      end
    end
  else
    existing = @macros_by_def[name]
    if existing then
      existing.each(&block)
    else
      macros = []
      @doc.structure.descend do |n, level|
        if n.is_a?(Glyph::MacroNode) && macro_eq?(name, n[:name]) && n.source
          macros << n
          block.call n
        end
      end
      @macros_by_def[name] = macros
    end
  end
end