class Kumogata2::Plugin::Ruby::Context

Public Instance Methods

template(&block) click to toggle source
# File lib/kumogata/template/ext/kumogata.rb, line 56
def template(&block)
  key_converter = proc do |k|
    k = k.to_s
    k.gsub!('____', '-')
    k.gsub!('___', '.')
    k.gsub!('__', '::')
    k.gsub!('_', ':')
    k
  end

  value_converter = proc do |v|
    case v
    when Hash, Array
      v
    else
      v.to_s
    end
  end

  @_template = Dslh.eval({
    key_conv: key_converter,
    value_conv: value_converter,
    scope_hook: proc {|scope|
      define_template_func(scope, @options.path_or_url)
    },
    filename: @options.path_or_url,
    ignore_methods: IGNORE_METHODS,
  }, &block)
end

Private Instance Methods

define_template_func(scope, path_or_url) click to toggle source
# File lib/kumogata/template/ext/kumogata.rb, line 88
  def define_template_func(scope, path_or_url)
    functions = ''
    Dir.glob(File.join(Kumogata2::Client::get_template_path, '*.rb')).all? do |file|
      functions << include_func(path_or_url, file)
      functions << "\n\n"
    end

    scope.instance_eval(<<-EOS)
#{functions}

      def _include(file, args = {})
        path = file.dup

        unless path =~ %r|\\A/| or path =~ %r|\\A\\w+://|
          path = File.expand_path(File.join(File.dirname(#{path_or_url.inspect}), path))
        end

        open(path) {|f| instance_eval(f.read) }
      end

      def _path(path, value = nil, &block)
        if block
          value = Dslh::ScopeBlock.nest(binding, 'block')
        end

        @__hash__[path] = value
      end

      def _outputs_filter(&block)
        @__hash__[:_outputs_filter] = block
      end

      def _post(options = {}, &block)
        commands = Dslh::ScopeBlock.nest(binding, 'block')

        @__hash__[:_post] = {
          :options  => options,
          :commands => commands,
        }
      end
    EOS
  end
get_funcname(file) click to toggle source
# File lib/kumogata/template/ext/kumogata.rb, line 147
def get_funcname(file)
  File.basename(file, '.rb').gsub('-', '_')
end
include_func(path_or_url, file) click to toggle source
# File lib/kumogata/template/ext/kumogata.rb, line 131
  def include_func(path_or_url, file)
    <<-EOS
      def _#{get_funcname(file)}(name, args = {})
        args[:name] = name unless args.key? :name

        path = "#{file}"

        unless path =~ %r|\\A/| or path =~ %r|\\A\\w+://|
          path = File.expand_path(File.join(File.dirname(#{path_or_url.inspect}), path))
        end

        open(path) {|f| instance_eval(f.read) }
      end
    EOS
  end