Class: Glyph::Analyzer
Overview
This class is used to collect statistics about a Glyph document.
Instance Attribute Summary collapse
- #stats ⇒ Object readonly
Instance Method Summary collapse
-
#initialize(doc = Glyph.document) ⇒ Analyzer
constructor
Initializes a new Analyzer.
-
#macro_array_for(name) ⇒ Object
Helper method used to return an array of specific macro instances.
-
#stats_for(stats_type, *args) ⇒ Object
Retrieves statistics of a given type (:macros, :bookmarks, :links, :snippets, :files, :global, :macro, :bookmark, :snippet, :link).
-
#with_macros(name = nil) {|n| ... } ⇒ Object
Iterator over specific macro instances.
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
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
#stats ⇒ Object (readonly)
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
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)
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
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 |