class Asciidoctor::Diagram::BasicSource

Base class for diagram source implementations that uses an md5 checksum of the source code of a diagram to determine if it has been updated or not.

Attributes

attributes[R]

Public Class Methods

new(block_processor, parent_block, attributes) click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 149
def initialize(block_processor, parent_block, attributes)
  @block_processor = block_processor
  @parent_block = parent_block
  @attributes = attributes
end

Public Instance Methods

attr(name, default_value = nil, inherit = diagram_type) click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 171
def attr(name, default_value = nil, inherit = diagram_type)
  name = name.to_s if ::Symbol === name
  name = [name] unless name.is_a?(Enumerable)

  value = name.lazy.map { |n| @attributes[n] }.reject { |v| v.nil? }.first

  if value.nil? && inherit
    inherited_values = name.lazy.map do |n|
      case inherit
      when String, Symbol
        @parent_block.attr("#{inherit.to_s}-#{n}", default_value, true)
      else
        @parent_block.attr(n, default_value, inherit)
      end
    end
    value = inherited_values.reject { |v| v.nil? }.first
  end

  value || default_value
end
checksum() click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 200
def checksum
  @checksum ||= compute_checksum(code)
end
config() click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 163
def config
  @block_processor.config
end
create_image_metadata() click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 196
def create_image_metadata
  {:checksum => checksum}
end
diagram_type() click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 155
def diagram_type
  @block_processor.name.downcase
end
image_name() click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 167
def image_name
  attr('target', 'diag-' + checksum)
end
resolve_path(target, start = base_dir) click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 159
def resolve_path target, start = base_dir
  @parent_block.normalize_system_path(target, start)
end
should_process?(image_file, image_metadata) click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 192
def should_process?(image_file, image_metadata)
  image_metadata[:checksum] != checksum
end

Protected Instance Methods

resolve_diagram_subs() click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 206
def resolve_diagram_subs
  if @attributes.key? 'subs'
    @parent_block.resolve_block_subs @attributes['subs'], nil, 'diagram'
  else
    []
  end
end

Private Instance Methods

compute_checksum(code) click to toggle source
# File lib/asciidoctor-diagram/diagram_source.rb, line 216
def compute_checksum(code)
  md5 = Digest::MD5.new
  md5 << code
  @attributes.each do |k, v|
    md5 << k.to_s if k
    md5 << v.to_s if v
  end
  md5.hexdigest
end