lib/dagger/generator.rb in ruby-dagger-0.1.1 vs lib/dagger/generator.rb in ruby-dagger-0.2.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'forwardable' module Dagger # Abstract super class for default value generators. # Stores the +Context+ on initialization, and provides private @@ -38,20 +40,18 @@ @context = context end private - delegate %i[dictionary] => :@context + delegate %i[dictionary vertex] => :@context # Stop processing the current rule chain # # :call-seq: # stop - # - # Raises +StopIteration+ def stop - raise StopIteration + throw @context.stop end # Update context attributes with new values # # :call-seq: @@ -64,9 +64,23 @@ # # :call-seq: # enumerable(value) => value || [value] def enumerable(value) value.respond_to?(:each) ? value : [value] + end + + # Format a +string+ with values from a +dictionary+ + # + # :call-seq: + # format(string) + def format_string(string) + hash = Hash.new do |_, key| + result = @context.dictionary[key] + next result unless result.nil? + return nil + end + + format(string, hash) end end end Dir[__dir__ + '/generator/*.rb'].each { |file| load(file) }