Sha256: 0f72280ec7b9d7d94a85d26ff4cf546946f06e90b27fe9ae0956185a0296da55

Contents?: true

Size: 1.32 KB

Versions: 10

Compression:

Stored size: 1.32 KB

Contents

require 'active_support'
require 'active_support/core_ext/hash'

module Fortitude
  module MethodTemplates
    class SimpleTemplate
      class << self
        def template(name)
          @templates ||= { }
          @templates[name] ||= new(File.join(File.dirname(__FILE__), "#{name}.rb.smpl"))
        end
      end

      def initialize(source_file)
        @lines = File.read(source_file).split(/\r\n|\r|\n/)
      end

      def result(bindings)
        bindings = bindings.stringify_keys
        bindings_target = Object.new
        bindings.each do |key, value|
          (class << bindings_target; self; end).send(:define_method, key) { value }
        end

        result_lines = [ ]
        @lines.each do |l|
          if l =~ /^(.*)\#\s*\:if\s*(.*?)\s*$/i
            l, condition = $1, $2
            next unless bindings_target.instance_eval(condition)
          end

          while l =~ /[^\\]\#\{([^}]+)\}/ || l =~ /^\#\{([^}]+)\}/
            name = $1
            begin
              value = bindings_target.send($1)
            rescue => e
              raise "Failed when processing #{l.inspect}: #{e.inspect}"
            end
            l = l.gsub("\#\{#{$1}\}", value.to_s)
          end
          l = l.gsub(/\\\#\{/, "\#\{")

          result_lines << l
        end

        result_lines.join("\n")
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
fortitude-0.0.7-java lib/fortitude/method_templates/simple_template.rb
fortitude-0.0.7 lib/fortitude/method_templates/simple_template.rb
fortitude-0.0.6-java lib/fortitude/method_templates/simple_template.rb
fortitude-0.0.6 lib/fortitude/method_templates/simple_template.rb
fortitude-0.0.5-java lib/fortitude/method_templates/simple_template.rb
fortitude-0.0.5 lib/fortitude/method_templates/simple_template.rb
fortitude-0.0.4-java lib/fortitude/method_templates/simple_template.rb
fortitude-0.0.4 lib/fortitude/method_templates/simple_template.rb
fortitude-0.0.3 lib/fortitude/method_templates/simple_template.rb
fortitude-0.0.2 lib/fortitude/method_templates/simple_template.rb