Class: Asciidoctor::Diagram::BasicSource

Inherits:
Object
  • Object
show all
Includes:
DiagramSource
Defined in:
lib/asciidoctor-diagram/diagram_source.rb

Overview

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.

Direct Known Subclasses

FileSource, ReaderSource

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DiagramSource

#base_dir, #code, #ensure_gem, #find_command, #global_attr, #global_opt, #load_code, #opt, #to_s

Constructor Details

#initialize(block_processor, parent_block, attributes) ⇒ BasicSource

Returns a new instance of BasicSource.



178
179
180
181
182
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 178

def initialize(block_processor, parent_block, attributes)
  @block_processor = block_processor
  @parent_block = parent_block
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



176
177
178
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 176

def attributes
  @attributes
end

Instance Method Details

#attr(name, default_value = nil, inherit = diagram_type) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 200

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?
    attr_position = config[:positional_attrs] || 1
    while value.nil? && !@attributes[attr_position].nil?
      if @attributes[attr_position] == name
        value = true
      end
    end
  end

  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

#checksumObject



237
238
239
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 237

def checksum
  @checksum ||= "#{diagram_type.to_s}-#{compute_checksum(code)}"
end

#configObject



192
193
194
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 192

def config
  @block_processor.config
end

#create_image_metadataObject



233
234
235
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 233

def 
  {:checksum => checksum}
end

#diagram_typeObject



184
185
186
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 184

def diagram_type
  @block_processor.name.downcase
end

#image_nameObject



196
197
198
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 196

def image_name
  attr('target', 'diag-' + checksum)
end

#resolve_path(target, start = base_dir) ⇒ Object



188
189
190
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 188

def resolve_path target, start = base_dir
  @parent_block.normalize_system_path(target, start)
end

#should_process?(image_file, image_metadata) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/asciidoctor-diagram/diagram_source.rb', line 229

def should_process?(image_file, )
  [:checksum] != checksum
end