Sha256: bebb88fad90219e5490371445934a3cf6005f053eb7dfe29482e1796df7b6024

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

module Sablon
  # A context represents the user supplied arguments to render a
  # template.
  #
  # This module contains transformation functions to turn a
  # user supplied hash into a data structure suitable for rendering the
  # docx template.
  module Context
    class << self
      def transform_hash(hash)
        Hash[hash.map { |k, v| transform_pair(k.to_s, v) }]
      end

      private

      def transform_standard_key(key, value)
        case value
        when Hash
          [key, transform_hash(value)]
        when Array
          [key, value.map { |v| v.is_a?(Hash) ? transform_hash(v) : v }]
        else
          [key, value]
        end
      end

      def transform_pair(key, value)
        if key =~ /\A([^:]+):(.+)\z/
          if value.nil?
            [Regexp.last_match[2], value]
          else
            key_sym = Regexp.last_match[1].to_sym
            [Regexp.last_match[2], Content.make(key_sym, value)]
          end
        else
          transform_standard_key(key, value)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sablon-0.4.0 lib/sablon/context.rb
sablon-0.3.2 lib/sablon/context.rb
sablon-0.3.1 lib/sablon/context.rb
sablon-0.3.0 lib/sablon/context.rb
sablon-0.2.1 lib/sablon/context.rb
sablon-0.2.0 lib/sablon/context.rb